用结构体创建链表和用类创建链表
来源:互联网 发布:java set contains 编辑:程序博客网 时间:2024/05/29 16:51
当要保存和管理的数据元素的数目变化很大时,使用链表效率会很高。在链表每个元素都保存在节点:
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=pthis->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;}
0 0
- 用结构体创建链表和用类创建链表
- 用结构体创建静态链表
- 用Entity Framework 来创建MySql数据库和表结构
- 用ADOQuery创建SQL Server数据库,并创建表结构、存储过程和视图
- 用结构创建学生信息表
- 用结构体实现链表的创建、遍历、结点插入、结点删除
- Hibernate自动更新和创建表结构<转>
- 创建基本表结构
- Oracle创建表结构
- mysql创建表结构
- 链表创建 和 倒置
- 创建和删除链表
- 数据结构--数组和广义表--创建以十字链表为存储结构的矩阵
- 用Java创建链表
- ABAP结构体,内表动态创建
- 用new动态创建结构体
- 用结构体实现链表的创建、遍历、结点插入、结点删除、链表删除-----带菜单选项
- Hibernate 自动创建表结构
- Java中的String与常量池
- myeclipse for spring优化
- java之软引用
- android学习布局管理器的一些心得2——基础篇
- 按钮&菜单提示信息
- 用结构体创建链表和用类创建链表
- 配置opencv1
- Automate Your Coding Standard
- 再论拷贝构造函数和拷贝赋值运算符
- noj算法题——二分查找
- Beauty Is in Simplicity
- opencv模拟button
- 自动排班系统2.0(基于网络流实现的排班系统,附详细注解)
- SHELL基础二函数和字符