【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)

来源:互联网 发布:国产数据库排名 编辑:程序博客网 时间:2024/06/10 22:05

http://blog.csdn.net/hanjing_1995/article/details/51539563

[cpp] view plain copy
  1. #define _CRT_SECURE_NO_WARNINGS 1  
  2. #include<iostream>  
  3. using namespace std;  
  4.   
  5.   
  6. //单链表的实现  
  7. #include<assert.h>  
  8.   
  9.   
  10. typedef int DataType;  
  11.   
  12. typedef struct SListNode  
  13. {  
  14.     DataType _data;  
  15.     struct SListNode* _next;  
  16. }SListNode;  
  17.   
  18.   
  19. SListNode* _CreateNode(DataType x)  
  20. {  
  21.     SListNode* head = (SListNode*)malloc(sizeof(SListNode));  
  22.     head->_data = x;  
  23.     head->_next = NULL;  
  24.     return head;  
  25. }  
  26.   
  27.   
  28. void PushBack(SListNode*& head, DataType x)  
  29. {  
  30.     if (head == NULL)  
  31.     {  
  32.         head = _CreateNode(x);  
  33.         head->_next = NULL;  
  34.     }  
  35.     else  
  36.     {  
  37.         SListNode* cur = head;  
  38.         while (cur->_next != NULL)  
  39.         {  
  40.             cur = cur->_next;  
  41.         }  
  42.         cur->_next = _CreateNode(x);  
  43.     }  
  44.       
  45. }  
  46.   
  47.   
  48. void PopBack(SListNode*& head)  
  49. {  
  50.     if (head == NULL)  
  51.     {  
  52.         return;  
  53.     }   
  54.     else if (head->_next == NULL)  
  55.     {  
  56.         free(head);  
  57.         head = NULL;  
  58.     }  
  59.     else  
  60.     {  
  61.         SListNode* cur = head;  
  62.         SListNode* next = head;  
  63.   
  64.         while (cur)  
  65.         {  
  66.             next = cur->_next;  
  67.             if (next != NULL && next->_next == NULL)  
  68.             {  
  69.                 free(next);  
  70.                 cur->_next = NULL;  
  71.                 return;  
  72.             }  
  73.   
  74.             cur = cur->_next;  
  75.         }  
  76.     }  
  77.       
  78. }  
  79.   
  80.   
  81. void PushFront(SListNode*& head,DataType x)  
  82. {  
  83.     if (head == NULL)  
  84.     {  
  85.         head = _CreateNode(x);  
  86.     }  
  87.     else  
  88.     {  
  89.         SListNode* pcur = _CreateNode(x);  
  90.         pcur->_next = head;  
  91.         head = pcur;  
  92.     }  
  93. }  
  94.   
  95.   
  96. void PopFront(SListNode*& head)  
  97. {  
  98.     if (head == NULL)  
  99.     {  
  100.         return;  
  101.     }  
  102.     else if (head->_next == NULL)  
  103.     {  
  104.         free(head);  
  105.         head = NULL;  
  106.     }  
  107.     else  
  108.     {  
  109.         SListNode* del = head;  
  110.         SListNode* next = head->_next;  
  111.         free(del);  
  112.         del = NULL;  
  113.         head = next;  
  114.     }  
  115. }  
  116.   
  117.   
  118. void Insert(SListNode* head,int pos,DataType x)  
  119. {  
  120.     assert(pos >= 0);  
  121.     SListNode* cur = head;  
  122.     while (--pos && cur)  
  123.     {              
  124.         cur = cur->_next;          
  125.     }  
  126.     if (pos > 0)  
  127.     {  
  128.         printf("pos位置大于链表长度!\n");  
  129.         return;  
  130.     }  
  131.     SListNode* newcur = _CreateNode(x);  
  132.     if (cur->_next)  
  133.     {  
  134.         SListNode* next = cur->_next;  
  135.       
  136.         cur->_next = newcur;  
  137.         newcur->_next = next;  
  138.     }  
  139.     else if (cur->_next == NULL)  
  140.     {  
  141.         cur->_next = newcur;  
  142.     }  
  143. }  
  144.   
  145.   
  146. size_t Length(SListNode*& head)  
  147. {  
  148.     size_t count = 0;  
  149.     SListNode* cur = head;  
  150.     while (cur)  
  151.     {  
  152.         count++;  
  153.         cur = cur->_next;  
  154.     }  
  155.     return count;  
  156. }  
  157.   
  158.   
  159. void PrintSList(SListNode*& head)  
  160. {  
  161.     if (head == NULL)  
  162.     {  
  163.         return;  
  164.     }  
  165.     SListNode* cur = head;  
  166.     while (cur)  
  167.     {  
  168.         printf("%d->", cur->_data);  
  169.         cur = cur->_next;  
  170.     }  
  171.     printf("\n");  
  172. }  
  173.   
  174.   
  175. void Test()  
  176. {  
  177.     SListNode* sList =NULL;  
  178.     PushBack(sList, 1);  
  179.     PushBack(sList, 2);  
  180.     PushBack(sList, 3);  
  181.     PushBack(sList, 4);  
  182.     PushBack(sList, 5);  
  183.     PrintSList(sList);  
  184.   
  185.     PopBack(sList);  
  186.     PrintSList(sList);  
  187.   
  188.     PushFront(sList, 0);  
  189.     PrintSList(sList);  
  190.   
  191.     PopFront(sList);  
  192.     PrintSList(sList);  
  193.   
  194.     Insert(sList, 3, 10);  
  195.     PrintSList(sList);  
  196.   
  197.     int ret = Length(sList);  
  198.     printf("单链表长度为:%d\n", ret);  
  199. }  
  200.   
  201.   
  202. int main()  
  203. {  
  204.     Test();  
  205.     system("pause");  
  206.     return 0;  
  207. }  

阅读全文
0 0
原创粉丝点击