#include "iostream.h"
#include "string.h"
typedef struct
{
char name[20];
int num;
}Student;
typedef struct DoubleNode
{
Student data;
DoubleNode *plink;
DoubleNode *nlink;
}DoubleNode;
typedef struct DoubleChainList
{
Student data;
DoubleNode *first;
}DoubleChainList;
int insert(DoubleChainList *L,int k,Student &x)
{
if(k<0) return 0;
int index=1;
DoubleNode*current=L->first;
while(index<k && current)
{
index++;
current=current->nlink;
}
if(k>0&&!current)return 0;
DoubleNode*q=new DoubleNode;
q->data=x;
if(k)
{
q->nlink=current->nlink;
q->plink=current;
DoubleNode*p=current->nlink;
p->plink=q;
current->nlink=q;
}
else
{
q->nlink=L->first;
q->plink=NULL;
L->first=q;
DoubleNode* p=L->first;
p->plink=q;
}
return 1;
}
void Output(DoubleChainList *L)
{
DoubleNode* x=L->first;
int j=1;
while(x!=NULL)
{
cout<<x->data.name<<" "<<'\t';
cout<<x->data.num<<" "<<'\t';
cout<<endl;
j++;
x=x->nlink;
}
}
int DeleteF(DoubleChainList *L,char s[20])
{
DoubleNode *p=L->first;
char m[20];
strcpy(m,p->data.name);
int h=strcmp(m,s);
while(h)
{
p=p->nlink;
strcpy(m,p->data.name);
h=strcmp(m,s);
}
DoubleNode* current=p;
if(!p->nlink)
{
DoubleNode*q=p->plink;
q->nlink=NULL;
}
if(!p->plink)
{
p=current->nlink;
L->first=p;
p->plink=NULL;
}
else
{
DoubleNode*d=p->nlink;
d->plink=p->plink;
d=p->plink;
d->nlink=p->nlink;
}
delete current;
return 1;
}
int DeleteS(DoubleChainList *L,int k)
{
if(k<1||!L->first)
return 0;
DoubleNode* current=L->first;
DoubleNode* p;
if(k==1)
{
p=current->nlink;
p->plink=NULL;
L->first=p;
}
else
{
DoubleNode *q=L->first;
for(int index=1;index<k && q;index++)
q=q->nlink;
if(!q)return 0;
current=q;
q=current->plink;
p=current->nlink;
q->nlink=p;
p->plink=q;
}
delete current;
return 1;
}
void Information_Producer() //the information of the producer
{
cout<<" ********************************************************* "<<endl;
cout<<" The Algorithm of Doublechainlist "<<endl;
cout<<" Producer : Liao Jiaping "<<endl;
cout<<" Class : IM & IS 0904 "<<endl;
cout<<" Number : 0909030406 "<<endl;
cout<<" Teacher : Wang Shaobo "<<endl;
cout<<" ********************************************************* "<<endl;
}
void Menu()
{
cout<<" ********************************************************* "<<endl;
cout<<" * do your choice please: * "<<endl;
cout<<" * 1.diaplay the linearlist. * "<<endl;
cout<<" * 2.insert the data element . * "<<endl;
cout<<" * 3.delete the data element(by key words) * "<<endl;
cout<<" * 4.delete the data element(by position) . * "<<endl;
cout<<" * 0.exit. * "<<endl;
cout<<" ********************************************************* "<<endl;
}
int main()
{
Information_Producer();
Student student[]={{"LEBRON",23},{"NASH",13},{"KOBY",24},{"DUNCAN",21},{"PUAL",3},
{"CURRY",50},{"YAO",11},{"ALLEN",25},{"ROY",1},{"YI",9}};
DoubleNode *A,*B;
A=new DoubleNode;
A->data=student[0];
A->plink=NULL;
DoubleChainList *L=new DoubleChainList;
Student J={"LiaoJiaping",17};
L->data=J;
L->first=A;
for(int i=1;i<10;i++)
{
B=new DoubleNode;
B->data=student[i];
B->nlink=NULL;
B->plink=A;
A->nlink=B;
A=B;
}
Menu();
start:
cout<<"------------Please Do Your Choice(input a number)---------- :"<<endl;
int C;
cin>>C;
switch (C)
{
case 1:
cout<<"the doublechainlist is:"<<endl;
Output(L);
break;
case 2:
int k;
Student a;
cout<<"please input the information(including the name and the number) of the student :"<<endl;
cin>>a.name>>a.num;
cout<<"please input the position that you want to insert the new student:";
cin>>k;
insert(L,k,a);
cout<<"now, the new doublechainnode is :"<<endl;
Output(L);
cout<<endl;
break;
case 3:
char n[20];
cout<<"please input the name of the student that you want to delete:"<<endl;
cin>>n;
DeleteF(L,n);
Output(L);
break;
case 4:
cout<<"please input the position that you want to delete the information : ";
cin>>k;
DeleteS(L,k);
Output(L);
break;
case 0:
return 0;
break;
}
goto start;
return 0;
}
【大二上的数据结构实验。。。竟然自己用了goto语句那时。。。不推荐用的这个。。。】