[C] 链表 →→→→→进入此内容的聊天室

来自 , 2020-10-01, 写在 C, 查看 211 次.
URL http://www.code666.cn/view/8db92642
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node{
  4. int data;//数
  5. struct Node *next;//指针
  6. }*Linklist,Node;//node是指向结构体的一个结构体变量linklist指向链表的指针
  7.  
  8. Linklist creat(int n)//建立
  9. {
  10.         Linklist head,r,p;
  11.         int x,i;
  12.         head=(Node*)malloc(sizeof(Node));//开辟空间
  13.         r=head;//r指向头
  14.         printf("输入数字:\n");
  15.         for(i=n;i>0;i--)
  16.         {
  17.                 scanf("%d",&x);
  18.                 p=(Node*)malloc(sizeof(Node));
  19.                 p->data=x;
  20.                 r->next=p;
  21.                 r=p;
  22.         }
  23.         r->next=NULL;
  24.         return head;
  25. }
  26. void output(Linklist head)//输出
  27. {
  28.         Linklist p;
  29.         p=head->next;
  30.         do{
  31.                 printf("%3d",p->data);
  32.                 p=p->next;
  33.         }while(p);
  34.         printf("\n");
  35. }
  36. void find(Linklist head,int x)//寻找
  37. {
  38. int i=0,j=0;
  39. Linklist p;
  40. p=head;
  41. while(p){
  42.         if(p->data==x){
  43.                 printf("元素存在,他在线性表的第%d个位置上\n",i++);
  44.                 p=p->next;
  45.                 j=1;}
  46.         else{
  47.                 p=p->next;
  48.                 i++;}
  49. }
  50. if(j==0)
  51.         printf("查找的元素不存在\n");
  52. }
  53. void insert(Linklist head,int i,int x)//插入
  54. {
  55.         int j=0;
  56.         Linklist p,s;
  57.         p=head;
  58.         while(p&&j<i-1)  
  59.         {
  60.                 p=p->next;
  61.                 j++;
  62.         }
  63.         if(!p||j>i-1)
  64.                 printf("\n指定的插入位置不存在");
  65.         s=(Linklist)malloc(sizeof(Node));
  66.         s->data=x;
  67.         s->next=p->next;
  68.         p->next=s;
  69. }
  70. void delete_ith(Linklist head,int i)//删除按位置
  71. {
  72.         int j=0;
  73.         Linklist p;
  74.         p=head;
  75.         while(p&&j<i-1)  
  76.         {
  77.                 p=p->next;
  78.                 j++;
  79.         }
  80.         if(!p||j>i-1)
  81.                 printf("\n删除的位置不合理");
  82.         p->next=p->next->next;
  83. }
  84. void delete1(Linklist head,int i)//删除按照元素
  85. {
  86.  
  87.         Linklist p;
  88.         p=head;
  89.         while(p->next)  
  90.         {
  91.                 if(p->next->data==i){
  92.                         p->next=p->next->next;
  93.                 }
  94.                 else{
  95.                 p=p->next;
  96.        
  97.                 }
  98.         }
  99. }
  100. void length(Linklist head)//长度
  101. {       int j=0;
  102.         Linklist p;
  103.         p=head->next;
  104.         while(p)
  105.         {
  106.                 p=p->next;
  107.                 j++;
  108.        
  109.         }
  110.                 printf("\n%d",j);
  111. }
  112. void null(Linklist head)//判断是否为空
  113. {       int j=0;
  114.         Linklist p;
  115.         p=head->next;
  116.         while(p)
  117.         {
  118.                 p=p->next;
  119.                 j++;
  120.        
  121.         }
  122.         if(j==0)
  123.                 printf("\n空");
  124.         else
  125.                 printf("\n不为空");         
  126. }
  127. /*void free(Linklist head)
  128. {
  129.         Linklist p;
  130.         p=head;
  131.         while(p)
  132.         {
  133.                 p=p->next;
  134.                 delete head;
  135.                 head=p;
  136.         }
  137. }*/
  138. void printi(Linklist head,int i)//
  139. {
  140.         int j=0;
  141.         Linklist p;
  142.         p=head;
  143.         while(p&&j<i)  //找到第i个元素
  144.         {
  145.                 p=p->next;
  146.                 j++;
  147.         }
  148.         printf("%d",p->data);
  149. }
  150. void main()
  151. {
  152. Linklist head;
  153. int n,x,position,i,j,a;
  154. printf("输入数字的个数n\n");
  155. scanf("%d",&n);
  156. head=creat(n);
  157. printf("输出数字\n");
  158. output(head);
  159. printf("输入你要查的数x:\n");
  160. scanf("%d",&x);
  161. find(head,x);
  162. printf("输入你要插入的数x和他的pos位置\n");
  163. scanf("%d%d",&x,&position);
  164. insert(head,position,x);
  165. output(head);
  166. printf("输入你要删除的第i个元素\n");
  167. scanf("%d",&i);
  168. delete_ith(head,i);
  169. printf("删除后的链表为\n");
  170. output(head);
  171.  
  172. printf("长度为");length(head);printf("\n");
  173. null(head);
  174. //free(head);
  175. output(head);
  176. scanf("%d",&j);
  177. printi(head,j);printf("\n");
  178. output(head);
  179.  
  180. scanf("%d",&a);
  181. delete1(head,a);
  182. output(head);
  183. }
  184.  
  185.  
  186.  
  187.  

回复 "链表"

这儿你可以回复上面这条便签

captcha