首页 > 其他 > 详细

单向链表删除某结点

时间:2014-04-13 12:36:47      阅读:566      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include<iostream>  
using namespace std;  
typedef struct node  
{  
    int number;  
    struct node *next;  
}Node;  
Node *Delete(Node *head,int key)  
{  
    Node *node1=head;  
    Node *node2=NULL;  
    if (head==NULL)  
    {  
        return NULL;  
    }   
    else  
    {  
        if (node1->number==key)  
        {  
            head=head->next;  
            free(node1);  
            return head;  
        }   
        else  
        {  
            while (node1!=NULL)  
            {  
                node2=node1;  
                node2=node2->next;  
                if (node2->number==key)  
                {  
                    node1->next=node2->next;  
                    free(node2);  
                    break;  
                }  
                node1=node1->next;  
            }  
            return head;  
        }  
    }  
}  
int main()  
{  
    Node *head=(Node*)malloc(sizeof(Node));  
    Node *p,*q,*q1;  
    int key;  
    p=(Node*)malloc(sizeof(Node));  
    q1=q=head;  
    int i;  
    for (i=1;i<10;i++)  
    {  
        p->number=i;  
        head->next=p;  
        head=p;  
        p=(Node*)malloc(sizeof(Node));  
    }  
    head->next=NULL;  
    cout<<"原链表数据: "<<endl;  
    q1=q1->next;  
    while (q1!=NULL)  
    {  
        cout<<q1->number<<" ";  
        q1=q1->next;  
    }  
    cout<<endl;  
    cout<<"输入要删除的数据:";  
    cin>>key;  
    p=Delete(q->next,key);  
    cout<<"删除一个"<<key<<"之后的链表数据: "<<endl;  
    while (p!=NULL)  
    {  
        cout<<p->number<<" ";  
        p=p->next;  
    }  
    cout<<endl;  
    free(p);  
    free(head);  
    return 0;  
}  
bubuko.com,布布扣

 

单向链表删除某结点,布布扣,bubuko.com

单向链表删除某结点

原文:http://www.cnblogs.com/tongusir/p/3660267.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!