国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-01-26 15:55:14
字體:
來源:轉載
供稿:網友
首先是是一個簡單的例子,單鏈表的建立和輸出。
程序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;
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台安县| 吉首市| 天门市| 米易县| 碌曲县| 商城县| 高淳县| 临沧市| 乌恰县| 乌审旗| 仙游县| 广德县| 武川县| 海林市| 东宁县| 普宁市| 临江市| 宣威市| 新余市| 成安县| 平舆县| 崇信县| 克东县| 甘洛县| 饶阳县| 江都市| 诸暨市| 隆尧县| 商南县| 太白县| 徐汇区| 金山区| 伽师县| 泰顺县| 太保市| 凌云县| 怀宁县| 南汇区| 安丘市| 辉县市| 安丘市|