欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目: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;
 //創(chuàng)建鏈表
 head=Create(head);
 //輸出鏈表
 Print(head);
 //插入數據
 int k;
 cout<<"請輸入你要插入的同學的序號:";
 cin>>k;
 Insert(head,k);
 //輸出鏈表
 Print(head);
 //銷毀鏈表
    Destory(head);
 return 0;
}



現在,佐井同學的成績已經插入。
但是,卡卡西老師發(fā)現,鳴人的成績抄錯了,實際上是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;
 //創(chuàng)建鏈表
 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字符串,string對象,字符串字面值的區(qū)別詳解

欄    目:C語言

下一篇:關于C/C++中static關鍵字的作用總結

本文標題:淺析C++中單鏈表的增、刪、改、減

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4187.html

網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

如果侵犯了您的權利,請與我們聯系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

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

Copyright © 2002-2020 腳本教程網 版權所有