淺析C++中單鏈表的增、刪、改、減
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
首先是是一個簡單的例子,單鏈表的建立和輸出。
程序1.1
復制代碼 代碼如下:
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
string score;
Student *next;//定義了指向Candidate類型變量的指針
};
int main(){
int n;//
cout<<"請輸入學生的總數:";
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<<"鏈表已經建立!"<<endl;
cout<<"\n==========下面輸入剛才的數據=============\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中,我們已經建立了一個鏈表。然后,我們在小櫻和鳴人之間插入一個佐井同學的成績
復制代碼 代碼如下:
#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<<"請輸入學生的總數:";
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<<"鏈表已經建立!"<<endl;
cout<<"\n==========下面輸入剛才的數據=============\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);
//插入數據
int k;
cout<<"請輸入你要插入的同學的序號:";
cin>>k;
Insert(head,k);
//輸出鏈表
Print(head);
//銷毀鏈表
Destory(head);
return 0;
}
現在,佐井同學的成績已經插入。
但是,卡卡西老師發(fā)現,鳴人的成績抄錯了,實際上是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<<"請輸入學生的總數:";
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<<"鏈表已經建立!"<<endl;
cout<<"\n==========下面輸入剛才的數據=============\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);
//插入數據
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;
}
上一篇:c字符串,string對象,字符串字面值的區(qū)別詳解
欄 目:C語言
本文標題:淺析C++中單鏈表的增、刪、改、減
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4187.html
您可能感興趣的文章
- 04-02c語言沒有round函數 round c語言
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現與遞歸實現的方法(C++)
- 01-10C++大數模板(推薦)
- 01-10淺談C/C++中的static與extern關鍵字的使用詳解
- 01-10深入C/C++浮點數在內存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


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