• 链表学习【随笔】


    【数据结构】书上代码:

      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
    ");
     17     scanf("%d",&num);
     18     while(num!=-1)
     19     {
     20         p = (NODE *)malloc(sizeof(NODE));
     21         if(p == NULL)
     22         {
     23             printf("Malloc failure
    ");
     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("
    input a number
    ");
     97         scanf("%d",&num);
     98         insertnode(head,num);
     99         printlist(head);
    100         printf("
    ");
    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
    ");
     17     scanf("%d",&num);
     18     while(num!=-1)
     19     {
     20         p = (NODE *)malloc(sizeof(NODE));
     21         if(p == NULL)
     22         {
     23             printf("Malloc failure
    ");
     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
    ",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 }
  • 相关阅读:
    垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
    自考感悟,话谈备忘录模式
    [每日一题] OCP1z0-047 :2013-07-26 alter table set unused之后各种情况处理
    Java实现 蓝桥杯 算法提高 p1001
    Java实现 蓝桥杯 算法提高 拿糖果
    Java实现 蓝桥杯 算法提高 拿糖果
    Java实现 蓝桥杯 算法提高 求arccos值
    Java实现 蓝桥杯 算法提高 求arccos值
    Java实现 蓝桥杯 算法提高 因式分解
    Java实现 蓝桥杯 算法提高 因式分解
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3567818.html
Copyright © 2020-2023  润新知