C++ 數據構造鏈表的完成代碼。本站提示廣大學習愛好者:(C++ 數據構造鏈表的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C++ 數據構造鏈表的完成代碼正文
C++ 鏈表
之前不斷沒怎樣在意C++中的鏈表,但是忽然一下子讓自己寫,就老是出錯。沒方法,決議好好惡補一下該方面的知識,也為今後的數據構造大下個良好的根底,於是我總結出以下幾點,有些中央能夠不正確,還望大家不吝賜教,旨在共同提高。
總結:
1、鏈表List的根本單元是節點Node,因而想要操作方便,就必需為每一步打好根底,Node的根本構造如下:
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
我們可以看出,Node的成員變量一共有兩個,都是public,由於我們要對這兩個變量停止操作,所以不能是private類型的。然後是一個結構函數,第二個參數默許值為NULL,也就是說假如我們創立新節點時只指定第一個參數,而不寫第二個參數,那麼它默許的就是NULL,以這種方式可以更靈敏的運用Node,團體建議這麼運用哦。
2、第二步就是創立我們的鏈表了,異樣我們這裡先給出鏈表的代碼,在停止逐個的解釋。
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);
};
我們這外面有兩個數據類型,一個是Node。另一個是指代節點地位的成員變量(起不到什麼作用,且不去管它吧)。運用head和tail來命名便是為了見名知意,使操作愈加精確。然後是重要的六個函數,各自的功用顯而易見咯,其實最重要的是在每一個函數中我們都默許能操作head和tail兩個成員變量,這樣能簡化我們的參數列表,使得函數愈加優雅。
上面是我的一個單鏈表的完成,包括創立鏈表,拔出值,刪除特定的值,查找特定值得在鏈表中的地位。
#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;
}
//在此我想解釋的是,之所以數字4在鏈表中的地位為3,是由於其是從零開端計數的
上面是代碼運轉後的後果:
好了,單鏈表的根本操作大致就是這樣了,希望我們都能從中有所播種。假如您發古代碼中有什麼錯誤,還望不吝賜教,讓我們共同提高吧。
感激閱讀,希望能協助到大家,謝謝大家對本站的支持!