zoukankan      html  css  js  c++  java
  • 数据结构:单链表 头尾插入,头尾删除,任意位置插入,任意位置删除

    本小结中心思想

      主要是为了深入理解链表和熟练的对链表操作,在定义count时一定要初始化,即:int count  = 0;。

    头结点和尾结点

    1 在链表的任意位置

    插入结点的操作中要把头结点和尾结点拿出来单独讨论

      假设头结点的位置为1,例如,链表数据:39 99 2  5 10,各个数字对应的位置是:1 2 3 4 5,五个数字,有六个空可以插入。在1位置插入数据9后链表数据为:9 39 99 2  5 10,也就是插入到39(position=1)的前面。

    头结点:

     1 //如果在位置1处插入结点
     2 if (pHead == cur){
     3     temp->next = pHead;
     4     pHead = temp;
     5     temp->val = val;
     6     break;
     7 }
     8 else{
     9     temp->next = cur;
    10     pNode->next = temp;
    11     temp->val = val;
    12     break;
    13 }
    View Code

      尾结点

    1 if (pos == count + 1)
    2     pNode->next = newNode(val);            
    3 else if (pos > count + 1){
    4     free(temp);
    5     return false;
    6 }
    View Code

    2 在删除链表的任意位置结点的操作中要把头结点拿出来单独讨论

     1 if (pos == count){
     2     if (pHead == cur){
     3         temp = pHead;
     4         pHead = temp->next;
     5         break;
     6     }
     7     else{
     8         pNode->next = cur->next;
     9         break;
    10     }
    11 
    12 }
    View Code

    3 C++完整代码

      1 #include <vector>                                
      2 #include <algorithm>                
      3 #include <string.h>                 
      4 #include <iostream>   
      5 #include <stack>
      6 
      7 
      8 using namespace std;
      9 
     10 
     11 struct ListNode{
     12     int val;
     13     struct ListNode *next;
     14     ListNode(int x) :
     15         val(x), next(NULL){}
     16 };
     17 
     18 class Solution {
     19 public:
     20     //1. 初始化链表
     21     void InitNode(ListNode* pHead){
     22         pHead = NULL;
     23     }
     24 
     25     //打印链表,递归,
     26     void PrintNode(ListNode* pHead){
     27         if (pHead != NULL){
     28             cout << pHead->val << ' ';
     29             PrintNode(pHead->next);
     30         }
     31     }
     32 
     33     //2. 新建结点
     34     ListNode* newNode(int data){
     35         ListNode* pNode = (ListNode*)malloc(sizeof(ListNode));
     36         if (NULL == pNode)
     37             exit(-1);        
     38         pNode->val = data;
     39         pNode->next = NULL;
     40         return pNode;
     41     }
     42 
     43     //3. 在单链表的头部插入一个节点
     44     void PushFront(ListNode* &pHead, int data){
     45         ListNode* pNew = (ListNode*)malloc(sizeof(ListNode));
     46         pNew->val = data;
     47         pNew->next = NULL;
     48         if (pHead == NULL)
     49             pHead = pNew;
     50         else{
     51             pNew->next = pHead;
     52             pHead = pNew;
     53         }
     54     }
     55 
     56     //4. 在单链表的尾部插入一个节点
     57     void PushBack(ListNode* &pHead, int data){
     58         if (NULL == pHead)
     59             pHead = newNode(data);
     60         else{
     61             ListNode* node = pHead;
     62             while (node->next){
     63                 node = node->next;
     64             }
     65             node->next = newNode(data);
     66         }    
     67     }
     68 
     69     //5. 在任意位置插入数据
     70     bool Insert(ListNode* &pHead, int pos, int val){
     71         int count = 0;    //一定要对其初始化
     72         if (pos <= 0)
     73             return 0;
     74         else if (pHead == NULL){
     75             if (pos == 1){
     76                 ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
     77                 cur->val = val;
     78                 cur->next = NULL;
     79                 pHead = cur;
     80             }
     81             else
     82                 return false;
     83         }
     84         else{
     85             ListNode* cur = pHead;
     86             ListNode* pNode = pHead;
     87             ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
     88             while (cur != NULL){
     89                 ++count;
     90                 if (pos == count){
     91                     if (pHead == cur){
     92                         
     93                         temp->next = pHead;
     94                         pHead = temp;
     95                         temp->val = val;
     96                         break;
     97                     }
     98                     else{
     99                         temp->next = cur;
    100                         pNode->next = temp;
    101                         temp->val = val;
    102                         break;
    103                     }
    104 
    105                 }
    106                 
    107                 pNode = cur;
    108                 cur = cur->next;
    109                 
    110             }
    111             if (pos == count + 1)
    112                 pNode->next = newNode(val);            
    113             else if (pos > count + 1){
    114                 free(temp);
    115                 return false;
    116             }        
    117         }
    118         return true;
    119     }
    120 
    121 
    122     //6. 删除链表的头结点
    123     ListNode* DelFront(ListNode* &pHead){
    124         if (pHead == NULL)
    125             return NULL;
    126         else if (pHead->next == NULL)
    127             pHead = NULL;
    128         else{
    129             ListNode* pNode = pHead;
    130             pHead = pNode->next;
    131         }
    132         return pHead;
    133     }
    134 
    135 
    136     //7. 删除尾结点
    137     ListNode* DelBack(ListNode* &pHead){
    138         ListNode* pre = pHead;
    139         ListNode* cur = pHead;
    140         if (pHead == NULL)
    141             return NULL;
    142         else if (pHead->next == NULL)
    143             pHead = NULL;
    144         while (cur->next){
    145             pre = cur;
    146             cur = cur->next;
    147         }
    148         pre->next = NULL;
    149         return pHead;
    150     }
    151 
    152     //7. 删除链表中的任意位置一个结点
    153     bool Del(ListNode* &pHead, int pos){
    154         int count = 0;    //一定要对其初始化
    155         if (pos <= 0)
    156             return 0;
    157         else if (pHead == NULL){
    158                 return false;
    159         }
    160         else{
    161             ListNode* cur = pHead;
    162             ListNode* pNode = pHead;
    163             ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
    164             while (cur != NULL){
    165                 ++count;
    166                 if (pos == count){
    167                     if (pHead == cur){
    168                         temp = pHead;
    169                         pHead = temp->next;
    170                         break;
    171                     }
    172                     else{
    173                         pNode->next = cur->next;
    174                         break;
    175                     }
    176 
    177                 }
    178 
    179                 pNode = cur;
    180                 cur = cur->next;
    181             }
    182             if (pos > count){
    183                 free(temp);
    184                 return false;
    185             }
    186         }
    187         return true;
    188     }
    189 
    190     ListNode * ReverseList(ListNode* pHead){
    191         ListNode* p = pHead;
    192         ListNode* q = pHead;
    193         stack<int> s;
    194         while (p != NULL){
    195             s.push(p->val);
    196             p = p->next;
    197         }
    198         while (q != NULL){
    199             q->val = s.top();
    200             s.pop();
    201             q = q->next;
    202         }
    203         return pHead;
    204     }
    205 
    206 
    207 };
    208 
    209 void test(){
    210 
    211     //ListNode* data;
    212     Solution a;
    213     ListNode* pHead = NULL;
    214     a.InitNode(pHead);
    215 
    216     //新建结点
    217     cout << "新建结点" << endl;
    218     a.newNode(999);
    219     a.PrintNode(pHead); cout << endl << endl;
    220 
    221     cout << "--------------------头部尾部插入数据创建链表-------------" << endl;
    222     cout << "在单链表的头部插入一个节点" << endl;
    223     a.PushFront(pHead, 100);
    224     a.PushFront(pHead, 101);
    225     a.PushFront(pHead, 102);
    226     a.PushFront(pHead, 103);
    227     a.PrintNode(pHead); cout << endl;
    228 
    229     cout << endl <<"在尾部添加结点" << endl;
    230     a.PushBack(pHead, 2);
    231     a.PushBack(pHead, 5);
    232     a.PushBack(pHead, 3);
    233     a.PushBack(pHead, 6);
    234     a.PushBack(pHead, 1);
    235     a.PrintNode(pHead); cout << endl;
    236     cout << "-------------------头部尾部插入数据创建链表-------------" << endl << endl;
    237 
    238 
    239     cout << endl << "----------------------任意位置插入-----------" << endl;
    240     cout << "" << endl;
    241     a.Insert(pHead, 1, 9999);
    242     a.Insert(pHead, 1, 1111);
    243     a.PrintNode(pHead); cout << endl;
    244 
    245     cout << "" << endl;
    246     a.Insert(pHead, 2, 999);
    247     a.PrintNode(pHead); cout << endl ;
    248 
    249     cout << "" << endl;
    250     a.Insert(pHead, 13, 99);
    251     a.PrintNode(pHead); 
    252     cout << endl << "-----------------------任意位置插入-----------" << endl << endl;
    253 
    254 
    255     cout  << "删除头结点" << endl;
    256     a.DelFront(pHead);
    257     a.PrintNode(pHead); cout << endl;
    258 
    259     cout << "删除尾结点" << endl;
    260     a.DelBack(pHead);
    261     a.PrintNode(pHead); cout << endl ;
    262 
    263     cout  << "删除任意位置的结点" << endl;
    264     a.Del(pHead, 1);
    265     a.PrintNode(pHead); cout << endl;
    266     a.Del(pHead, 10);
    267     a.PrintNode(pHead); cout << endl << endl;
    268 
    269 }
    270 
    271 int main() {
    272 
    273     test();
    274 
    275     system("pause");
    276     return 0;
    277 }
    View Code

    运行结果

    还可以参考https://www.cnblogs.com/chenyingjie1207/p/10036923.html(知识点很多),继续学习

     参考资料

    https://blog.csdn.net/m0_37925202/article/details/78187633

    https://blog.csdn.net/Tina224243/article/details/52840387

    https://www.cnblogs.com/chenyingjie1207/p/10036923.html(知识点很多)

  • 相关阅读:
    光学
    ZYNQ学习笔记2——实例
    ZYNQ学习笔记
    AD使用技巧
    关于浮点运算的一点见解
    解决ccs不能同时导入两个相同工程名的问题
    multisum14 快捷键
    你的进程为什么被OOM Killer杀死了?
    Linux下哪些进程在消耗我们的cache?
    linux 安装python3.7.5
  • 原文地址:https://www.cnblogs.com/wxwhnu/p/11454273.html
Copyright © 2011-2022 走看看