单链表的增、删、改、减(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来完 ...
随机推荐
- wikioi 1048 石子归并
dp[i][j]=min(dp[i][j],dp[i][k],dp[k+1][j]+sum[i][j]); 表示i-j的最小合并代价. #include <iostream> #inclu ...
- wikioi 1014 装箱问题(背包)
题目描述 Description 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30),每个物品有一个体积(正整数). 要求n个物品中,任取若 ...
- C#高级编程9-第4章 继承
继承是面向对象的一大特征.要深刻学习继承,需要学会使用调试的技巧来学习它,因为它比较抽象. 继承 继承是指一个具体的类型直接使用另一类型的某些数据成员或函数成员,继承的类是基类(父类),被继承的类是派 ...
- 记一次帮朋友解决apache站点403错误的过程
apache版本: [root@iZ25eby2utyZ web]# rpm -qa | grep httpd httpd-tools--.el6.centos..x86_64 httpd--.el6 ...
- mysql数据库cup飙升处理思路
1.先top查看是那一个进程,哪个端口占用CPU多. 2.show processeslist查看是否由于大量并发,锁引起的负载问题. 3.否则,查看慢查询,找出执行时间长的sql:explain分析 ...
- js解析顺序了解一下??
我们在学习一种新事物的时候,总是知其然,而不知其所然.有些人会探究到底,有一些人会得过且过. 好了,开场白结束,直接进入正题. js不像C语言那样只要编译一次之后成.exe文件之后就不用在编译可以直接 ...
- 解决firefox不支持innerText的办法
js代码: <script> window.onload = function(){ if(window.navigator.userAgent.toLowerCase().indexOf ...
- Ubuntu下(Linux+Apache+MYSQL+PHP, LAMP)环境搭建
近期開始玩PHP,于是试着搭建一下开发环境并做个记录,以备日后再使用起来方便可查. 第一步 确保软件包是最新的 sudo apt-get update 第二步 安装Apache2 sudo apt-g ...
- 正确理解java编译时,运行时以及构建时这三个概念
Java中的许多对象(一般都是具有父子类关系的父类对象)在运行时都会出现两种类型:编译时类型和运行时类型,例如:Person person = new Student();这行代码将会生成一个pers ...
- Announcing Mobile SDK V2.0
As you might have read over at our PayPal Forward Blog it’s time to celebrate for PayPal | Developer ...