最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年09月02日 09:03:22   作者:  
以下是對C++中單鏈表的增、刪、改、減進行了詳細的介紹,需要的朋友可以過來參考下
首先是是一個簡單的例子,單鏈表的建立和輸出。
程序1.1
復制代碼 代碼如下:

#include<iostream>
#include<string>
using namespace std;
struct Student{
 string name;
 string score;
 Student *next;//定義了指向Candidate類型變量的指針
};
int main(){
 int n;//
 cout<<"請輸入學生的總數(shù):";
 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<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\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中,我們已經(jīng)建立了一個鏈表。然后,我們在小櫻和鳴人之間插入一個佐井同學的成績
復制代碼 代碼如下:

#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<<"請輸入學生的總數(shù):";
 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<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\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);
 //插入數(shù)據(jù)
 int k;
 cout<<"請輸入你要插入的同學的序號:";
 cin>>k;
 Insert(head,k);
 //輸出鏈表
 Print(head);
 //銷毀鏈表
    Destory(head);
 return 0;
}



現(xiàn)在,佐井同學的成績已經(jīng)插入。
但是,卡卡西老師發(fā)現(xiàn),鳴人的成績抄錯了,實際上是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<<"請輸入學生的總數(shù):";
 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<<"鏈表已經(jīng)建立!"<<endl;
 cout<<"\n==========下面輸入剛才的數(shù)據(jù)=============\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);
 //插入數(shù)據(jù)
 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;
}



相關(guān)文章

最新評論

蒙山县| 东至县| 武定县| 洱源县| 大连市| 永康市| 赣州市| 焦作市| 凤冈县| 寻甸| 博罗县| 陕西省| 名山县| 根河市| 克山县| 义马市| 磐安县| 丘北县| 抚顺市| 城步| 平谷区| 嘉黎县| 化州市| 南宫市| 天津市| 济源市| 鹰潭市| 琼中| 玛纳斯县| 富顺县| 岳普湖县| 庆阳市| 鹤山市| 芮城县| 疏附县| 蒙山县| 晋城| 宜都市| 清水县| 淮阳县| 芮城县|