posts - 0,comments - 0,trackbacks - 0
#include<iostream>
using namespace std;
typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode;
struct LNode *creat(LNode *head)
{
    struct LNode *p1,*p2;
    p1=(LNode*)malloc(sizeof(LNode));
    if (head==NULL) head=p1;
    while(p1->data!=0)
    {
        p2=p1;
        p1=(LNode*)malloc(sizeof(LNode));
        cin>>p1->data;
        p2->next=p1;
    }
    p2->next=NULL;
    return head;
}

void print(struct LNode *head)
{
    struct LNode *temp;
    temp=head->next;
    while(temp!=NULL)
    {
        cout<<temp->data<<"  ";
        temp=temp->next;
    }
    cout<<endl;
}
void SearchAndDelete(LNode *head,int x)
{
    LNode *p,*q=head;
    while(q->next!=NULL&&q->next->data!=x)
    {
        q=q->next;
    }
    if(q->next==NULL)cout<<"查无此数字"<<endl;
    else
    {
        p=q->next;
        q->next=p->next;
        free(p);
        cout<<"已经删除此数字"<<endl;
        print(head);
    }
}
int main()
{
    LNode *a=NULL;
    cout<<"请输入有序链表(以零结束):";
    a=creat(a);
    print(a);    
    cout<<"查找到某一值是否在链表中,在则删除"<<endl;
    int x;
    cin>>x;
    SearchAndDelete(a,x);
    

    return 0;
}
posted on 2012-08-21 16:12 yyj 阅读(186) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理