雙線性鏈表代碼 C++描述

來源:互聯網
上載者:User

#include <iostream>
#include <stdlib.h>

template <class T>
struct Node{   //結點類型
 T data;
 Node *next;  //前驅結點
 Node *prev;   //後繼結點
};

template <class T>
class lnkList{        //鏈表類型
 Node<T> *head,*tail;
public:
 lnkList(){       //初始化鏈表
  head = tail =new Node<T>;
  head->next=NULL;
 }
 ~lnkList(){      //解構函式,刪去鏈表
  Node<T> *tmp;
  while(head!=NULL){
   tmp=head;
   head=head->next;
   delete tmp;
  }
 }

 

 bool Insert(const int n,T value);
 bool Delete(const int n,T &value);
 bool TailAppend(const T value);
 bool headAppend(const T value);
 bool GetValue(const int n,T &value);
 void Print();

 

 

};

template <class T>
bool lnkList<T>::headAppend(const T value){  //頭插法
 Node<T> *p=new Node<T>;
 p->data=value;
 p->next=head->next;
 p->prev=head;
 head->next->prev=p;
 head->next=p;
 return true;

}

template <class T>
bool lnkList<T>::TailAppend(const T value){   //尾插法
 Node<T> *p=tail;
 Node<T> *q=new Node<T>;
 q->data=value;
 q->prev=q;
 q->next=NULL;
 p->next=q;
 tail=q;

 return true;
}

template <class T>
void lnkList<T>::Print(){  //列印輸出鏈表
 Node<T> *p=head->next;
 while(p!=NULL){
  std::cout<<p->data<<std::endl;
  p=p->next;
 }
}

 

template <class T>
bool lnkList<T>::Insert(const int n,T value){     //在鏈表第n個位置插入元素
 Node<T> *q=head;
 int count=1;
 while(q&&count<n){
  q=q->next;
  ++count;
 }
 if(!q||count>n){
  return false;
 }
 Node<T> *p=new Node<T>;
 p->data=value;
 p->prev=q;
 p->next=q->next;
 q->next->prev=p;
 q->next=p;
 return true;

}

template <class T>
bool lnkList<T>::Delete(const int n,T &value){   //刪除鏈表的第n個位置元素
 Node<T> *q=head;
 int count=1;
 while(q&&count<n){
  q=q->next;
  ++count;
 }
 Node<T> *p=q->next;
 q->next=p->next;
 p->next->prev=q;
 value=p->data;
 delete p;
 return true;
}

template <class T>
bool lnkList<T>::GetValue(const int n,T &value){  //得到鏈表的第n個元素
 Node<T> *q=head;
 int count=1;
 while(q&&count<n){
  q=q->next;
  ++count;
 }
 if(n<1||!q){
  return false;
 }
 value=q->next->data;
 return true;
}

 

 

 

//測試代碼
//////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
 double m;
 lnkList<double> l;
  l.TailAppend(2);
  l.TailAppend(3);
  l.TailAppend(4);
 l.TailAppend(5);
 l.headAppend(1);
  l.headAppend(0);
 l.Insert(3,1.5);
 l.GetValue(3,m);
  l.Print();
 return 0;
}
//////////////////////////////////////////////////////////////////////////
//在VS2008上測試通過

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.