雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

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)步吧。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

上一篇:實現(xiàn)一個內(nèi)存池管理的類方法

欄    目:C語言

下一篇:減少C++代碼編譯時間的簡單方法(必看篇)

本文標(biāo)題:C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實現(xiàn)代碼

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1829.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有