淺析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<<"請輸入學(xué)生的總數(shù):";
cin>>n;
int i=1;
Student *p=NULL;
Student *node=NULL;
Student *head=NULL;
//建立鏈表
for(;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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)建立了一個鏈表。然后,我們在小櫻和鳴人之間插入一個佐井同學(xué)的成績
#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<<"請輸入學(xué)生的總數(shù):";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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<<"位同學(xué)的名字:";
cin>>node->name;
cout<<"第"<<k<<"位同學(xué)的成績:";
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<<"請輸入你要插入的同學(xué)的序號:";
cin>>k;
Insert(head,k);
//輸出鏈表
Print(head);
//銷毀鏈表
Destory(head);
return 0;
}

現(xiàn)在,佐井同學(xué)的成績已經(jīng)插入。
但是,卡卡西老師發(fā)現(xiàn),鳴人的成績抄錯了,實際上是100,需要修改成績;然后,佐助同學(xué)輟學(xué)了,所以,還要刪除他的成績。
#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<<"請輸入學(xué)生的總數(shù):";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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位同學(xué)的名字:";
cin>>node->name;
cout<<"第1位同學(xué)的成績:";
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é)的成績:";
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é)的成績:";
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<<"請輸入你要插入的同學(xué)的序號:";
cin>>k;
Insert(head,k);
//輸出鏈表
Print(head);
//修改鏈表
cout<<"請輸入你要修改的同學(xué)的序號:";
cin>>k;
Alter(head,k);
//輸出鏈表
Print(head);
//刪除其中的一個項
cout<<"請輸入你要刪除的同學(xué)的序號:";
cin>>k;
head=Delete(head,k);
//輸出鏈表
Print(head);
//銷毀鏈表
Destory(head);
return 0;
}

程序1.1
復(fù)制代碼 代碼如下:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
string score;
Student *next;//定義了指向Candidate類型變量的指針
};
int main(){
int n;//
cout<<"請輸入學(xué)生的總數(shù):";
cin>>n;
int i=1;
Student *p=NULL;
Student *node=NULL;
Student *head=NULL;
//建立鏈表
for(;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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)建立了一個鏈表。然后,我們在小櫻和鳴人之間插入一個佐井同學(xué)的成績
復(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<<"請輸入學(xué)生的總數(shù):";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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<<"位同學(xué)的名字:";
cin>>node->name;
cout<<"第"<<k<<"位同學(xué)的成績:";
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<<"請輸入你要插入的同學(xué)的序號:";
cin>>k;
Insert(head,k);
//輸出鏈表
Print(head);
//銷毀鏈表
Destory(head);
return 0;
}

現(xiàn)在,佐井同學(xué)的成績已經(jīng)插入。
但是,卡卡西老師發(fā)現(xiàn),鳴人的成績抄錯了,實際上是100,需要修改成績;然后,佐助同學(xué)輟學(xué)了,所以,還要刪除他的成績。
復(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<<"請輸入學(xué)生的總數(shù):";
cin>>n;
for(int i=1;i<=n;i++){
node=new Student;
cout<<"請輸入第"<<i<<"個同學(xué)的姓名:";
cin>>node->name;
cout<<"請輸入第"<<i<<"個同學(xué)的成績:";
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<<"個同學(xué)==="<<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位同學(xué)的名字:";
cin>>node->name;
cout<<"第1位同學(xué)的成績:";
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é)的成績:";
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é)的成績:";
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<<"請輸入你要插入的同學(xué)的序號:";
cin>>k;
Insert(head,k);
//輸出鏈表
Print(head);
//修改鏈表
cout<<"請輸入你要修改的同學(xué)的序號:";
cin>>k;
Alter(head,k);
//輸出鏈表
Print(head);
//刪除其中的一個項
cout<<"請輸入你要刪除的同學(xué)的序號:";
cin>>k;
head=Delete(head,k);
//輸出鏈表
Print(head);
//銷毀鏈表
Destory(head);
return 0;
}

相關(guān)文章
淺談關(guān)于指針作為參數(shù)并改變它的值的問題
這篇文章介紹了關(guān)于指針作為參數(shù)并改變它的值的問題,有需要的朋友可以參考一下2013-10-10
VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法
這篇文章介紹了VS2017開發(fā)C語言出現(xiàn)“no_init_all“的解決辦法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
C語言中isalnum()函數(shù)和isalpha()函數(shù)的對比使用
這篇文章主要介紹了C語言中isalnum()函數(shù)和isalpha()函數(shù)的對比使用,都可以判斷是否為字母但isalnum的判斷還包括數(shù)字,需要的朋友可以參考下2015-08-08
C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL)
這篇文章主要介紹了C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06

