刪除鏈表節點

注:所講述鏈表不帶單獨的頭結點、

刪除結點其實是一個很簡單的問題,其關鍵在索要善刪除的結點是第一個結點是,頭結點位的解決。

若是頭結點需要單獨進行操作,其源碼如下:

   1: int del_node(linka** head, int elem) {
   2:     if(head ==NULL || *head ==NULL)
   3:         return -1; //鏈表爲空,刪除失敗
   4:  
   5:     linka *tmp = NULL;
   6:  
   7:     if( (*head) ->data == elem)
   8:     {
   9:         tmp = *head ;
  10:         *head = (*head) ->next;
  11:         delete tmp;        
  12:         return 0;
  13:     }
  14:  
  15:     linka *phead =*head;
  16:     linka *pre=phead;
  17:     linka *cur=phead->next;    
  18:     while(cur)
  19:     {
  20:         if( cur->data == elem)
  21:         {    
  22:             pre ->next= cur->next;
  23:             break;
  24:         }else
  25:         {
  26:             pre= cur;
  27:             cur = cur->next ;
  28:         }        
  29:     }
  30:     if(cur == NULL){
  31:         return -1;
  32:     }else{
  33:         delete cur;
  34:         return 0;
  35:     }
  36: }

刪除鏈表結點算法完整源代碼(VS2010編譯):

   1: // code-summary.cpp : 定義控制檯應用程序的入口點。
   2:  
   3: /**************************************************************************
   4:     * Copyright (c) 2013,  All rights reserved.
   5:     * 文件名稱    : code-summary.cpp
   6:     * 文件標識    :
   7:     * 摘    要    : 鏈表節點刪除 
   8:     * 
   9:     * 當前版本    : Ver 1.0
  10:     * 作者    : 徐鼕鼕 華科
  11:     * 完成日期    : 2013/05/10
  12:     *
  13:     * 取代版本    : 
  14:     * 原作者    :
  15:     * 完成日期    :  
  16:     * 開放版權  : GNU General Public License GPLv3
  17: *************************************************************************/
  18: #include "stdafx.h"
  19:  
  20: #include <iostream>
  21: #include <random>
  22: #include <stack>
  23: using namespace std;
  24: //鏈表節點刪除
  25:  
  26: struct linka {
  27:     int data;
  28:     linka* next;
  29: };
  30: void print(linka *head);
  31: typedef linka link;
  32:  
  33: void init(link **head)
  34: {
  35:     int i= 0;
  36:     link *phead = NULL;
  37:     link *cur = NULL;
  38:     for (i= 0 ;i< 10;i++)
  39:     {
  40:         cur = new link ;
  41:         cur ->data  =rand();
  42:         cur->next = phead;
  43:         phead =cur ;
  44:     }
  45:     *head = phead;
  46:     return ;
  47: }
  48:  
  49: void print(link *head)
  50: {
  51:     int i=0;
  52:     link *cur=head;
  53:     for ( ; cur != NULL ; cur= cur->next)
  54:     {
  55:         cout<< cur ->data <<'\t' ;
  56:     }
  57:     cout<<endl;
  58: }
  59:  
  60: int del_node(linka** head, int elem) {
  61:     if(head ==NULL || *head ==NULL)
  62:         return -1; //鏈表爲空,刪除失敗
  63:  
  64:     linka *tmp = NULL;
  65:  
  66:     if( (*head) ->data == elem)
  67:     {
  68:         tmp = *head ;
  69:         *head = (*head) ->next;
  70:         delete tmp;        
  71:         return 0;
  72:     }
  73:  
  74:     linka *phead =*head;
  75:     linka *pre=phead;
  76:     linka *cur=phead->next;    
  77:     while(cur)
  78:     {
  79:         if( cur->data == elem)
  80:         {    
  81:             pre ->next= cur->next;
  82:             break;
  83:         }else
  84:         {
  85:             pre= cur;
  86:             cur = cur->next ;
  87:         }        
  88:     }
  89:     if(cur == NULL){
  90:         return -1;
  91:     }else{
  92:         delete cur;
  93:         return 0;
  94:     }
  95: }
  96:  
  97:  
  98:  
  99: int _tmain(int argc, _TCHAR* argv[])
 100: {
 101:     linka *head =NULL ;
 102:     init(&head);
 103:     print(head);
 104:     int i= del_node(&head ,6334);
 105:     if (i ==-1)
 106:     {
 107:         cout<<"not found"<<endl;
 108:     }
 109:     print(head);
 110:  
 111:     system("pause");
 112:     return 0;
 113: }
 114:  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章