c++学习笔记—单链表基本操作的实现

用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表)、结点的查找、删除、排序、打印输出、逆置、链表销毁等基本操作。

IDE:vs2013

具体实现代码如下:

[cpp] view plaincopy
 
  1. #include "stdafx.h"  
  2. #include <malloc.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. typedef struct Lnode  
  6. {  
  7.     int data;  
  8.     struct Lnode *next;  
  9. }*node;  
  10. node head_creat()   //头插法建立单链表  
  11. {  
  12.     node head = (struct Lnode *)malloc(sizeof(struct Lnode));  
  13.     head->next = NULL;  
  14.     node p;  
  15.     int temp;  
  16.     while (cin >> temp)  
  17.     {  
  18.         p = (node)malloc(sizeof(struct Lnode));  
  19.         p->data = temp;  
  20.         p->next = head->next;  
  21.         head->next=p;  
  22.     }  
  23.     return head;  
  24. }  
  25. bool search(node h, int target)     //查找某元素是否在链表中  
  26. {  
  27.     int flag = 0;  
  28.     node p = h->next;  
  29.     if (h->next == NULL)  
  30.         return false;  
  31.     for (; p != NULL;)  
  32.     {  
  33.         if (p->data == target)  
  34.         {  
  35.             flag = 1;  
  36.             break;  
  37.         }  
  38.         else  
  39.             p++;  
  40.     }  
  41.     if (flag)  
  42.         return true;  
  43.     else  
  44.         return false;  
  45. }  
  46. node back_creat()   //尾插法建立单链表  
  47. {  
  48.     node head = (struct Lnode *)malloc(sizeof(struct Lnode));  
  49.     head->next = NULL;  
  50.     node p;  
  51.     node r = head;  
  52.     int temp;  
  53.     while (cin >> temp)  
  54.     {  
  55.         p = (node)malloc(sizeof(struct Lnode));  
  56.         p->data = temp;  
  57.         r->next=p;  
  58.         r = p;  
  59.     }  
  60.     r->next = NULL;  
  61.     return head;  
  62. }  
  63. void print(node h)    //打印单链表  
  64. {  
  65.   
  66.     node p = h->next;  
  67.     while (p)  
  68.     {  
  69.         cout << p->data << endl;  
  70.         p = p->next;  
  71.     }  
  72. }  
  73. node delete_node(node h,int del_val)     //删除指定值的节点  
  74. {  
  75.     node p = h->next;  
  76.     node q = h;  
  77.     node r=NULL;  
  78.     while (p)  
  79.     {  
  80.         if (p->data == del_val)  
  81.         {  
  82.             r = p;  
  83.             p = p->next;  
  84.             q->next = p;  
  85.             free(r);  
  86.         }  
  87.         else  
  88.         {  
  89.             q = p;  
  90.             p = p->next;  
  91.         }  
  92.     }  
  93.     return h;  
  94. }  
  95. node sort(node h)     //对链表进行排序(降序)  
  96. {  
  97.     node p=h->next;  
  98.     if (p->next == NULL)  
  99.         return h;  
  100.       
  101.     for (; p != NULL; p = p->next)  
  102.     {  
  103.         for (node q = p->next; q != NULL; q = q->next)  
  104.         {  
  105.             int temp;  
  106.             if (p->data > q->data)  
  107.             {  
  108.                 temp = p->data;  
  109.                 p->data = q->data;  
  110.                 q->data = temp;  
  111.             }  
  112.         }  
  113.     }  
  114.         return h;  
  115. }  
  116. node reverse(node h)  //逆置链表  
  117. {  
  118.     node p, q;  
  119.     p = h->next;  
  120.     h->next = NULL;  
  121.     while (p)  
  122.     {  
  123.         q = p;  
  124.         p = p->next;  
  125.         q->next = h->next;  
  126.         h->next = q;  
  127.     }  
  128.     return h;  
  129. }  
  130. void destroy_List(node head)  //销毁链表  
  131. {  
  132.     if (NULL == head)  
  133.     {  
  134.         return;  
  135.     }  
  136.     if (NULL == head->next)  
  137.     {  
  138.         free(head);  
  139.         head = NULL;  
  140.         return;  
  141.     }  
  142.     node p = head->next;  
  143.     while (NULL != p)  
  144.     {  
  145.         node tmp = p;  
  146.         p = p->next;  
  147.         free(tmp);  
  148.     }  
  149.     free(head);  
  150.     head = NULL;  
  151. }  
  152. int _tmain(int argc, _TCHAR* argv[])  
  153. {  
  154.     cout << "---------------构造链表-------------" << endl;  
  155.     node h = back_creat();  
  156.     cout << "---------------打印链表-------------" << endl;  
  157.     print(h);  
  158.     //cout << "-----------删除指定值后打印链表-----" << endl;  
  159.     //h = delete_node(h, 2);  
  160.     //print(h);  
  161.     cout << "-----------排序后打印链表------------" << endl;  
  162.     h = sort(h);  
  163.     print(h);  
  164.     cout << "-----------逆置后打印链表------------" << endl;  
  165.     h = reverse(h);  
  166.     print(h);  
  167.     destroy_List(h);  
  168.     return 0;  
  169. }  

运行结果:

原文地址:https://www.cnblogs.com/xujian2014/p/4225558.html