双向链表模板类
dlist.h
#ifndef DLIST_H
#define DLIST_H
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template<typename T>
class DList
{
struct node{
T data;
node* next;
node* prev;
};
node *head = NULL;
node *tail = NULL;
int len;
public:
DList(){
len = 0;
}
DList(const DList &l){
len = 0;
if(l.head!=NULL){
node *pHc = l.head;
head = new node();
head->data = pHc->data;
node *pH = head;
len++;
while(pHc!=l.tail){
pHc = pHc->next;
pH->next = new node();
pH->next->prev = pH;
pH = pH->next;
pH->data = pHc->data;
len++;
}
}
else{
head=tail=NULL;
}
}
DList& operator = (const DList &l){
len = 0;
if (this == &l){
return *this;
}
len = 0;
if(l.head!=NULL){
node *pHc = l.head;
head = new node();
head->data = pHc->data;
node *pH = head;
len++;
while(pHc!=l.tail){
pHc = pHc->next;
pH->next = new node();
pH->next->prev = pH;
pH = pH->next;
pH->data = pHc->data;
len++;
}
}
else{
head=tail=NULL;
}
return *this;
}
~DList(){
node *bgn = head;
while(head!=tail)
{
head = head->next;
delete bgn;
bgn = head;
}
len = 0;
}
int size(){
return len;
}
void print(){
for(int i=0;i<len;i++){
cout<<at(i)<<" ";
}
cout<<endl;
}
void push_back(T data){
if(head==NULL){
head = new node();
head->data = data;
len++;
tail = head;
}
else{
tail->next = new node();
tail->next->data = data;
len++;
tail->next->prev = tail;
tail = tail -> next;
}
return;
}
T at(int index){
node *p;
p = head;
if(index>=len)
throw out_of_range("in at(int index) index out of range");
else{
for(int i=0;i<index;i++)
p = p->next;
}
return p->data;
}
int indexOf(T _data){
int index = 0;
node *p = head;
while(p->data != _data){
p = p->next;
index++;
}
if(index >= len)
return -1;
else
return index;
}
void removeAt(int index){
node *p;
p = head;
if(index >= len)
throw out_of_range("in removeAt(int index) index out of range");
else{
for(int i=0;i<index;i++){
p = p->next;
}
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
p = NULL;
len--;
}
return;
}
void clear(){
node *bgn = head;
while(head!=tail)
{
head = head->next;
delete bgn;
bgn = head;
}
len = 0;
}
void swap(int i, int j){
T vi = at(i);
T vj = at(j);
node *p;
p = head;
for(int k=0;k<i;k++)
p = p->next;
p->data = vj;
p = head;
for(int k=0;k<j;k++)
p = p->next;
p->data = vi;
}
int Partition(int low, int high){
T pivotkey;
pivotkey = at(low);
while(low < high){
while(low < high && at(high) >= pivotkey)
high--;
swap(low, high);
while(low < high && at(low) <= pivotkey)
low++;
swap(low, high);
}
return low;
}
void QSort(int low, int high){
int pivot;
if(low < high){
pivot = Partition(low,high);
QSort(low,pivot-1);
QSort(pivot+1,high);
}
}
void QuickSort(){
QSort(0,len-1);
}
};
#endif
测试类型为 DList< std::string >
main.cpp
#include <iostream>
#include "dlist.h"
int main(int argc, char** argv) {
try {
DList<string> list;
list.push_back("a");
list.push_back("b");
list.push_back("c");
list.push_back("d");
list.push_back("e");
list.push_back("f");
list.push_back("g");
list.push_back("h");
cout<<"【测试 push_back】 ";
list.print();
DList<string> list2(list);
cout<<"【测试 拷贝构造函数】 ";
list2.print();
DList<string> list3 = list;
cout<<"【测试 运算符重载函数】 ";
list3.print();
cout<<"【测试 indexOf()】 ";
cout<<list3.indexOf("c")<<endl;
cout<<"【测试 swap(0,1)】 ";
list3.swap(0,1);
list3.print();
cout<<"【测试 QuickSort】 ";
list3.QuickSort();
list3.print();
cout<<"【测试 removeAt(4)】 ";
list3.removeAt(4);
list3.print();
cout<<"【测试 clear】 ";
list3.clear();
list3.print();
}
catch (exception const& ex) {
cerr << "Exception: " << ex.what() <<endl;
return -1;
}
return 0;
}
运行结果