雙向鍊錶的實現

2021-06-19 19:08:57 字數 3427 閱讀 4401

// doublelist.cpp : 定義控制台應用程式的入口點。 

//雙向鍊錶 

#include "stdafx.h" 

#include

using namespace std; 

template

struct node 

;  template

class doublelist 

;  template

doublelist::doublelist()//預設建構函式 

template

doublelist::doublelist(const doublelist& otherlist)//拷貝建構函式 

}  template

doublelist::~doublelist() 

template

void doublelist::createinserthead()//頭插法 

}  }  template

void doublelist::createinsertrear()//尾插法 

}  template

void doublelist::initlist()//生成頭結點,尾部設定為null 

template

bool doublelist::isempty() 

else 

}  template

int doublelist::length() 

template

void doublelist::destorylist() 

head = null; 

m_length = 0; 

}  template

void doublelist::getfirstdata(type& firstdata) 

}  template

void doublelist::find(const type finddata) 

if (current == null) 

else 

m_length++; 

}  template

void doublelist::insertlast(const type newdata) 

newnode->next = current->next; 

current->next = newnode; 

newnode->prior = current; 

m_length++; 

}  template

void doublelist::insertbefore(const int pos,const type newdata) 

newnode->next = current->next; 

current->next = newnode; 

newnode->prior = current; 

newnode->next->prior = newnode; 

m_length++; 

}        

}  template

void doublelist::insertafter(const int pos,const type newdata) 

newnode->next = current->next; 

current->next = newnode; 

newnode->prior = current; 

newnode->next->prior = newnode; 

m_length++; 

}        

}  template

void doublelist::deletenode(const type deletedata) 

if (current==null) 

precurrent->next = current->next; 

delete current; 

cout<

void doublelist::deletenode(const int pos,type& deletedata) 

current = precurrent->next; 

deletedata = current->data; 

if (current->next != null) 

precurrent->next = current->next; 

delete current; 

cout<<"位置為"<

void doublelist::reverse() 

current = nextcurrent; 

}  }    

}  template

const doublelist& doublelist::operator=(const doublelist&otherlist) 

node*current = head; 

node*otherlistcurrent = otherlist.head->next; 

while(otherlistcurrent!=null) 

m_length = otherlist.m_length; 

}  return *this; 

}  template

ostream& operator<< <>(ostream& cout,const doublelist& list) 

cout<

void doublelist::print() 

coutcurrent = current->prior; 

}  cout<*list = new doublelist; 

list->createinserthead(); 

cout<<"list:"<<*listlist->createinsertrear(); 

cout<<"list:"<<*listcout<<"第乙個元素是:"list->insertfirst(45); 

list->insertlast(50); 

list->insertbefore(3,2012); 

list->insertafter(4,719); 

list->deletenode(5); 

int deletedata; 

list->deletenode(4,deletedata); 

cout<<"list:"<<*list<*copylist = new doublelist(*list); 

cout<<"copylist:"<<*copylistcopylist = list; 

cout<<"copylist:"<<*copylistsystem("pause"); 

return 0; 

雙向鍊錶實現

template class link link link pre null,link ne null void operator new size t void operator delete void ptr template link link freelist null template v...

雙向鍊錶實現

雙向鍊錶的應用背景主要是單向鍊錶只能順序訪問,逆序訪問單向鍊錶 尤其是較大的單向鍊錶是一件極其費時費力的工作 相比於單向鍊錶,雙向鍊錶增加了乙個域,這個域裡面增加了乙個指向前驅節點的指標,使得整個鍊錶可以順序訪問或者逆序訪問,來去自如 定義的雙向鍊錶的標頭檔案,裡面包含了雙向鍊錶的宣告和資料型別的定...

實現雙向鍊錶

雙向鍊錶和單向鍊錶相比更加靈活,它的每乙個元素除了本身的值以為擁有兩個指標,分別指向上乙個和下乙個節點。維護成本上要高於單向鍊錶。鍊錶的大部分操作依賴於遍歷,這一方面雙向鍊錶會效率會好一些,可以根據查詢下標的位置從而選擇從煉表頭開始遍歷還是從鍊錶尾開始遍歷。返回元素個數 public intsize...