淺析C++中單鏈表的增、刪、改、減。本站提示廣大學習愛好者:(淺析C++中單鏈表的增、刪、改、減)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析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;
//創立鏈表
head=Create(head);
//輸入鏈表
Print(head);
//拔出數據
int k;
cout<<"請輸出你要拔出的同窗的序號:";
cin>>k;
Insert(head,k);
//輸入鏈表
Print(head);
//燒毀鏈表
Destory(head);
return 0;
}
如今,佐井同窗的成就曾經拔出。
然則,卡卡西先生發明,鳴人的成就抄錯了,現實上是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;
//創立鏈表
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;
}