模板 鍊錶模板 有序鍊錶模板及測試

2021-04-21 17:21:37 字數 2984 閱讀 8738

//鍊錶模板《c++程式設計——資料結構與程式設計方法》16.2作為抽象資料型別的鍊錶

//header file:linkedlist.h

#ifndef _linkedlist_h

#define _linkedlist_h

template

struct nodetype

;template

class linkedlisttype

;template

bool linkedlisttype::isemptylist()

template

bool linkedlisttype::isfulllist()

template

linkedlisttype::linkedlisttype()

template

void linkedlisttype::destroylist()

last=null;

}template

void linkedlisttype::initializelist()

template

void linkedlisttype::print()

}//鍊錶的長度

template

int linkedlisttype::length()

return count;

}//檢索第乙個節點中的資料

template

void linkedlisttype::retrievefirst(type& firstelement)

//查詢鍊錶

template

void linkedlisttype::search(const type& item)

//插入最後乙個節點

template

void linkedlisttype::insertlast(const type& newitem)

else

}//刪除節點

template

void linkedlisttype::deletenode(const type& deleteitem)

else

else

found=true;

}if(found)

else

cout<<"item to be deleted is not in the list."template

linkedlisttype::~linkedlisttype()

last=null;

}//拷貝建構函式

template

linkedlisttype::linkedlisttype(const linkedlisttype& otherlist)

//形參是物件的常引用,在函式中不能改變實參的值

else}}

//過載賦值運算子

template

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

else}}

return *this;

}#endif

//有序鍊錶模板——《c++程式設計——資料結構與程式設計方法》16.3有序鍊錶

//header file:orderedlist.h

#ifndef _orderedlist_h

#define _orderedlist_h

#include

#include "linkedlist.h"

using namespace std;

//有序鍊錶通常進行下面的操作

//1.初始化鍊錶

//2.檢查鍊錶是否為空

//3.檢查甸表是否為滿

//4.列印鍊錶

//5.逆向列印鍊錶

//6.刪除鍊錶

//7.在鍊錶中查詢給定的元素

//8.在鍊錶中插入乙個元素

//9.在鍊錶中刪除乙個元素

//10.查詢鍊錶長度

//11。拷貝鍊錶

//有序鍊錶與一般鍊錶很類似,可以從類linkedlisttype派生

//需要改變查詢、插入和刪除操作的演算法

template

class orderedlinkedlisttype:public linkedlisttype

;//查詢節點

template

void orderedlinkedlisttype::search(const type& item)

if(current==first)

else}}

//刪除節點

template

void orderedlinkedlisttype::deletenode(const type& deleteitem)

if(current==null)

cout<<"item to be delete is not in the list."else

}else

cout<<"item to be delete is not in the list."template

void orderedlinkedlisttype::printlistreverse() const

}#endif

//測試程式,測試有序鍊錶中的各種操作《c++程式設計——資料結構與程式設計方法》16.3有序鍊錶

//實現檔案:testorderedlist.cpp

#include

#include "orderedlist.h"

using namespace std;

int main()

coutlist1.print();

coutcout<<"line 16: list 2: ";

list2.print();

coutcin>>num;

coutcout<<"line 23: after deleteing the onde, list 2: "}

模板順序鍊錶

對於模板這個東西,我感覺好像概念清楚,但一直沒機會動手寫一寫。今天終於動手了,寫了才知道自己還是有很多相關的東西不知道的。今天寫了乙個模板順序鍊錶,還花了不少時間,以後有機會將會寫更多的模板資料結構。下面的資料結構支援記憶體自動增長。有查詢,插入,刪除,賦值等簡單基本操作 ifndef afx xt...

模板雙向鍊錶

順序表和煉表幾乎是面試必考點,前面我們已經介紹過了模板順序表 用模板寫鍊錶主要還是為了實現 的型別通用性,以下 將實現鍊錶的增 刪 查 改 判空等操作。define crt secure no warnings 1 pragma once include include using namespac...

鍊錶類模板

include using namespace std class cnode 定義乙個節點類 template 定義類模板 class clist 定義clist類 type movetrail 獲取尾節點 return ptmp 返回尾節點 void addnode type pnode 新增節...