单链表建立,插入,删除,查找,遍历操作!!!!

来源:互联网 发布:尼龙粘扣 知乎 编辑:程序博客网 时间:2024/05/22 13:42
[cpp] view plain copy
  1. // Link.cpp : 定义控制台应用程序的入口点。  
  2. //单链表  
  3. #include "stdafx.h"  
  4. #include <iostream>  
  5. #include <complex>  
  6. using namespace std;  
  7.   
  8. typedef struct node {  
  9.     int data;//节点内容  
  10.     node *next;//下一个节点  
  11. }node;  
  12.   
  13. //创建单链表  
  14. node *create(){  
  15.     node *head,*p,*q;  
  16.     int i=0; //单链表中数据的个数  
  17.     int a=-1;  
  18.     head=new node;//创建头节点  
  19.     head->next=NULL;  
  20.     while (1)  
  21.     {  
  22.         cout<<"please input the data(input -1,quit):";  
  23.         cin>>a;  
  24.         if (-1==a) //如果输入的是-1,退出  
  25.         {  
  26.             break;  
  27.         }  
  28.         p=new node;  
  29.         p->data=a;  
  30.         p->next=NULL;//链表的最后一个指针为NULL  
  31.         if (++i==1) //链表只有一个元素  
  32.         {           //连接到head的后面  
  33.             head->next=p;  
  34.         }  
  35.         else{  
  36.             q->next=p;//连接到链表尾端  
  37.         }  
  38.         q=p;//q指向末节点  
  39.     }  
  40.     return head;  
  41. }  
  42.   
  43. //单链表的测长  
  44. int length(node *head){  
  45.     int len=0;  
  46.     node *p=head->next;  
  47.     while (p!=NULL){//遍历链表  
  48.         ++len;  
  49.         p=p->next;  
  50.     }  
  51.     return len;  
  52. }  
  53.   
  54. //打印单链表  
  55. void print(node *head){  
  56.     node *p=head->next;  
  57.     int index=0;  
  58.     if (p==NULL)//链表为NULL  
  59.     {  
  60.         cout<<"Link is empty!"<<endl;  
  61.         getchar();  
  62.         return;  
  63.     }  
  64.     while (p!=NULL)//遍历链表  
  65.     {  
  66.         cout<<"The "<<++index<<"th node is :"<<p->data<<endl;//打印元素  
  67.         p=p->next;  
  68.     }  
  69. }  
  70.   
  71. //查找单链表pos位置的节点,返回节点指针  
  72. //pos 从o开始,0 返回head节点  
  73. node *search_node(node *head,int pos){  
  74. node *p=head->next;  
  75. if (pos<0)//pos 位置不正确  
  76. {  
  77.     cout<<"incorrect position to search node"<<endl;  
  78.     return NULL;  
  79. }  
  80. if (0==pos)//在head位置,返回head  
  81. {  
  82.     return head;  
  83. }  
  84. if (p==NULL)  
  85. {  
  86.     cout<<"Link is empty!"<<endl;  
  87.     return NULL;  
  88. }  
  89. while (--pos)  
  90. {  
  91.     if ((p=p->next)==NULL)  
  92.     {  
  93.         cout<<"incorrect position to search node"<<endl;  
  94.         break//超出链表返回  
  95.     }  
  96. }  
  97. return p;  
  98. }  
  99.   
  100. //单链表节点插入  
  101. //在单链表中某个位置(第pos个节点)后面插入节点,返回链表头指针  
  102. //pos 从0开始计算,0表示插入head节点后面  
  103. node *insert_node(node *head,int pos,int data){  
  104.     node *p=new node;  
  105.     p->data=data;  
  106.     p->next=NULL;  
  107.     node *q=head;  
  108.     if (pos==0)  
  109.     {  
  110.         p->next=q->next;  
  111.         q->next=p;  
  112.     }  
  113.     while (pos)  
  114.     {  
  115.         q=q->next;  
  116.         --pos;  
  117.     }  
  118.     if (q!=NULL)  
  119.     {  
  120.     p->next=q->next;//p指向原pos节点的后一个节点  
  121.     q->next=p; //p插入到pos的后面  
  122.     }  
  123.     return head;  
  124. }  
  125. //删除单链表的pos位置的节点,返回链表头指针  
  126. //pos从1开始计算,1表示删除head后的第一个节点  
  127. node *delete_node(node *head,int pos){  
  128.         node *q=head->next;  
  129.         node *p=NULL;  
  130.     if (q==NULL)  
  131.     {  
  132.         cout<<"Link is empty!"<<endl;  
  133.         return NULL;  
  134.     }  
  135.     while (--pos)  
  136.     {  
  137.         q=q->next;//得到位置pos的节点  
  138.     }  
  139.     if (q!=NULL && q->next!=NULL)  
  140.     {  
  141.         p=q->next;  
  142.         q->next=p->next;  
  143.         delete p;  
  144.     }  
  145.     return head;  
  146. }  
  147. int _tmain(int argc, _TCHAR* argv[])  
  148. {  
  149.     node *head=create();//创建单链表  
  150.     cout<<"Length:"<<length(head)<<endl;//测量单链表长度  
  151.     head=insert_node(head,2,5); //在第2个节点后面插入5  
  152.     cout<<"insert integer 5 after 2th node:"<<endl;  
  153.     print(head); //打印单链表  
  154.     head=delete_node(head,2);//删除第2个节点  
  155.     cout<<"delete the 3th node:"<<endl;  
  156.     print(head);  
  157.     cout<<"search the 3th node:"<<endl;  
  158.     node *p=search_node(head,3); //查找第3个节点  
  159.     if (p!=NULL)  
  160.     {  
  161.         cout<<p->data<<endl;  
  162.     }  
  163.     system("pause");  
  164.     delete [] head;  
  165.     return 0;  
  166. }  


0 0