带头结点和不带头结点的单链表的尾插法以及各种操作

一.带头结点的单链表

先说一下create()建立的链表,采用cycle的方式。

  1. /************************************************************* 
  2.  
  3. 带头结点的单链表13-1-1ex3.cpp 
  4.  
  5. 1.建立单链表 
  6.  
  7. 2.求链表长度 
  8.  
  9. 3.在第i个位置插入元素(种方法) 
  10.  
  11. 4.删除指定序号的节点 
  12.  
  13. 由于本程序是带头节点的,所以在删除函数Del()和插入函数InsertList() 
  14.  
  15. 中,head均没有被改变,所以,主函数中,用了Del和InsertList之后 
  16.  
  17. 均没有改变head,在PrintList中,还可以继续把head传入。如果是这样 
  18.  
  19. 的话,这两个函数完全可以定义成void类型,而不是Node *类型的 
  20.  
  21. 只要将Create()定义成Node *就行,再在main()中定义一个指针head 
  22.  
  23. 将head=Create();以后这个head都不会变了。好像带头节点链表的这些 
  24.  
  25. 常规操作均不改变Head节点。 
  26.  
  27. **************************************************************/  
  28.   
  29. #include<stdio.h>  
  30.   
  31. #include<stdlib.h>  
  32.   
  33. //#include<string.h>  
  34.   
  35. //#include<conio.h>  
  36.   
  37.    
  38.   
  39. typedef struct student  
  40.   
  41. {  
  42.   
  43.          int data;  
  44.   
  45.          struct student *next;  
  46.   
  47.          //struct student *pre;  
  48.   
  49. }Node;  
  50.   
  51.    
  52.   
  53.    
  54.   
  55. int GetLength(Node *head)  
  56.   
  57. {  
  58.   
  59.          int n=0;  
  60.   
  61.          Node *p;  
  62.   
  63.          p=head->next;  
  64.   
  65.          while(p!=NULL)  
  66.   
  67.          {  
  68.   
  69.                   p=p->next;  
  70.   
  71.                   n++;  
  72.   
  73.          }  
  74.   
  75.          return n;  
  76.   
  77. }  
  78.   
  79.    
  80.   
  81. Node *InsertList(Node *head, int x, int i) //插入元素方法1  
  82.   
  83. {  
  84.   
  85.          Node *p, *q, *s;  
  86.   
  87.          int j=1;  
  88.   
  89.          p=head;  
  90.   
  91.          if(i<1 || i>GetLength(head)+1)  
  92.   
  93.          {  
  94.   
  95.                   exit(1);  
  96.   
  97.          }  
  98.   
  99.          s=(Node *)malloc(sizeof(Node));  
  100.   
  101.          s->data=x;  
  102.   
  103.          while(j<=i)  
  104.   
  105.          {  
  106.   
  107.                   q=p;  
  108.   
  109.                   p=p->next;  
  110.   
  111.                   j++;  
  112.   
  113.          }  
  114.   
  115.          s->next=q->next;  
  116.   
  117.          q->next=s;  
  118.   
  119.          return head;  
  120.   
  121. }  
  122.   
  123.    
  124.   
  125. void  InsertList2(Node *head, int x, int i) //插入元素方法2  
  126.   
  127. {  
  128.   
  129.          Node *p,*s;  
  130.   
  131.          int j=1;  
  132.   
  133.          p=head;  
  134.   
  135.          if(i<1 || i>GetLength(head)+1) //这块儿比删除节点函数多加了一个,因为可以插入到最后一个节点的后面一个节点。但是删除节点函数不行,只能删除到最后一个节点  
  136.   
  137.          {  
  138.   
  139.                   exit(1);  
  140.   
  141.          }  
  142.   
  143.          s=(Node *)malloc(sizeof(Node));  
  144.   
  145.          s->data=x;  
  146.   
  147.          while(j<i)  
  148.   
  149.          {  
  150.   
  151.                   p=p->next;  
  152.   
  153.                   j++;  
  154.   
  155.          }  
  156.   
  157.          s->next=p->next;  
  158.   
  159.          p->next=s;  
  160.   
  161.          //return head;  
  162.   
  163. }  
  164.   
  165. Node *Del(Node *head, int i)  
  166.   
  167. {  
  168.   
  169.          Node *p, *q;  
  170.   
  171.          int j=1;  
  172.   
  173.          p=head;  
  174.   
  175.          if(i<1 || i>GetLength(head))  
  176.   
  177.          {  
  178.   
  179.                   exit(1);  
  180.   
  181.          }  
  182.   
  183.          while(j<i)  
  184.   
  185.          {  
  186.   
  187.                   p=p->next;  
  188.   
  189.                   j++;  
  190.   
  191.          }  
  192.   
  193.          q=p->next;  
  194.   
  195.          p->next=q->next;  
  196.   
  197.          free(q);  
  198.   
  199.          return head;  
  200.   
  201. }  
  202.   
  203.    
  204.   
  205. Node *create()  
  206.   
  207. {  
  208.   
  209.          Node *head, *p, *s;  
  210.   
  211.          int x, cycle=1;  
  212.   
  213.          head=(Node *)malloc(sizeof(Node));  
  214.   
  215.          p=head; //如果换成带头节点的链表。这里也不能变,切记,不能变成p=head->next;那就错了  
  216.   
  217.          printf("\nplease input the data:\n");  
  218.   
  219.          while(cycle)  
  220.   
  221.          {  
  222.   
  223.                   //printf("\nplease input the data:\n");  
  224.   
  225.                   scanf("%d",&x);  
  226.   
  227.                   if(x!=0)  
  228.   
  229.                   {  
  230.   
  231.                           s=(Node *)malloc(sizeof(Node));  
  232.   
  233.                           s->data=x;  
  234.   
  235.                           //printf("\n %d", s->data);  
  236.   
  237.                           p->next=s;  
  238.   
  239.                           //s->pre=p;  
  240.   
  241.                           p=s;  
  242.   
  243.                   }  
  244.   
  245.                   else  
  246.   
  247.                   {  
  248.   
  249.                           cycle=0;  
  250.   
  251.                   }  
  252.   
  253.          }  
  254.   
  255.          p->next=NULL;  
  256.   
  257.          //head=head->next;//注意,这句是不带头结点的单链表必须做的,因为按照算法。输入的第一个数实际上是赋值给了head->next  
  258.   
  259.          //printf("\nhead->data:%d\n",head->data);  
  260.   
  261.          return head;  
  262.   
  263. }  
  264.   
  265.    
  266.   
  267. void PrintList(Node *head) //打印链表的所有元素  
  268.   
  269. {  
  270.   
  271.          Node *p;  
  272.   
  273.          p=head->next; //1)  
  274.   
  275.          while(p!=NULL)  
  276.   
  277.          {  
  278.   
  279.                   printf("%3d", p->data);  
  280.   
  281.                   p=p->next;  
  282.   
  283.          }  
  284.   
  285.          printf("\n");  
  286.   
  287. }  
  288.   
  289.    
  290.   
  291. int main()  
  292.   
  293. {  
  294.   
  295.          Node *p, *q ,*t, *head;  
  296.   
  297.          head=create();  
  298.   
  299.          //p=p->next;//如果是带头节点的单链表建立,除了少了上面那个函数的末尾的步之外,多了这一步,保证从head的下一个节点输出链表data。  
  300.   
  301.          printf("\nnow output the linklist:\n");  
  302.   
  303.          PrintList(head);  
  304.   
  305.           
  306.   
  307.          InsertList2(head, 99,3);  
  308.   
  309.          //q=q->next;  
  310.   
  311.          printf("\nnow output the new linklist after INSERT:\n");  
  312.   
  313.          PrintList(head);  
  314.   
  315.          /* 
  316.  
  317.          while(q!=NULL) 
  318.  
  319.          { 
  320.  
  321.                   printf("%3d",q->data); 
  322.  
  323.                   q=q->next; 
  324.  
  325.          } 
  326.  
  327.          printf("\n"); 
  328.  
  329.          */  
  330.   
  331.            
  332.   
  333.          Del(head, 3);  
  334.   
  335.          printf("\nnow output the new linklist after DELETE:\n");  
  336.   
  337.          PrintList(head);  
  338.   
  339.          /* 
  340.  
  341.          t=t->next; 
  342.  
  343.          while(t!=NULL); 
  344.  
  345.          { 
  346.  
  347.                   printf("%3d",t->data); 
  348.  
  349.                   t=t->next; 
  350.  
  351.          } 
  352.  
  353.          printf("\n"); 
  354.  
  355.          */  
  356.   
  357.          system("pause");  
  358.   
  359.          return 0;  
  360.   
  361. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值