数据结构基础 - 链表内节点的删除

博客包含C#代码,涉及数据结构与算法相关内容,在信息技术领域,C#常用于后端开发,数据结构和算法是编程基础,能助力高效开发。
由于邮寄名单中的一人已经移民到国外,需要删除把他从名单中删除。
在链表内删除节点又三种不同的情形:
1.删除第一个节点:只需要将head指向第二个节点
2.删除最后一个节点,只需要将指向最后一个节点的指针(引用)指向NULL(null)
3.删除中间节点,只要将需要将指向需要删除节点的指针(引用)指向需要删除节点的下一个节点

C代码:
  1None.gif#include<stdio.h>
  2None.gif
  3None.gifstruct llist
  4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  5InBlock.gif    int num;
  6InBlock.gif    char name[10];
  7InBlock.gif    struct llist *next;
  8ExpandedBlockEnd.gif}
;
  9None.giftypedef struct llist node;
 10None.giftypedef node *llink;
 11None.gif
 12None.gif
 13ExpandedBlockStart.gifContractedBlock.gif/**//*链表的创建*/
 14None.gifllink createllist()
 15ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 16InBlock.gif    llink head;
 17InBlock.gif    llink ptr,ptr1;
 18InBlock.gif    int i;
 19InBlock.gif    
 20InBlock.gif    head = (llink)malloc(sizeof(node));            //分配第一个节点
 21InBlock.gif    if(!head)
 22InBlock.gif        return NULL;    
 23InBlock.gif    printf("请输入六项邮寄数据:\n");
 24InBlock.gif    printf("请输入编号 ==> ");
 25InBlock.gif    scanf("%d",&head->num);
 26InBlock.gif    printf("请输入编号(%d)的姓名 ==> ",head->num);
 27InBlock.gif    scanf("%s",head->name);
 28InBlock.gif    head->next = NULL;
 29InBlock.gif    ptr = head;    
 30InBlock.gif    for(i = 1;i < 6; i++)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 32InBlock.gif        ptr1 = (llink)malloc(sizeof(node));
 33InBlock.gif        if(!ptr1)
 34InBlock.gif            return NULL;
 35InBlock.gif        printf("请输入编号 ==> ");
 36InBlock.gif        scanf("%d",&ptr1->num);
 37InBlock.gif        printf("请输入编号(%d)的姓名 ==> ",ptr1->num);
 38InBlock.gif        scanf("%s",ptr1->name);
 39InBlock.gif        ptr1->next = NULL;
 40InBlock.gif        ptr -> next = ptr1;
 41InBlock.gif        ptr = ptr ->next;
 42ExpandedSubBlockEnd.gif    }

 43InBlock.gif    return head;
 44ExpandedBlockEnd.gif}

 45None.gif
 46ExpandedBlockStart.gifContractedBlock.gif/**//*链表的节点遍历*/
 47None.gifllink findnode(llink head,int num)
 48ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 49InBlock.gif    llink ptr;
 50InBlock.gif    
 51InBlock.gif    ptr = head;
 52InBlock.gif    while(ptr != NULL)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif        if(ptr->num == num)
 55InBlock.gif            return ptr;
 56InBlock.gif        ptr = ptr->next;
 57ExpandedSubBlockEnd.gif    }

 58InBlock.gif    return ptr;
 59ExpandedBlockEnd.gif}

 60None.gif
 61ExpandedBlockStart.gifContractedBlock.gif/**//*链表的输出*/
 62None.gifvoid printllist(llink ptr)
 63ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 64InBlock.gif    while(ptr != NULL)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 66InBlock.gif        printf("Number:[%d]\n",ptr->num);
 67InBlock.gif        printf("Name:%s",ptr->name);
 68InBlock.gif        ptr = ptr->next;
 69InBlock.gif        printf("\n");
 70ExpandedSubBlockEnd.gif    }

 71ExpandedBlockEnd.gif}

 72None.gif
 73ExpandedBlockStart.gifContractedBlock.gif/**//*指定节点的删除*/
 74None.gifllink deletenode(llink head,llink ptr)
 75ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 76InBlock.gif    llink previous;
 77InBlock.gif    
 78InBlock.gif    if(ptr == head)                        //如果是第一个节点
 79InBlock.gif        return head->next;
 80InBlock.gif    else
 81ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 82InBlock.gif        previous = head;
 83InBlock.gif        while(previous -> next != ptr)        //找到需要查找节点的前一个节点
 84InBlock.gif            previous = previous->next;
 85InBlock.gif            
 86InBlock.gif        if(ptr->next == NULL)            //链表是否结束
 87InBlock.gif            //删除最后一个结点
 88InBlock.gif            previous->next = NULL;
 89InBlock.gif        else
 90InBlock.gif            //删除中间一个节点
 91InBlock.gif            previous->next = ptr->next;
 92ExpandedSubBlockEnd.gif    }

 93InBlock.gif    return head;
 94ExpandedBlockEnd.gif}

 95None.gif
 96None.gif
 97None.gifvoid main()
 98ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 99InBlock.gif    llink head;
100InBlock.gif    llink ptr;
101InBlock.gif    int num;
102InBlock.gif    
103InBlock.gif    head = createllist();
104InBlock.gif    if(!head)
105ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
106InBlock.gif        printf("内存分配失败!\n");
107InBlock.gif        exit(1);
108ExpandedSubBlockEnd.gif    }

109InBlock.gif    printf("原来的链表:\n");
110InBlock.gif    printllist(head);
111InBlock.gif    while(1)
112ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
113InBlock.gif        printf("请输入要删除的邮寄编号 ==> ");
114InBlock.gif        scanf("%d",&num);
115InBlock.gif        if(num != -1)
116ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
117InBlock.gif            ptr = findnode(head,num);
118InBlock.gif            if(!ptr)
119InBlock.gif                printf("没有找到\n");
120InBlock.gif            else
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122InBlock.gif                head = deletenode(head,ptr);
123InBlock.gif                printf("删除后的链表:\n");
124InBlock.gif                printllist(head);
125ExpandedSubBlockEnd.gif            }

126ExpandedSubBlockEnd.gif        }

127InBlock.gif        else
128InBlock.gif            exit(1);
129ExpandedSubBlockEnd.gif    }

130ExpandedBlockEnd.gif}


C#代码:

  1None.gifusing System;
  2None.gif
  3None.gifclass Node
  4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  5InBlock.gif    private int num;
  6InBlock.gif    
  7InBlock.gif    public int Num
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9ExpandedSubBlockStart.gifContractedSubBlock.gif        setdot.gif{num = value;}
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        getdot.gif{return num;}
 11ExpandedSubBlockEnd.gif    }

 12InBlock.gif    
 13InBlock.gif    private string name;
 14InBlock.gif    
 15InBlock.gif    public string Name
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        setdot.gif{name = value;}
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        getdot.gif{return name;}
 19ExpandedSubBlockEnd.gif    }

 20InBlock.gif    
 21InBlock.gif    private Node next;
 22InBlock.gif    
 23InBlock.gif    public Node Next
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        setdot.gif{next = value;}
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        getdot.gif{return next;}
 27ExpandedSubBlockEnd.gif    }

 28InBlock.gif    
 29InBlock.gif    public void Print()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 31InBlock.gif        Console.WriteLine("编号:[{0}]",num);
 32InBlock.gif        Console.WriteLine("姓名:{0}",name);
 33ExpandedSubBlockEnd.gif    }
 
 34ExpandedBlockEnd.gif}

 35None.gif
 36None.gifclass Llist
 37ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 38InBlock.gif    //链表的创建
 39InBlock.gif    public static Node CreateLlist()
 40ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 41InBlock.gif        Node head;
 42InBlock.gif        Node tempNode,tempNode1;
 43InBlock.gif        int i;
 44InBlock.gif        
 45InBlock.gif      head = new Node();
 46InBlock.gif      Console.WriteLine("请输入六项邮寄数据:\n");
 47InBlock.gif      Console.WriteLine("请输入编号==>");
 48InBlock.gif      head.Num = Convert.ToInt32(Console.ReadLine());
 49InBlock.gif      Console.WriteLine("请输入编号({0})的姓名 ==>",head.Num);
 50InBlock.gif      head.Name = Console.ReadLine();
 51InBlock.gif      head.Next = null;
 52InBlock.gif      tempNode = head;
 53InBlock.gif      for( i = 1 ;i < 6; i++)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
 55InBlock.gif          tempNode1 = new Node();
 56InBlock.gif          Console.WriteLine("请输入编号 ==>");
 57InBlock.gif          tempNode1.Num = Convert.ToInt32(Console.ReadLine());
 58InBlock.gif          Console.WriteLine("请输入编号({0})的姓名",tempNode1.Num);
 59InBlock.gif          tempNode1.Name = Console.ReadLine();
 60InBlock.gif          tempNode1.Next = null;
 61InBlock.gif          tempNode.Next = tempNode1;
 62InBlock.gif          tempNode = tempNode.Next;
 63ExpandedSubBlockEnd.gif      }

 64InBlock.gif      return head;
 65ExpandedSubBlockEnd.gif    }

 66InBlock.gif    
 67InBlock.gif    //链表的节点遍历
 68InBlock.gif    public static Node FindNode(Node head,int num)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 70InBlock.gif        Node tempNode;
 71InBlock.gif        
 72InBlock.gif        tempNode = head;
 73InBlock.gif        while(tempNode != null)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            if(tempNode.Num == num)
 76InBlock.gif                return tempNode;
 77InBlock.gif            tempNode = tempNode.Next;
 78ExpandedSubBlockEnd.gif        }

 79InBlock.gif        return tempNode;
 80ExpandedSubBlockEnd.gif    }

 81InBlock.gif    
 82InBlock.gif    //链表的输出
 83InBlock.gif    public static void PrintLlist(Node tempNode)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 85InBlock.gif        while( tempNode != null)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87InBlock.gif            tempNode.Print();
 88InBlock.gif            tempNode = tempNode.Next;
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif        Console.WriteLine();
 91ExpandedSubBlockEnd.gif    }

 92InBlock.gif    
 93InBlock.gif    //链表内指定节点的删除
 94InBlock.gif    public static Node DeleteNode(Node head,Node tempNode)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif        Node previous;
 97InBlock.gif        
 98InBlock.gif        if(tempNode == head)            //如果是第一个节点
 99InBlock.gif            return head.Next;
100InBlock.gif        else
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102InBlock.gif            previous = head;
103InBlock.gif            while(previous.Next != tempNode)        //找到需要查找节点的前一个节点
104InBlock.gif                previous = previous.Next;
105InBlock.gif                
106InBlock.gif            if(tempNode.Next == null)        //链表是否结束
107InBlock.gif                previous.Next = null;            //删除最后一个结点
108InBlock.gif            else
109InBlock.gif                previous.Next = tempNode.Next;        //删除中间一个节点
110ExpandedSubBlockEnd.gif        }

111InBlock.gif        return head;    
112ExpandedSubBlockEnd.gif    }

113InBlock.gif    
114ExpandedBlockEnd.gif}

115None.gif
116None.gifclass Test
117ExpandedBlockStart.gifContractedBlock.gifdot.gif{
118InBlock.gif    public static void Main()
119ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
120InBlock.gif        Node head;
121InBlock.gif        Node tempNode;
122InBlock.gif        int num;
123InBlock.gif        
124InBlock.gif        head = Llist.CreateLlist();
125InBlock.gif        Console.WriteLine("原来的链表:");
126InBlock.gif        Llist.PrintLlist(head);
127InBlock.gif        while(true)
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129InBlock.gif            Console.WriteLine("请输入要删除的邮寄编号 ==>");
130InBlock.gif            num = Convert.ToInt32(Console.ReadLine());
131InBlock.gif            if(num != -1)
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                tempNode = Llist.FindNode(head,num);
134InBlock.gif                if(tempNode == null)
135InBlock.gif                    Console.WriteLine("没有找到\n");
136InBlock.gif                else
137ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
138InBlock.gif                    head = Llist.DeleteNode(head,tempNode);
139InBlock.gif                    Console.WriteLine("删除后的链表:");
140InBlock.gif                    Llist.PrintLlist(head);
141ExpandedSubBlockEnd.gif                }

142ExpandedSubBlockEnd.gif            }

143InBlock.gif            else
144InBlock.gif                break;
145ExpandedSubBlockEnd.gif        }

146ExpandedSubBlockEnd.gif    }

147ExpandedBlockEnd.gif}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值