雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

淺析C++中單鏈表的增、刪、改、減

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

首先是是一個(gè)簡(jiǎn)單的例子,單鏈表的建立和輸出。
程序1.1

復(fù)制代碼 代碼如下:

#include<iostream>
#include<string>
using namespace std;
struct Student{
 string name;
 string score;
 Student *next;//定義了指向Candidate類型變量的指針
};
int main(){
 int n;//
 cout<<"請(qǐng)輸入學(xué)生的總數(shù):";
 cin>>n;
 int i=1;
 Student *p=NULL;
 Student *node=NULL;
 Student *head=NULL;
 //建立鏈表
 for(;i<=n;i++){
  node=new Student;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的姓名:";
  cin>>node->name;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的成績(jī):";
  cin>>node->score;
  if(head==NULL)
   head=node;
  else
   p->next=node;
  p=node;
  if(i==n){
   p->next=NULL;
  }
 }
 //輸出鏈表
 p=head;
 cout<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\n"<<endl;
 i=1;
 while(p!=NULL){
  cout<<"第"<<i<<"個(gè)同學(xué)==="<<p->name<<"==成績(jī)===="<<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中,我們已經(jīng)建立了一個(gè)鏈表。然后,我們?cè)谛押网Q人之間插入一個(gè)佐井同學(xué)的成績(jī)
復(fù)制代碼 代碼如下:

#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<<"請(qǐng)輸入學(xué)生的總數(shù):";
 cin>>n;
 for(int i=1;i<=n;i++){
  node=new Student;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的姓名:";
  cin>>node->name;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的成績(jī):";
  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<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\n"<<endl;
 int i=1;
 while(p!=NULL){
  cout<<"第"<<i<<"個(gè)同學(xué)==="<<p->name<<"==成績(jī)===="<<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<<"位同學(xué)的名字:";
   cin>>node->name;
   cout<<"第"<<k<<"位同學(xué)的成績(jī):";
   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;
 //創(chuàng)建鏈表
 head=Create(head);
 //輸出鏈表
 Print(head);
 //插入數(shù)據(jù)
 int k;
 cout<<"請(qǐng)輸入你要插入的同學(xué)的序號(hào):";
 cin>>k;
 Insert(head,k);
 //輸出鏈表
 Print(head);
 //銷毀鏈表
    Destory(head);
 return 0;
}



現(xiàn)在,佐井同學(xué)的成績(jī)已經(jīng)插入。
但是,卡卡西老師發(fā)現(xiàn),鳴人的成績(jī)抄錯(cuò)了,實(shí)際上是100,需要修改成績(jī);然后,佐助同學(xué)輟學(xué)了,所以,還要?jiǎng)h除他的成績(jī)。
復(fù)制代碼 代碼如下:

#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<<"請(qǐng)輸入學(xué)生的總數(shù):";
 cin>>n;
 for(int i=1;i<=n;i++){
  node=new Student;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的姓名:";
  cin>>node->name;
  cout<<"請(qǐng)輸入第"<<i<<"個(gè)同學(xué)的成績(jī):";
  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<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\n"<<endl;
 int i=1;
 while(p!=NULL){
  cout<<"第"<<i<<"個(gè)同學(xué)==="<<p->name<<"==成績(jī)===="<<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位同學(xué)的名字:";
   cin>>node->name;
   cout<<"第1位同學(xué)的成績(jī):";
   cin>>node->score;
   node->next=head->next;
   head=node;
 }
 int i=1;
 while(p!=NULL){
  if(i+1==k){
   node=new Student;
   cout<<"第"<<k<<"位同學(xué)的名字:";
   cin>>node->name;
   cout<<"第"<<k<<"位同學(xué)的成績(jī):";
   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<<"位同學(xué)的名字:";
   cin>>p->name;
   cout<<"第"<<k<<"位同學(xué)的成績(jī):";
   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;
 //創(chuàng)建鏈表
 head=Create(head);
 //輸出鏈表
 Print(head);
 //插入數(shù)據(jù)
 int k;
 cout<<"請(qǐng)輸入你要插入的同學(xué)的序號(hào):";
 cin>>k;
 Insert(head,k);
 //輸出鏈表
 Print(head);
 //修改鏈表
 cout<<"請(qǐng)輸入你要修改的同學(xué)的序號(hào):";
 cin>>k;
 Alter(head,k);
 //輸出鏈表
 Print(head);
 //刪除其中的一個(gè)項(xiàng)
 cout<<"請(qǐng)輸入你要?jiǎng)h除的同學(xué)的序號(hào):";
 cin>>k;
 head=Delete(head,k); 
 //輸出鏈表
 Print(head);
 //銷毀鏈表
    Destory(head);
 return 0;
}



上一篇:c字符串,string對(duì)象,字符串字面值的區(qū)別詳解

欄    目:C語(yǔ)言

下一篇:關(guān)于C/C++中static關(guān)鍵字的作用總結(jié)

本文標(biāo)題:淺析C++中單鏈表的增、刪、改、減

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/4187.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有