C++链表学习
来源:互联网 发布:js数组中添加对象 编辑:程序博客网 时间:2024/06/08 08:37
链表在一定程度上弥补了数组的不足,高效的利用了存储空间。是结构体与指针的结合。
简单链表示例
//简单链表示例#include <iostream>using namespace std;struct student{//建立一个用于创建链表的结构体 int id; char name[20]; float score; student *next;//链表结构体关键};void main(){ student *head;//创建一个结构体指针用作链表的链表头。 //根据需要创建结构体成员。 student stu1={1,"xiaoming",59.5,NULL},stu2={2,"angelababy",61,NULL},stu3={3,"xiaohong",99,NULL};//建立链表 head = &stu1;//用指针将结构体串起来 stu1.next= &stu2; stu2.next= &stu3; stu3.next= NULL;}
创建链表的函数
#include <iostream>#include <string>#include <sstream>using namespace std;struct student{ int id; char name[20]; float score; student *next;};//此函数的作用是自动创建链表,第一输入的是首节点,最后一个是首节点。student * createList(){ int n; student *pstu; student *pre=NULL; cout<<"请输入节点个数:"<<endl; cin>>n; while(n) { pstu = new student; cout<<"请输入学号 姓名 成绩"<<endl; cin>>pstu->id>>pstu->name>>pstu->score; pstu->next=pre; pre=pstu;//指针赋值传的是地址 n--; } return pstu;}void main(){}
输出链表的数据
#include <iostream>using namespace std;struct student{ int id; char name[20]; float score; student *next;};//此函数的作用是自动创建链表,第一输入的是首节点,最后一个是首节点。student * createList(){ int n; student *pstu; student *pre=NULL; cout<<"请输入节点个数:"<<endl; cin>>n; while(n) { pstu = new student; cout<<"请输入学号 姓名 成绩"<<endl; cin>>pstu->id>>pstu->name>>pstu->score; pstu->next=pre; pre=pstu;//指针赋值传的是地址 n--; } return pstu;}//输出链表的数据void showList(student *head){ student *p = head; while(p!=NULL){ cout<<p->id<<p->name<<p->score; p=p->next; }}void main(){ student *p ; p =createList(); showList(p);}
节点的插入
#include <iostream>using namespace std;struct student{ int id; char name[20]; float score; student *next;};//此函数的作用是自动创建链表,第一输入的是首节点,最后一个是首节点。student * createList(){ int n; student *pstu; student *pre=NULL; cout<<"请输入节点个数:"<<endl; cin>>n; while(n) { pstu = new student; cout<<"请输入学号 姓名 成绩"<<endl; cin>>pstu->id>>pstu->name>>pstu->score; pstu->next=pre; pre=pstu;//指针赋值传的是地址 n--; } return pstu;}//输出链表的数据void showList(student *head){ student *p = head; while(p!=NULL){ cout<<p->id<<p->name<<p->score; p=p->next; }}//插入链表节点函数,仔细理解。void insert(student *head ,student *insert){ student *temp = head->next; if(head==NULL){ head->next=insert; } head->next= insert; insert->next= temp;}void main(){ student *p ; p =createList(); //创建一个用于插入的节点 student cha= {12,"insert",122,NULL}; insert(p,&cha); showList(p);}
删除链表节点
#include <iostream>using namespace std;struct student{ int id; char name[20]; float score; student *next;};//此函数的作用是自动创建链表,第一输入的是首节点,最后一个是首节点。student * createList(){ int n; student *pstu; student *pre=NULL; cout<<"请输入节点个数:"<<endl; cin>>n; while(n) { pstu = new student; cout<<"请输入学号 姓名 成绩"<<endl; cin>>pstu->id>>pstu->name>>pstu->score; pstu->next=pre; pre=pstu;//指针赋值传的是地址 n--; } return pstu;}//输出链表的数据void showList(student *head){ student *p = head; while(p!=NULL){ cout<<p->id<<p->name<<p->score<<'\n'; p=p->next; }}//插入链表节点函数,仔细理解。void insert(student *head ,student *insert){ student *temp = head->next; if(head==NULL){ head->next=insert; } head->next= insert; insert->next= temp;}//节点的删除,首节点。对于中间节点则依据节点成员查询进行判断student * delect(student *head ){ student *temp1 = head->next; student *temp2 = temp1->next; head=temp1;return head;}void main(){ student *p ; p =createList(); //创建一个用于插入的节点 student cha= {12,"insert",122,NULL}; insert(p,&cha); p = delect(p); showList(p);}
0 0
- 【C++】链表学习
- C语言链表学习
- C语言:链表的学习
- C双向链表学习笔记
- Linux-C学习笔记-循环链表
- c语言链表《学习记录》
- Linux C 学习 单向链表
- C语言学习笔记 链表基础
- Linux C 学习 单向链表
- 链表学习(C语言实现)
- C语言链表学习笔记
- c语言之链表学习小结
- C语言学习之链表
- 用C学习线性表
- 有C语言实现链表的专题学习过程
- 数据结构学习系类列十-链表集合(c++)
- 学习C/C++语言:结构体,动态链表
- C语言数据结构--双向链表的学习
- 免安装Oracle客户端使用PL/SQL连接…
- PowerDesigner 逆向工程
- ORA-04091:表发生了变化触发器/函…
- Praat 学习日志1--安装和创建项目(入门必看)
- Latex插图笔记
- C++链表学习
- QTP--不能录制的问题
- [转]WebService优点和缺点小结
- pageoffice使用
- OAS-oci.dll
- JAVA内存不足
- 一台机器上安装多个Agile applicat…
- 【Windows/Linux常用命令】
- 欢迎使用CSDN-markdown编辑器