首页 > 新闻动态 >  

新闻动态
NEWS

删除链表节点

添加时间:2013-5-10 点击量:

注:所讲述链表不带零丁的头结点、

删除结点其实是一个很简单的题目,其关键在索要善删除的结点是第一个结点是,头结点位的解决。

若是头结点须要零丁进行操纵,其源码如下:

   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:          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:          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:&#160; 



  26: struct linka {



  27:     int data;



  28:     linka next;



  29: };



  30: void print(linka head);



  31: typedef linka link;



  32:&#160; 



  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:&#160; 



  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:&#160; 



  60: int del_node(linka head, int elem) {



  61:     if(head ==NULL || head ==NULL)



  62:         return -1; //链表为空,删除失败



  63:&#160; 



  64:     linka tmp = NULL;



  65:&#160; 



  66:     if( (head) ->data == elem)



  67:     {



  68:         tmp = head ;



  69:         head = (head) ->next;



  70:          tmp;        



  71:         return 0;



  72:     }



  73:&#160; 



  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:          cur;



  93:         return 0;



  94:     }



  95: }



  96:&#160; 



  97:&#160; 



  98:&#160; 



  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:&#160; 



 111:     system("pause");



 112:     return 0;



 113: }



 114:&#160; 


原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
分享到: