淺析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;
}
上一篇:c字符串,string對象,字符串字面值的區(qū)別詳解
欄 目:C語言
下一篇:關(guān)于C/C++中static關(guān)鍵字的作用總結(jié)
本文標(biāo)題:淺析C++中單鏈表的增、刪、改、減
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4187.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置