淺析C++中單鏈表的增、刪、改、減
更新時(shí)間:2013年09月02日 09:03:22 作者:
以下是對(duì)C++中單鏈表的增、刪、改、減進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
首先是是一個(gè)簡(jiǎn)單的例子,單鏈表的建立和輸出。
程序1.1
#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ī)
#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ī)。
#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;
}

程序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)文章
詳解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),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下2015-08-08關(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-04C語言實(shí)現(xiàn)電腦關(guān)機(jī)程序
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)電腦關(guān)機(jī)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Visual Studio 2022最新版安裝教程(圖文詳解)
本文主要介紹了Visual Studio 2022最新版安裝教程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01