双向链表的简单实现
2017-07-30 21:10
267 查看
代码实现
测试代
4000
码
#include<iostream> #include<cassert> using namespace std; typedef int DataType; struct ListNode { ListNode* _next; ListNode* _prev; DataType _data; ListNode(DataType x) :_data(x) , _next(NULL) , _prev(NULL) {} }; class List { typedef ListNode Node; private: Node* _head; Node* _tail; 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) { List tmp(l); Swap(tmp); } return *this; } void Swap(List& l) { swap(_head, l._head); swap(_tail, l._tail); } ~List() { if (_head != NULL) { Node* del = _head; _head = _head->_next; delete del; } } void PushBack(DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } else { Node* tmp = new Node(x); _tail->_next = tmp; tmp->_prev = _tail; _tail = tmp; } } void PopBack() { if (_head == NULL) { cout << NULL << endl; } else if (_head->_next == NULL) { delete _tail; _tail = NULL; _head = NULL; cout << NULL << endl; } else { Node* cur = _tail; cur->_prev->_next = NULL; _tail = cur->_prev; delete cur; } } void PushFront(DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } else { Node* tmp = new Node(x); tmp->_next = _head; _head->_prev = tmp; _head = tmp; } } void PopFront() { if (_head == NULL) { cout << NULL << endl; } else if (_head->_next == NULL) { delete _tail; _tail = NULL; _head = NULL; cout << NULL << endl; } else { Node* cur = _head; _head = _head->_next; delete cur; _head->_prev = NULL; } } // 在pos的前面插入一个 void Insert(Node* pos, DataType x) { assert(pos); if (pos == _head) { PushFront(x); } else if (pos == _tail) { PushBack(x); } else { Node* tmp = new Node(x); Node* prev = pos->_prev; Node* next = pos->_prev->_next; prev->_next = tmp; tmp->_next = next; next->_prev = tmp; tmp->_prev = prev; } } void Erase(Node* pos) { if (pos==_head) { PopFront(); } else if (pos==_tail) { PopBack(); } else { Node* prev = pos->_prev; Node* next = pos->_next; delete pos; prev->_next = next; next->_prev = prev; } } Node* Find(DataType x) { Node* cur = _head; while (cur) { if (cur->_data == x) return cur; cur = cur->_next; } return NULL; } void Reverse() { Node* begin = _head; Node* end = _tail; while ((begin != end)&&(begin->_prev != end)) { swap(begin->_data, end->_data); begin = begin->_next; end = end->_prev; } } void Print() { if (_head != NULL) { Node* cur = _head; while (cur) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; } else { cout << NULL << endl; } } };
测试代
4000
码
#define _CRT_SECURE_NO_WARNINGS #include"List.h" void Test() { List l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); l1.Reverse(); l1.Print(); //l1.Insert(l1.Find(4), 8); //l1.Print(); /*l1.PushFront(6); l1.Print(); l1.PopFront(); l1.PopFront(); l1.PopFront(); l1.PopFront(); l1.Print(); */ /*List l2; l2 = l1; l2.Print(); List l3(l1); l3.Print(); */ //l1.PopBack(); //l1.Print(); } int main() { Test(); system("pause"); return 0; }
相关文章推荐
- 简单数据结构之双向链表(C++实现)
- C++实现简单的双向链表
- [java数据结构]--java双向链表LinkedList的简单实现
- 双向链表的简单实现丶
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- STL中双向链表的简单实现
- 数据结构复习 之 一个简单双向链表的实现
- C++ 双向链表简单实现通讯录
- 几种基本数据结构--栈、队列、双向链表、有根树的分析和简单实现
- 双向链表的简单实现
- 简单双向链表(C++模版技术实现)
- JAVA实现最简单的双向链表、队列与栈(内部使用链表形式存储)
- 简单实现 linkedList 双向链表
- (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作
- (C++版)链表(三)——实现双向链表的创建、插入、删除等简单操作
- STL之双向链表及其迭代器简单实现
- 简单易懂的C语言实现双向链表代码
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作