欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2013年09月02日 09:03:22   作者:  
以下是對(duì)C++中單鏈表的增、刪、改、減進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
首先是是一個(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;
}



相關(guān)文章

  • Dev-C++無法使用bits/stdc++.h問題及解決

    Dev-C++無法使用bits/stdc++.h問題及解決

    這篇文章主要介紹了Dev-C++無法使用bits/stdc++.h問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++中多線程間共享數(shù)據(jù)詳解

    C++中多線程間共享數(shù)據(jù)詳解

    這篇文章主要為大家詳細(xì)介紹了C++中多線程間共享數(shù)據(jù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 詳解C++中const_cast與reinterpret_cast運(yùn)算符的用法

    詳解C++中const_cast與reinterpret_cast運(yùn)算符的用法

    這篇文章主要介紹了C++中const_cast與reinterpret_cast運(yùn)算符的用法,經(jīng)常被用于表達(dá)式中的類型轉(zhuǎn)換,需要的朋友可以參考下
    2016-01-01
  • 使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu)

    使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu)

    這篇文章主要介紹了使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下
    2015-08-08
  • C++虛函數(shù)表的原理與使用解析

    C++虛函數(shù)表的原理與使用解析

    對(duì)C++?了解的人都應(yīng)該知道虛函數(shù)(Virtual?Function)是通過一張?zhí)摵瘮?shù)表(Virtual?Table)來實(shí)現(xiàn)的。簡(jiǎn)稱為V-Table。本文就將詳細(xì)講講虛函數(shù)表的原理與使用,需要的可以參考一下
    2022-04-04
  • 關(guān)于C語言中數(shù)據(jù)在內(nèi)存中的存儲(chǔ)詳解

    關(guān)于C語言中數(shù)據(jù)在內(nèi)存中的存儲(chǔ)詳解

    這篇文章主要給大家介紹了關(guān)于C語言中數(shù)據(jù)在內(nèi)存中的存儲(chǔ)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • C語言實(shí)現(xiàn)電腦關(guān)機(jī)程序

    C語言實(shí)現(xiàn)電腦關(guān)機(jī)程序

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)電腦關(guān)機(jī)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Visual Studio 2022最新版安裝教程(圖文詳解)

    Visual Studio 2022最新版安裝教程(圖文詳解)

    本文主要介紹了Visual Studio 2022最新版安裝教程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 快速學(xué)習(xí)六大排序算法

    快速學(xué)習(xí)六大排序算法

    這篇文章主要介紹了六大排序算法-插入排序、希爾排序、選擇排序、冒泡排序、堆排序、快速排序,需要學(xué)習(xí)的小伙伴可以參考這篇文章
    2021-08-08
  • 淺談C++左值引用和右值引用

    淺談C++左值引用和右值引用

    下面小編就為大家?guī)硪黄獪\談C++左值引用和右值引用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01

最新評(píng)論