zoukankan      html  css  js  c++  java
  • 删除链表节点

    注:所讲述链表不带单独的头结点、

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

    若是头结点需要单独进行操作,其源码如下:

       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:  
  • 相关阅读:
    jvm 垃圾回收
    shell编写一键启动
    jvm内存结构
    java 线程监控
    linux 操作系统级别监控 nmon命令
    linux 操作系统级别监控 vmstat/dstat 命令
    linux 操作系统级别监控 df 命令
    linux 操作系统级别监控 iostat 命令
    linux 操作系统级别监控 free命令
    linux 操作系统级别监控 TOP命令
  • 原文地址:https://www.cnblogs.com/xuddong/p/3071526.html
Copyright © 2011-2022 走看看