日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

链表学习【随笔】

發(fā)布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表学习【随笔】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【數(shù)據(jù)結構】書上代碼:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<malloc.h>
  4 typedef struct node
  5 {
  6     int num;
  7     struct node *next;
  8 }NODE;
  9 
 10 NODE *create()
 11 {
 12     NODE *head,*tail,*p;
 13     int num;
 14 
 15     head=tail=NULL;
 16     printf("input until -1\n");
 17     scanf("%d",&num);
 18     while(num!=-1)
 19     {
 20         p = (NODE *)malloc(sizeof(NODE));
 21         if(p == NULL)
 22         {
 23             printf("Malloc failure\n");
 24             return NULL;
 25         }
 26         p->num=num;
 27         p->next=NULL;
 28         if(head == NULL)
 29             head=p;
 30         else
 31             tail->next = p;
 32         tail = p;
 33         scanf("%d",&num);
 34     }
 35     return head;
 36 }
 37 
 38 void printlist(NODE *head)
 39 {
 40     while(head)
 41     {
 42         printf("%2d",head->num);
 43         head=head->next;
 44     }
 45 }
 46 
 47 NODE *insertnode(NODE *head,int num)
 48 {
 49     NODE *p,*q,*next;
 50 
 51     next=(NODE *)malloc(sizeof(NODE));
 52     next->num=num;
 53     next->next=NULL;
 54     if(head == NULL)
 55     {
 56         return next;
 57     }
 58 
 59     p=head;
 60     q=NULL;
 61     while(p)
 62     {
 63         if(p->num < num)
 64         {
 65             q = p;
 66             p = p->next;
 67         }
 68         else
 69         {
 70             if(q)
 71             {
 72                 q->next=next;
 73                 next->next=p;
 74             }
 75             else
 76             {
 77                 next->next=head;
 78                 head=next;
 79             }
 80             break;
 81         }
 82     }
 83     if(!p)
 84         q->next = next;
 85 
 86     return head;
 87 }
 88 int main()
 89 {
 90     while(1)
 91     {
 92         NODE *head;
 93         int num;
 94         head = create();
 95         printlist(head);
 96         printf("\ninput a number\n");
 97         scanf("%d",&num);
 98         insertnode(head,num);
 99         printlist(head);
100         printf("\n");
101     }
102     return 0;
103 }

?

打印的紙上代碼:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<malloc.h>
  4 /*typedef struct node
  5 {
  6     int num;
  7     struct node *next;
  8 }NODE;
  9 
 10 NODE *create()
 11 {
 12     NODE *head,*tail,*p;
 13     int num;
 14 
 15     head=tail=NULL;
 16     printf("input until -1\n");
 17     scanf("%d",&num);
 18     while(num!=-1)
 19     {
 20         p = (NODE *)malloc(sizeof(NODE));
 21         if(p == NULL)
 22         {
 23             printf("Malloc failure\n");
 24             return NULL;
 25         }
 26         p->num=num;
 27         p->next=NULL;
 28         if(head == NULL)
 29             head=p;
 30         else
 31             tail->next = p;
 32         tail = p;
 33         scanf("%d",&num);
 34     }
 35     return head;
 36 }
 37 
 38 void printlist(NODE *head)
 39 {
 40     while(head)
 41     {
 42         printf("%2d",head->num);
 43         head=head->next;
 44     }
 45 }
 46 
 47 NODE *insertnode(NODE *head,int num)
 48 {
 49     NODE *p,*q,*next;
 50 
 51     next=(NODE *)malloc(sizeof(NODE));
 52     next->num=num;
 53     next->next=NULL;
 54     if(head == NULL)
 55     {
 56         return next;
 57     }
 58 
 59     p=head;
 60     q=NULL;
 61     while(p)
 62     {
 63         if(p->num < num)
 64         {
 65             q = p;
 66             p = p->next;
 67         }
 68         else
 69         {
 70             if(q)
 71             {
 72                 q->next=next;
 73                 next->next=p;
 74             }
 75             else
 76             {
 77                 next->next=head;
 78                 head=next;
 79             }
 80             break;
 81         }
 82     }
 83     if(!p)
 84         q->next = next;
 85 
 86     return head;
 87 }
 88 */
 89 
 90 typedef struct node{
 91     int data;
 92     struct node *next;
 93 }NODE;
 94 
 95 NODE *creat(int n){
 96     NODE *head;
 97 
 98     head=(NODE *)malloc(sizeof(NODE));
 99     head->data = n;
100     head->next = NULL;
101 
102     return head;
103 }
104 
105 NODE *back_insert(NODE *head,int n){
106     NODE *p,*q;
107     p = head;
108     q= NULL;
109 
110     while(p->next != NULL){
111         p = p->next;
112     }
113     q = (NODE *)malloc(sizeof(NODE));
114     q->data = n;
115     q->next = NULL;
116 
117     p->next = q;
118 
119     return head;
120 }
121 
122 void print_list(NODE *head){
123     NODE *p = NULL;
124     p = head;
125 
126     while(p!=NULL){
127         printf("%d\n",p->data);
128         p = p->next;
129     }
130 }
131 int main()
132 {
133     NODE *head;
134     head = creat(1);
135     back_insert(head,2);
136     back_insert(head,4);
137     back_insert(head,6);
138     print_list(head);
139     return 0;
140 }

?

轉載于:https://www.cnblogs.com/wushuaiyi/p/3567818.html

總結

以上是生活随笔為你收集整理的链表学习【随笔】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。