浅析C++中单链表的增、删、改、减_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;
}

时间: 2024-09-08 12:34:11

浅析C++中单链表的增、删、改、减_C 语言的相关文章

oracle监控某表变动触发器例子(监控增,删,改)_oracle

使用oracle触发器 实现对某个表的增改删的监控操作,并记录到另一个表中. 代码: 复制代码 代码如下: create or replace trigger test_trigger    before insert or update or delete on test_table    for each row  declare    v_id        varchar2(30);    v_bdlb      varchar2(1);    v_jgdm      VARCHAR2(

简单的php数据库操作类代码(增,删,改,查)_php实例

数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) 连接数据库服务器•resource mysql_pconnect ( [string server [, strin

Lucene能否像数据库那样对已存放记录的索引文件的某字段进行增/删/改?

问题描述 比如现在的索引文件中的字段是articleid.title.content.create_date.userid,并且已经索引了10000个记录.我现在需要将其中的userid字段删除掉,将date字段重命名为last_modify,同时增加一个visit_count字段.请问Lucene是否支持这样的操作呢? 解决方案 据我所知不能,都不能不过你可以试试引用通过读取index文件,然后遍历所有的doc,获取所有的field,然后添加到你新的doc里面,然后重新写入index文件因为我

使用DataTable更新数据库(增,删,改)_实用技巧

1.修改数据 复制代码 代码如下:             DataRow dr = hRDataSet.Tables["emp"].Rows.Find(textBox3.Text);            //DataRow dr = hRDataSet.Tables["emp"].Select("id="+textBox3.Text)[0];            dr.BeginEdit();            dr["nam

JS操作图片(增,删,改) 例子_javascript技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></titl

Struts2实现CRUD(增 删 改 查)功能实例代码_java

CRUD是Create(创建).Read(读取).Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影.如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了,所以大家使用新框架开发OLTP(Online Transaction Processing)应用程序时,首先会研究一下如何编写CRUD.这类似于大家在学习新编程语言时喜欢编写"Hello World". 本文旨在讲述Struts 2上的CRUD开发,所以为了例子的简单易懂,我不会花时间在数

简单的增 删 改 查

一:insert语句into 关键字是可选的values关键字前面的()是可选的,这里是要接收数据的列values后面,有两种方式提供值1:显式的给出值  2:从select语句中导出值 insert语句注意几点1:不要理标志列,系统会给你插入的2:给出实际的值,如果没有,那就null3:给出默认的值,default关键字,告诉数据库取默认值 insert into ... select什么时候会这么用,当成批的数据来自1:数据库中的另一个表2:同一台服务器完全不同的数据库3:另一个SQLSER

C语言之单向链表详解及实例代码_C 语言

1,单向链简洁. 单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指针成员变量指列表中的下一个结点:列表是由结点构成,由head指针指向第一个成为表头的结点而终止于最后一个指向nuLL的指针: 2,例子要求: 根据示例代码中的例子,完成单向链表(single linked list)中的以字符串为数据的链表的插入.删除以及查找,并支持单向链表的反转

浅析C/C++中动态链接库的创建和调用_C 语言

DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的创建步骤: 创建Dll有两种方式. 一.创建Non-MFC DLL动态链接库 1.打开File -> New -> Project选项,选择Win32 Dynamic-Link Library ->sample project ->工程名:DllDemo 2.新建一个.h文件DllDe