单链表的增、删、改、减(C++)
首先是是一个简单的例子,单链表的建立和输出。
程序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;
}
单链表的增、删、改、减(C++)的更多相关文章
- django单表操作 增 删 改 查
一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- StringBuilder修改字符串内容,增,删,改,插
package seday01;/** * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作的性能.因此String不适合频繁修改内容. * 若有频繁修改操作,使用StringBuilder来完 ...
随机推荐
- UOJ #17. 【NOIP2014】飞扬的小鸟 背包DP
#17. [NOIP2014]飞扬的小鸟 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:// ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- C#高级编程9-第1章.NET体系结构
C#与NET的关系 C#编写的所有代码必须使用.NET FrameWork运行 C#是一种语言,但它本身不是.NET的一部分 C#一些特性,.NET不支持,.NET一些特性,C#不支持 公共语言运行库 ...
- ProtoBuffer使用笔记
ProtoBuffer是由谷歌研发的对象序列化和反序列化的开源工具,ProtoBuffer和Xml类似,都是数据描述工具,后者使用更为广泛,前者Google内部使用且具有更高的效率.该工具安装和使用都 ...
- MySQL多表联查之ThinkPHP中的实现
创建两个表如图: sp_user表: sp_dept表: 目的:通过sp_user的dept_id查询所属部门即sp_dept中的name. 原生sq方法一:select t1.*,t2.name a ...
- 使用Chrome快速实现数据的抓取(二)——协议
在前面的文章简单的介绍了一下Chrome调试模式的启动方式,但前面的API只能做到简单的打开,关闭标签操作,当我们需要对某个标签页进行详细的操作时,则需要用到页面管理API.首先我们还是来回顾下获取页 ...
- ICD2 VPP limiter for new PIC microcontrollers.
http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...
- 用资源管理器右键编译 Visual Studio 解决方案文件
每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...
- 【liunx】使用xshell连接虚拟机上的CentOS 7,使用xhell连接本地虚拟机上的Ubuntu, 获取本地虚拟机中CentOS 7的IP地址,获取本地虚拟机中Ubuntu 的IP地址,Ubuntu开启22端口
注意,如果想用xshell去连接本地虚拟机中的linux系统,需要本地虚拟机中的系统是启动的才能连接!!!!! ============================================ ...
- Android Gradle Plugin指南(五)——Build Variants(构建变种版本号)
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants 6. Build Vari ...