歡迎您光臨本站 註冊首頁

C語言實現單鏈表的操作:創建,刪除,插入,反轉

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  

剛學了數據結構的單鏈表基本操作:創建,刪除,插入,反轉等,以下是詳細內容,其中很多關於數據結構的表述並不完整,只是簡單的基本演算法思想的表現。

由於沒經驗,代碼有點亂·····

如有不當或錯誤之處,歡迎指正,不勝感激!

  1. #include   
  2. #include    
  3. struct node  
  4. {  
  5.        int data;  
  6.        struct node* next;                
  7. }head;    
  8. struct node *p,*q;//聲明臨時節點    
  9. void head_insert(int x)//從頭部插入新節點    
  10. {  
  11.       
  12.      p=(struct node*)malloc(sizeof(struct node));  
  13.               if(p==NULL)  
  14.               {  
  15.                           printf("內存申請失敗,退出");  
  16.                           exit(0);  
  17.               }   
  18.               p->data=x;  
  19.               p->next=head.next;  
  20.               head.next=p;      
  21. }     
  22. void tail_insert(int x)//從尾部插入節點    
  23. {  
  24.                 
  25.                p=(struct node*)malloc(sizeof(struct node));  
  26.                if(p==NULL)  
  27.               {  
  28.                           printf("內存申請失敗,退出");  
  29.                           exit(0);  
  30.               }              
  31.               p->data=x;  
  32.               p->next=NULL;  
  33.               q->next=p;  
  34.               q=p;  
  35. }   
  36. void node_length()//輸出鏈表長度    
  37. {  
  38.      int length=0;  
  39.      p=head.next;  
  40.       if(p==NULL) printf("The length of node is 0.\n");  
  41.     else   
  42.     {  
  43.     do  
  44.     {  
  45.             length++;  
  46.             p=p->next;                            
  47.     } while(p);  
  48.      printf("The length of node is %d.\n",length);  
  49.     }   
  50. }  
  51. void print_node()//列印鏈表    
  52. {  
  53.      printf("輸出此時鏈表:\n");  
  54.       p=head.next;  
  55.       if(p==NULL) {printf("NULL\n");return;}  
  56.       else  
  57.       {  
  58.       while(p)  
  59.       {  
  60.       printf("%d ",p->data);  
  61.             p=p->next;           
  62.       }  
  63.       printf("\n");    
  64.       }                 
  65.       
  66. }    
  67. void clear_node()//清空鏈表    
  68. {  
  69.      p=head.next;  
  70.      head.next=NULL;  
  71.      while(p)  
  72.      {  
  73.              q=p;  
  74.              p=p->next;  
  75.              free(q);  
  76.      }  
  77. }  
  78. void new_insert(int i,int a)//在第i個位置后插入新整型元素 a   
  79. {  
  80.     q=(struct node*)malloc(sizeof(struct node));  
  81.     q->data=a;  
  82.     p=head.next;  
  83.     if(i<0) printf("Position Error.\n");  
  84.     else  
  85.    {   
  86.     while(p&&--i)  p=p->next;  
  87.     if(i)  printf("Position Error.\n");  
  88.     else  
  89.     {  
  90.     q->next=p->next;  
  91.     p->next=q;  
  92.     }  
  93.    }  
  94. }   
  95. void delete_node(int i)//刪除某節點    
  96. {  
  97.      p=&head;  
  98.      while(p->next&&--i)              
  99.                p=p->next;  
  100.      if(i) printf("Position Error.\n");   
  101.      else  
  102.      {  
  103.          q=p->next;  
  104.          p->next=q->next;  
  105.         free(q);  
  106.      }  
  107. }   
  108. void invert_order()//將鏈表反轉    
  109. {  
  110.      node *This,*prev;  
  111.      p=head.next;  
  112.      This=NULL;  
  113.      while(p)  
  114.      {  
  115.              prev=This;  
  116.              This=p;  
  117.              p=p->next;  
  118.              This->next=prev;  
  119.                
  120.      }  
  121.      head.next=This;  
  122. }  
  123. int main()  
  124. {  
  125.     int number,i,a;    
  126.     head.next=NULL;  
  127.     q=&head;  
  128.     printf("輸入整型數據:\n");  
  129.     while(scanf("%d",&number)!=EOF)  //將數據存入鏈表    
  130.     {  
  131.             head_insert(number);//從頭插入 即逆序插入    
  132.                 
  133.      /*     tail_insert(number); 從尾端 插入即正序插入       */    
  134.     }   
  135.     invert_order();  
  136.     node_length();//輸出鏈表長度    
  137.     print_node();//輸出鏈表   
  138.     printf("在第i個位置后插入a,請輸入i和a:\n");  
  139.     scanf("%d%d",&i,&a);   
  140.     new_insert(i,a);  
  141.     print_node();  
  142.     printf("輸入要刪除的第i個結點:\n");  
  143.     scanf("%d",&i);  
  144.     delete_node(i);  
  145.     print_node();  
  146.     clear_node();  
  147.     return 0;  
  148. }  

附:在Windows下,輸入數據完畢后先按Enter鍵,再按Ctrl+Z鍵,最後按Enter鍵即可結束輸入;在Linux下,輸入完畢后按Ctrl+D鍵可結束輸入。


[火星人 ] C語言實現單鏈表的操作:創建,刪除,插入,反轉已經有499次圍觀

http://coctec.com/docs/program/show-post-71474.html