当要保存和管理的数据元素的数目变化很大时,使用链表效率会很高。在链表每个元素都保存在节点:
node 1 node 2 node n
head--->元素1 -->元素2..........元素n<----tail
next next NULL
用结构体实现:
#include"15.h"
struct stu{
char name[22];
int age;
stu *next;};
int main(){
char name[22];
int age;
stu c={"ccc",20,NULL};
stu c1={"bbb",21,&c};
stu b={"bbb",24,&c1};
stu a={"aaa",27,&b};
stu *p,*head=&a,*before=NULL;//链表的创建
int f=0,k=0;
p=head;
cout<<"the inf of stu:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}
cout<<endl;
p=head;
cout<<"enter the name you want:"<<endl;
cin>>name;
while(p!=NULL){
f=0;
if(strcmp(name,p->name)==0) {f=1;k++;}
if(f==1)cout<<" the name:"<<p->name<<" the age:"<<p->age<<endl;
p=p->next;}//结构链表的查询
if(k==0) cout<<"no use!"<<endl;
cout<<endl;
p=head;
cout<<"enter enter the age of add,and it will added by age:"<<endl;
cin>>age;
stu add={"add",age,NULL};
while(p!=NULL){
if(age>p->age) break;
before=p;
p=p->next;}
if(before==NULL) {add.next=head;head=&add;}//最大,在表头插入
else {add.next=before->next;before->next=&add;}//在表内部插入
p=head;
cout<<"the inf of stu after adding:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}
cout<<endl;
p=head;
before=NULL;
cout<<"enter the name you delete:"<<endl;
cin>>name;
while(p!=NULL){
f=0,k=0;
if(strcmp(name,p->name)==0) {f=1;k++;break;}
before=p;
p=p->next;}
if(f==1)//含有该数据进行删除
{
if(before==NULL){head=p->next;}//在表头删除
else {before->next=p->next;}//在表中删除
}
if(k==0) {cout<<"no use!"<<endl;
p=head;
cout<<"the inf of stu after no deleted:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}}
else{
p=head;
cout<<"the inf of stu after deleted:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}}
cout<<endl;
p=head;
before=NULL;
system("pause");
return 0;}
在结构体实现的链表里,实现了查找,插入,删除等常用操作。
而类实现:
class stu{
public:
char name[22];
int age;
stu *next;
//改成可以访问
stu(){next=NULL;}
stu(char *p,int age,stu *add){strcpy(name,p);//注意不要name=p
this->age=age;next=add;}
};
int main(){
char name[22];
int age;
stu *head,*before=NULL,*p;
stu c("ccc",20,NULL);
stu c1("bbb",21,&c);
stu b("bbb",24,&c1);
stu a("aaa",27,&b);
head=&a;
p=head;
int f=0,k=0;
cout<<"the inf of stu:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}
cout<<endl;
p=head;
cout<<"enter the name you want:"<<endl;
cin>>name;
while(p!=NULL){
f=0;
if(strcmp(name,p->name)==0){f=1;k++;}
if(f==1)cout<<" the name:"<<p->name<<" the age:"<<p->age<<endl;
p=p->next;}//结构链表的查询
if(k==0) cout<<"no use!"<<endl;
cout<<endl;
p=head;
cout<<"enter enter the age of add,and it will added by age:"<<endl;
cin>>age;
stu add("add",age,NULL);
while(p!=NULL){
if(age>p->age) break;
before=p;
p=p->next;}
if(before==NULL) {add.next=head;head=&add;}//最大,在表头插入
else {add.next=before->next;before->next=&add;}//在表内部插入
p=head;
cout<<"the inf of stu after adding:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}
cout<<endl;
p=head;
before=NULL;
cout<<"enter the name you delete:"<<endl;
cin>>name;
while(p!=NULL){
f=0,k=0;
if(strcmp(name,p->name)==0) {f=1;k++;break;}
before=p;
p=p->next;}
if(f==1)//含有该数据进行删除
{
if(before==NULL){head=p->next;}//在表头删除
else {before->next=p->next;}//在表中删除
}
if(k==0) {cout<<"no use!"<<endl;
p=head;
cout<<"the inf of stu after no deleted:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}}
else{
p=head;
cout<<"the inf of stu after deleted:"<<endl;
while(p!=NULL){
cout<<"the name:"<<p->name<<" the age: "<<p->age<<" the next:"<<endl;
p=p->next;}}
cout<<endl;
p=head;
before=NULL;
system("pause");
return 0;}