用结构体创建链表和用类创建链表

当要保存和管理的数据元素的数目变化很大时,使用链表效率会很高。在链表每个元素都保存在节点:

                  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;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值