C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼
C++ 鏈表
之前一直沒怎么在意C++中的鏈表,但是突然一下子讓自己寫,就老是出錯。沒辦法,決定好好惡補(bǔ)一下該方面的知識,也為今后的數(shù)據(jù)結(jié)構(gòu)大下個良好的基礎(chǔ),于是我總結(jié)出以下幾點,有些地方可能不正確,還望大家不吝賜教,旨在共同進(jìn)步。
總結(jié):
1、鏈表List的基本單元是節(jié)點Node,因此想要操作方便,就必須為每一步打好基礎(chǔ),Node的基本結(jié)構(gòu)如下:
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
我們可以看出,Node的成員變量一共有兩個,都是public,因為我們要對這兩個變量進(jìn)行操作,所以不能是private類型的。然后是一個構(gòu)造函數(shù),第二個參數(shù)默認(rèn)值為NULL,也就是說如果我們創(chuàng)建新節(jié)點時只指定第一個參數(shù),而不寫第二個參數(shù),那么它默認(rèn)的就是NULL,以這種方式可以更靈活的使用Node,個人建議這么使用哦。
2、第二步就是創(chuàng)建我們的鏈表了,同樣我們這里先給出鏈表的代碼,在進(jìn)行一一的解釋。
class List{
private:
Node *head,*tail;
int position;
public:
List(){head=tail=NULL;};
~List(){delete head;delete tail;};
void print();
void Insert(int da=0);
void Delete(int da=0);
void Search(int da=0);
};
我們這里面有兩個數(shù)據(jù)類型,一個是Node。另一個是指代節(jié)點位置的成員變量(起不到什么作用,且不去管它吧)。使用head和tail來命名便是為了見名知意,使操作更加準(zhǔn)確。然后是重要的六個函數(shù),各自的功能不言而喻咯,其實最重要的是在每一個函數(shù)中我們都默認(rèn)能操作head和tail兩個成員變量,這樣能簡化我們的參數(shù)列表,使得函數(shù)更加優(yōu)雅。
下面是我的一個單鏈表的實現(xiàn),包含創(chuàng)建鏈表,插入值,刪除特定的值,查找特定值得在鏈表中的位置。
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
class List{
private:
Node *head,*tail;
int position;
public:
List(){head=tail=NULL;};
~List(){delete head;delete tail;};
void print();
void Insert(int da=0);
void Delete(int da=0);
void Search(int da=0);
int getValueAt(int position);
void setValueAt(int position,int da);
};
int List::getValueAt(int position){
Node *p=head;
if(p==NULL){
cout<<"The List is Empty!"<<endl;
}else{
int posi=0;
while(p!=NULL&&posi!=position){
posi++;
p=p->next;
}
if(p==NULL){
cout<<"There is no value of this position in this List!"<<endl;
}else{
cout<<"In this Position,the value is"<<p->data<<endl;
}
}
return p->data;
}
void List::setValueAt(int position,int da){
Node *p=head;
if(p==NULL){
cout<<"The List is Empty!"<<endl;
}else{
int posi=0;
while(p!=NULL&&posi!=position){
posi++;
p=p->next;
}
if(p==NULL){
cout<<"There is No Position in this List!"<<endl;
}else{
p->data=da;
cout<<"The Value in this position has been Updated!"<<endl;
}
}
}
void List::Search(int da){
Node *p=head;
if(p==NULL){
cout<<"Sorry, The List is Empty!"<<endl;
return;
}
int count=0;
while(p!=NULL&&p->data!=da){
p=p->next;
count++;
}
cout<<"the value you want to search is at position %d"<<count<<endl;
}
void List::Delete(int da){
Node *p=head,*q=head;
if(p==NULL){
cout<<"Sorry, The List is Empty!"<<endl;
return;
}
while(p!=NULL&&p->data!=da){
q=p;
p=p->next;
}
q->next=p->next;
cout<<"The Deletion Operation had been finished!"<<endl;
}
void List::Insert(int da){
if(head==NULL){
head=tail=new Node(da);
head->next=NULL;
tail->next=NULL;
}else{
Node *p=new Node(da);
tail->next=p;
tail=p;
tail->next=NULL;
}
}
void List::print(){
Node *p=head;
while(p!=NULL){
cout<<p->data<<" \a";
p=p->next;
}
cout<<endl;
}
int main(){
cout<<"Hello World!"<<endl;
List l1;
l1.Insert(1);
l1.Insert(2);
l1.Insert(3);
l1.Insert(4);
l1.Insert(5);
l1.Insert(6);
l1.Insert(7);
l1.print();
l1.Search(4);
l1.Delete(6);
l1.print();
l1.getValueAt(3);
l1.setValueAt(3,9);
l1.print();
cout<<"The End!"<<endl;
return 0;
}
//在此我想解釋的是,之所以數(shù)字4在鏈表中的位置為3,是因為其是從零開始計數(shù)的
下面是代碼運行后的結(jié)果:

好了,單鏈表的基本操作大致就是這樣了,希望我們都能從中有所收獲。如果您發(fā)現(xiàn)代碼中有什么錯誤,還望不吝賜教,讓我們共同進(jìn)步吧。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)與算法之隊列的實現(xiàn)詳解
隊列只允許在一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表,隊列具有先進(jìn)先出FIFO(First In First Out)的原則。本文將通過實例詳細(xì)說說隊列的實現(xiàn),需要的可以學(xué)習(xí)一下2022-10-10
C++實現(xiàn)拷貝構(gòu)造函數(shù)的方法詳解
拷貝構(gòu)造函數(shù)是構(gòu)造函數(shù)的一個重載,因此顯式的定義了拷貝構(gòu)造,那么編譯器也不再默認(rèn)生成構(gòu)造函數(shù)。本文主要介紹了C++實現(xiàn)拷貝構(gòu)造函數(shù)的方法,需要的可以參考一下2022-09-09
include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)
下面小編就為大家?guī)硪黄猧nclude包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
C++動態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù)
這篇文章主要介紹了C++動態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù),是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C語言實現(xiàn)大學(xué)生考勤管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)大學(xué)生考勤管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12

