基于c++11编写的双向链表
实现:dlist.h
#ifndef DLIST_H
#define DLIST_H
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
typedef int ElemData;
struct node{
ElemData data;
node* next;
node* prev;
};
class DList
{
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(ElemData 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;
}
ElemData 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(ElemData _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){
int vi = at(i);
int 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(DList *l,int low, int high){
int pivotkey;
pivotkey = l->at(low);
while(low < high){
while(low < high && l->at(high) >= pivotkey)
high--;
swap(low, high);
while(low < high && l->at(low) <= pivotkey)
low++;
swap(low, high);
}
return low;
}
void QSort(DList *l,int low, int high){
int pivot;
if(low < high){
pivot = Partition(l,low,high);
QSort(l,low,pivot-1);
QSort(l,pivot+1,high);
}
}
void QuickSort(){
QSort(this,0,len-1);
}
};
#endif
调用: main.cpp
#include <iostream>
#include "dlist.h"
int main(int argc, char** argv) {
try {
DList list;
list.push_back(1);
list.push_back(9);
list.push_back(9);
list.push_back(4);
list.push_back(0);
list.push_back(8);
list.push_back(0);
list.push_back(1);
cout<<"【测试 push_back】 ";
list.print();
DList list2(list);
cout<<"【测试 拷贝构造函数】 ";
list2.print();
DList list3 = list;
cout<<"【测试 运算符重载函数】 ";
list3.print();
cout<<"【测试 indexOf(4)】 ";
cout<<list3.indexOf(4)<<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;
}
输出:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0f76c27a64b95123538cbabbfe91d0de.png)