#include
struct Node
{
int iData;
Node *pstNextNode;
} ;
Node *Head;
Node *This;
Node *New;
//定义函数
void insert_record()
{
New=new Node;
if(Head==NULL)
{
Head=New;
}
else
{
This=Head;
while(This->pstNextNode !=NULL)
{
This=This->pstNextNode;
}
This->pstNextNode=New;
}
This=New;
cout<<" Input your number";
cin>>This->iData;
This->pstNextNode=NULL;
}
void show_record()
{
int i=0;
if(Head==NULL)
{
cout<<"empty list.\n";
// return;
}
This=Head;
do{
cout<<"Record number:"<<++i<<" "<iData< This=This->pstNextNode;
}while(This!=NULL);
}
void del()
{
if(Head==NULL)
{
return ;
}
while(Head->pstNextNode!=NULL)
{
This=Head;
Head=Head->pstNextNode;
delete This;
}
delete Head;
cout<<"all the Nodes has been deleted."< }
void delete_record()
{
Node *p, *pi ;
int num;
cout<<"Input the number you want to delete"< cin>>num;
if(Head==NULL)
{
cout<<"list is empty!"< }
else
{
p=Head;
while(num!=p->iData && p->pstNextNode!=NULL)
{
pi=p;
p=p->pstNextNode;
}
}
if(num==p->iData)
{
if(p==Head)
Head=p->pstNextNode;
else
{
pi->pstNextNode=p->pstNextNode;
cout<<"delete:"<
}
}
else
cout<<"can not found"< }
int main()
{
char cLetter;
int iFlag;
Head=NULL;
while(iFlag)
{
cout<<" 'a' to enter a new record\n";
cout<<" 'd' to delete a record\n";
cout<<" '1' to show all the records\n";
cout<<" others charactor to return:"< cin>>cLetter;
switch(cLetter)
{
case 'a':
insert_record();
break;
case 'd':
delete_record();
break;
case '1':
show_record();
break;
default:
iFlag=0;
}
}
del();
return 0;
}
--------------------next---------------------
阅读(1011) | 评论(0) | 转发(0) |