#include<iostream>
using namespace std;
typedef struct DLNode
{
int data;
struct DLNode *prior;
struct DLNode *next;
}LNode;
struct DLNode *creat(DLNode *&L)
{
DLNode *s,*r;
L=r=s=(DLNode*)malloc(sizeof(DLNode));
while(s->data!=0)
{
r=s;
s=(DLNode*)malloc(sizeof(DLNode));
cin>>s->data;
r->next=s;
s->prior=r;
}
r->next=NULL;
return L;
}
void print(struct DLNode *head)
{
struct DLNode *temp;
temp=head->next;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
DLNode* searchNode(DLNode *C,int x)
{
DLNode *p=C->next;
while(p!=NULL)
{
if(p->data==x)break;
p=p->next;
}
return p;
}
void DeleteNode(DLNode *head,int x)
{
DLNode *p=searchNode(head,x);
if(p==NULL)cout<<"无此节点"<<endl;
else
{
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
cout<<"删除成功"<<endl;
print(head);
}
}
void InsertNode(DLNode *head,int x,int y)
{
DLNode *p=searchNode(head,x);
if(p==NULL)cout<<"无此节点,错误"<<endl;
else
{
DLNode *r=(DLNode*)malloc(sizeof(DLNode));
r->data=y;
r->next=p->next;
r->prior=p;
p->next=r;
cout<<"插入成功,结果为:";
print(head);
}
}
int main()
{
DLNode *a=NULL;
cout<<"请输入有序链表(以零结束):";
a=creat(a);
print(a);
cout<<"请输入要删除的节点";
int x; cin>>x;
DeleteNode(a,x);
cout<<"在指定的节点后面插入节点,输入指定的节点、插入的数";
int y;
cin>>x>>y;
InsertNode(a,x,y);
return 0;
}
posted on 2012-08-21 17:21
yyj 阅读(149)
评论(0) 编辑 收藏 引用