Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2351563
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:03:45

#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) |
给主人留下些什么吧!~~