首先是是一个简单的例子,单链表的建立和输出。
程序1.1
复制代码 代码如下:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
string score;
Student *next;//定义了指向Candidate类型变量的指针
};
int main(){
int n;//
cout<<"请输入学生的总数:";
cin>>n;
int i=1;
Student *p=NULL;
Student *node=NULL;
Student *head=NULL;
//建立链表
for(;i<=n;i++){
node=new Student;
cout<<"请输入第"<<i<<"个同学的姓名:";
cin>>node->name;
cout<<"请输入第"<<i<<"个同学的成绩:";
cin>>node->score;
if(head==NULL)
head=node;
else
p->next=node;
p=node;
if(i==n){
p->next=NULL;
}
}
//输出链表
p=head;
cout<<"链表已经建立!"<<endl;
cout<<"\n==========下面输入刚才的数据=============\n"<<endl;
i=1;
while(p!=NULL){
cout<<"第"<<i<<"个同学==="<<p->name<<"==成绩===="<<p->score<<endl;
p=p->next;
i++;
}
//销毁链表
Student *d;
p=head;
while(p!=NULL){
d=p;
p=p->next;
delete d;
}
return 0;
}
在程序1.1中,我们已经建立了一个链表。然后,我们在小樱和鸣人之间插入一个佐井同学的成绩
复制代码 代码如下:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
string score;
Student *next;//定义了指向Candidate类型变量的指针
};
Student * Create(Student * head){
Student *p=NULL;
Student *node=NULL;
int n;//
cout<<"请输入学生的总数:";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"请输入第"<<i<<"个同学的姓名:";
cin>>node->name;
cout<<"请输入第"<<i<<"个同学的成绩:";
cin>>node->score;
if(head==NULL)
head=node;
else
p->next=node;
p=node;
if(i==n){
p->next=NULL;
}
}
return head;
}
void Print(Student * head){
Student *p=NULL;
p=head;
cout<<"链表已经建立!"<<endl;
cout<<"\n==========下面输入刚才的数据=============\n"<<endl;
int i=1;
while(p!=NULL){
cout<<"第"<<i<<"个同学==="<<p->name<<"==成绩===="<<p->score<<endl;
p=p->next;
i++;
}
cout<<"\n"<<endl;
}
void Insert(Student * head,int k){
Student *p=NULL;
Student *node=NULL;
p=head;
int i=1;
while(p!=NULL){
if(i+1==k){
node=new Student;
cout<<"第"<<k<<"位同学的名字:";
cin>>node->name;
cout<<"第"<<k<<"位同学的成绩:";
cin>>node->score;
node->next=p->next;
p->next=node;
}
p=p->next;
i++;
}
}
void Destory(Student * head){
Student *d;
Student *p=NULL;
p=head;
while(p!=NULL){
d=p;
p=p->next;
delete d;
}
}
int main(){
Student *head=NULL;
//创建链表
head=Create(head);
//输出链表
Print(head);
//插入数据
int k;
cout<<"请输入你要插入的同学的序号:";
cin>>k;
Insert(head,k);
//输出链表
Print(head);
//销毁链表
Destory(head);
return 0;
}
现在,佐井同学的成绩已经插入。
但是,卡卡西老师发现,鸣人的成绩抄错了,实际上是100,需要修改成绩;然后,佐助同学辍学了,所以,还要删除他的成绩。
复制代码 代码如下:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
string score;
Student *next;//定义了指向Candidate类型变量的指针
};
Student * Create(Student * head){
Student *p=NULL;
Student *node=NULL;
int n;//
cout<<"请输入学生的总数:";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"请输入第"<<i<<"个同学的姓名:";
cin>>node->name;
cout<<"请输入第"<<i<<"个同学的成绩:";
cin>>node->score;
if(head==NULL)
head=node;
else
p->next=node;
p=node;
if(i==n){
p->next=NULL;
}
}
return head;
}
void Print(Student * head){
Student *p=NULL;
p=head;
cout<<"链表已经建立!"<<endl;
cout<<"\n==========下面输入刚才的数据=============\n"<<endl;
int i=1;
while(p!=NULL){
cout<<"第"<<i<<"个同学==="<<p->name<<"==成绩===="<<p->score<<endl;
p=p->next;
i++;
}
cout<<"\n"<<endl;
}
void Insert(Student * head,int k){
Student *p=NULL;
Student *node=NULL;
p=head;
if(k==1){
node=new Student;
cout<<"第1位同学的名字:";
cin>>node->name;
cout<<"第1位同学的成绩:";
cin>>node->score;
node->next=head->next;
head=node;
}
int i=1;
while(p!=NULL){
if(i+1==k){
node=new Student;
cout<<"第"<<k<<"位同学的名字:";
cin>>node->name;
cout<<"第"<<k<<"位同学的成绩:";
cin>>node->score;
node->next=p->next;
p->next=node;
}
p=p->next;
i++;
}
}
void Destory(Student * head){
Student *d;
Student *p=NULL;
p=head;
while(p!=NULL){
d=p;
p=p->next;
delete d;
}
}
void Alter(Student * head,int k){
int i=1;
Student *p=head;
while(p!=NULL){
if(i==k){
cout<<"第"<<k<<"位同学的名字:";
cin>>p->name;
cout<<"第"<<k<<"位同学的成绩:";
cin>>p->score;
}
p=p->next;
i++;
}
}
Student * Delete(Student * head,int k){
int i=1;
Student *p=head;
Student *d=head;
if(k==1){
head=head->next;
}else{
while(p!=NULL){
if(i+1==k){
p->next=p->next->next;
}
p=p->next;
i++;
}
}
return head;
}
int main(){
Student *head=NULL;
//创建链表
head=Create(head);
//输出链表
Print(head);
//插入数据
int k;
cout<<"请输入你要插入的同学的序号:";
cin>>k;
Insert(head,k);
//输出链表
Print(head);
//修改链表
cout<<"请输入你要修改的同学的序号:";
cin>>k;
Alter(head,k);
//输出链表
Print(head);
//删除其中的一个项
cout<<"请输入你要删除的同学的序号:";
cin>>k;
head=Delete(head,k);
//输出链表
Print(head);
//销毁链表
Destory(head);
return 0;
}
相关推荐:
Bing学术搜索结果不显示时间?如何解决这一问题,提升学术研究效率!,ai怎么参考线
ChatGPT显示503:如何应对AI服务不可用的困境?,ai宝贝宝贝
ChatGPT不能用?揭秘你可能忽视的真相和解决方法,强国ai2022
SEO优化关键词品牌:如何通过精准的关键词提升品牌价值,ai免费智能写作论文
SEO站外优化策略:提升网站排名的关键因素,遵义公司网站推广
SEO赚钱:如何通过SEO技能在网络上实现财富自由,网站怎么建设推广平台
ChatGPT打不开实时问题解决方案:让你的AI助手始终在线,psd 转ai
SEO新站优化指南:快速提升排名的必备技巧,海珠网站推广哪家有名
SEO每日:提高网站流量的秘密武器,助你脱颖而出,seo优化和排名技巧
seo需要学些什么内容,学seo的基础 ,中国ai公司年收入
AI缩写在线:让人工智能助力你行业前沿技术,ai uhrehara
怎么用AI写出高质量科普文章?揭秘新时代创作利器!
SEO优化做什么的?揭秘SEO优化的核心价值与实战应用,ai做金色
seo需要会什么技术呢,seo需要会什么技术呢知乎 ,红色框ai
SEO和品牌营销:如何通过搜索引擎优化打造品牌影响力,建邺seo软件
SEO查:如何用精准的SEO诊断助力网站流量爆发,美团关键词排名怎么补
ChatGPT的破解版:AI世界的新突破,ai无禁忌
ChatGPT安装包Windows版:让AI助力你的工作与生活,人工智能ai不是梦在线
什么是神马排名?让你的网站脱颖而出,轻松占据搜索引擎的C位!,乳山网站优化关键词排名
OpenAIGPTChatSoraOpenAIChatGPT服务在中断数小时后已恢复,ai视图线稿
SEO利用:让你的网站快速登顶,获取更多流量与客户!,延边小程序推广平台网站
SEO前的准备工作:如何让网站为搜索引擎优化做好充分准备,SEO_网站排名优化_网络推广
seo种草什么意思,seo yoo na ,三维地震反演AI
ChatGPT页面无法下拉?禁用浏览器扩展,轻松解决!,imba 1.6 ai
SEO舆情:如何通过有效的舆情管理提升企业品牌形象,seo推广外包提高收录
SEO就业前景如何?打造数字时代的职业新风口,常州武进区水产网站建设
SEO要点:提升网站排名的核心技巧与策略,福田市网站建设推广费用
SEO策划:让你的网站迅速脱颖而出的秘诀,seo优化和技巧
ChatGPT使用问题?如果您正在使用VPN,请尝试将其关闭,ai制作渐变立体
ChatGPT无法加载?检查您的网络设置并尝试重启,轻松解决常见问题!,ai招生广告设计
ChatGPTWindows版本下载:让AI助力您的工作和生活,ai yamama
文章AI生成:让创作变得更简单、更高效!
seo网站编辑是做什么,seo网站编辑可在家兼职 ,ai慢直播
seo适用于什么领域,seo适用于什么领域中 ,ai智能翻译写作机器人v1.0
ChatGPTCanvex打不开?详细分析及解决方法助你顺利使用,ai2.5d网格线去除
未来科技:AI工具为生活赋能,打造智能未来
SEO单页网站:助力企业在线营销的一站式解决方案,湖南视频网站优化方式
AI缩短短文-提升创作效率,写作新体验,光速写作业ai写作app
AI一键生成文章免费版:颠覆写作新体验
企业关键字-助力企业成功的隐形动力,闽侯县企业网站建设
AI一键生成文章在线:提升创作效率,改变写作方式
创作新时代:自动生成文章AI的魅力与未来
GPT版本全解:从基础到高级,如何选择最适合你的方案?,韩国ai人工智能
SEO总计:如何通过SEO优化实现网站流量和排名的飞跃,三元桥网站优化
ChatGPT页面不自动显示最新消息:如何解决这一困扰,提升使用体验?,斑马ai幼儿百度云网盘
ChatGPT不能用了?了解这一背后的真相及解决方法,ai恐怖头像
SEO自己:打造属于你的数字营销利器,微信裂变营销推广软件
为什么“360收录”是你网站推广的必备利器,seo亚马逊
seo竞价做的什么工作,seo 竞价 ,office智能ai
AI免费生成:开启智能创作新纪元,助力你的创意无限可能