C++实现双向链表
2017-04-08 19:36
183 查看
C++实现双向链表
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
typedef int DataType;
typedef struct ListNode
{
DataType data;
ListNode* prev;
ListNode* next;
ListNode(const DataType d)
:data(d)
, prev(NULL)
, next(NULL)
{}
}Node;
class List
{
public:
List()
:_head(NULL)
, _tail(NULL)
{}
List(const List& l)
:_head(NULL)
, _tail(NULL)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->next;
}
}
/*List& operator=(const List& l)
{
if (this != &l)
{
Clear();
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->next;
}
}
return *this;
}
List& operator=(const List& l)
{
if (this != &l)
{
List tmp(l);
swap(_head, tmp._head);
swap(_tail, tmp._tail);
}
return *this;
}*/
List& operator=(List l)
{
if (this != &l)
{
Swap(l);
}
return *this;
}
~List()
{
Clear();
}
void PushBack(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_tail->next = tmp;
tmp->prev = _tail;
_tail = tmp;
_tail->next = NULL;
}
}
void PopBack()
{
if (_head == NULL)
{
cout << "链表为空,无法尾删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _tail;
_tail = _tail->prev;
_tail->next = NULL;
delete del;
}
}
void PushFront(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_head->prev = tmp;
tmp->next = _head;
_head = tmp;
_head->prev = NULL;
}
}
void PopFront()
{
if (_head == NULL)
{
cout << "链表为空,无法头删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->next;
_head->prev = NULL;
delete del;
}
}
void Insert(Node* pos, DataType d) //在pos前插入一个数据
{
assert(pos);
Node* tmp = new Node(d);
if (pos == _head)
{
PushFront(d);
}
else
{
pos->prev->next = tmp;
tmp->prev = pos->prev;
tmp->next = pos;
pos->prev = tmp;
}
}
void Erase(Node* pos) //删除pos位置的数据
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
delete pos;
}
}
Node* Find(DataType d)
{
Node* cur = _head;
while (cur)
{
if (cur->data == d)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
private:
void Clear()
{
Node* cur = _head;
while (cur)
{
Node* del = cur;
cur = cur->next;
delete[] del;
}
_head = _tail = NULL;
}
void Swap(List& l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
private:
Node* _head;
Node* _tail;
};
测试函数如下
#include "List.h"
void Test1()
{
List l1;
/*l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();*/
/*List l2(l1);
l2.Print();
List l3;
l3 = l1;
l3.Print();*/
/*l1.PopBack();
l1.Print();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
l1.PopBack();*/
l1.PushFront(4);
l1.PushFront(3);
l1.PushFront(2);
l1.PushFront(1);
l1.Print();
l1.PopFront();
l1.Print();
l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.Print();
l1.PopFront();
}
void Test2()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
Node* res = l1.Find(2);
/*l1.Insert(res, 0);
l1.Print();*/
l1.Erase(res);
l1.Print();
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
typedef int DataType;
typedef struct ListNode
{
DataType data;
ListNode* prev;
ListNode* next;
ListNode(const DataType d)
:data(d)
, prev(NULL)
, next(NULL)
{}
}Node;
class List
{
public:
List()
:_head(NULL)
, _tail(NULL)
{}
List(const List& l)
:_head(NULL)
, _tail(NULL)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->next;
}
}
/*List& operator=(const List& l)
{
if (this != &l)
{
Clear();
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->next;
}
}
return *this;
}
List& operator=(const List& l)
{
if (this != &l)
{
List tmp(l);
swap(_head, tmp._head);
swap(_tail, tmp._tail);
}
return *this;
}*/
List& operator=(List l)
{
if (this != &l)
{
Swap(l);
}
return *this;
}
~List()
{
Clear();
}
void PushBack(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_tail->next = tmp;
tmp->prev = _tail;
_tail = tmp;
_tail->next = NULL;
}
}
void PopBack()
{
if (_head == NULL)
{
cout << "链表为空,无法尾删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _tail;
_tail = _tail->prev;
_tail->next = NULL;
delete del;
}
}
void PushFront(DataType d)
{
Node* tmp = new Node(d);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_head->prev = tmp;
tmp->next = _head;
_head = tmp;
_head->prev = NULL;
}
}
void PopFront()
{
if (_head == NULL)
{
cout << "链表为空,无法头删" << endl;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->next;
_head->prev = NULL;
delete del;
}
}
void Insert(Node* pos, DataType d) //在pos前插入一个数据
{
assert(pos);
Node* tmp = new Node(d);
if (pos == _head)
{
PushFront(d);
}
else
{
pos->prev->next = tmp;
tmp->prev = pos->prev;
tmp->next = pos;
pos->prev = tmp;
}
}
void Erase(Node* pos) //删除pos位置的数据
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
delete pos;
}
}
Node* Find(DataType d)
{
Node* cur = _head;
while (cur)
{
if (cur->data == d)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
private:
void Clear()
{
Node* cur = _head;
while (cur)
{
Node* del = cur;
cur = cur->next;
delete[] del;
}
_head = _tail = NULL;
}
void Swap(List& l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
private:
Node* _head;
Node* _tail;
};
测试函数如下
#include "List.h"
void Test1()
{
List l1;
/*l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();*/
/*List l2(l1);
l2.Print();
List l3;
l3 = l1;
l3.Print();*/
/*l1.PopBack();
l1.Print();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
l1.PopBack();*/
l1.PushFront(4);
l1.PushFront(3);
l1.PushFront(2);
l1.PushFront(1);
l1.Print();
l1.PopFront();
l1.Print();
l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.Print();
l1.PopFront();
}
void Test2()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
Node* res = l1.Find(2);
/*l1.Insert(res, 0);
l1.Print();*/
l1.Erase(res);
l1.Print();
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}
相关文章推荐
- 双向循环链表的c++ 实现
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
- C++ List 双向链表 实现 会用也要会写
- 双向链表的相关操作C++实现
- 简单双向链表(C++模版技术实现)
- 数组、单链表和双链表介绍 和 双向链表的C/C++/Java实现
- C++ 学习练手 - 双向链表的模板实现
- (C++版)链表(三)——实现双向链表的创建、插入、删除等简单操作
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
- 构造双向链表根据访问频度动态调整位置_C++实现
- 双向链表的C++实现 Implement of Doubly Linked List
- 双向链表之C++实现
- c++双向链表的实现_1.0版本
- 双向循环链表插入算法的C++程序实现
- 双向链表的相关操作C++实现
- 多文件编程动态开辟空间实现双向链表的应用 c++版
- 双向链表的C++实现
- 使用C++实现的双向链表
- 使用C++实现的双向链表