C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼
C++ 鏈表
之前一直沒怎么在意C++中的鏈表,但是突然一下子讓自己寫,就老是出錯。沒辦法,決定好好惡補一下該方面的知識,也為今后的數(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)步吧。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
本文標(biāo)題:C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1829.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達(dá)式求值的方法詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10深入理解鏈表的各類操作詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(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-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?


