单链表和双链表的实现
来源:互联网 发布:ubuntu uefi 引导修复 编辑:程序博客网 时间:2024/06/05 05:23
单链表的实现
#include<iostream>#include<assert.h>using namespace std;typedef int DataType;struct sListNode{ sListNode*_next; DataType _data; sListNode(DataType x) : _data(x) , _next(NULL) {}};typedef sListNode Node;class sList{public: sList() :_head(NULL) , _tail(NULL) {} sList(const sList &s) :_head(NULL) , _tail(NULL) { Copy(s); } ~sList() { Destory(); } sList &operator=(const sList &s) { Destory(); Copy(s); return *this; } void Copy(const sList &s) { Node*cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } void Destory() { { Node*cur = _head; while (_head != NULL) { cur = _head; _head = cur->_next; delete cur; } _head = _tail = NULL; } } void PushBack(DataType x)//没有节点 一个节点 两个节点 { if ((_head == NULL) && (_tail == NULL)) { _head = _tail = new Node(x); } else { _tail->_next = new Node(x); _tail = _tail->_next; } } void PopBack()//没有节点 有节点 { if ((_head == NULL) && (_tail==NULL)) { return; } else { Node*cur = _head; while (cur != NULL) { cur = cur->_next; } delete cur; } _head = _tail = NULL; } void PushFront(DataType x)//没有数据 有数据 { if ((_head == NULL) && (_tail == NULL)) { _head = _tail = new Node(x); } else { Node*cur = new Node(x); cur->_next = _head; _head=cur; } } void PopFront()//没有节点 有节点 { if ((_head == NULL) && (_tail == NULL)) { return; } else { Node*cur = _head; _head = _head->_next; delete cur; _head = _tail = NULL; } } Node* Find(DataType x) { Node*tmp = _head; while (tmp) { if ((tmp->_data) == x) return tmp; tmp = tmp->_next; } } void Insert(Node* pos, DataType x) { assert(pos); if (pos == 0) { PushBack(x); } else { Node*cur = _head; while (cur->_next != pos) { cur = cur->_next; } Node*tmp = new Node(x); cur->_next = tmp; tmp->_next = pos; } } void Erase(Node*pos) { assert(pos); if (pos == 0) { PopFront(); } else if (pos->_next == NULL) { PopBack(); } else { Node*cur = _head; while (cur->_next != pos) { cur = cur->_next; } Node*tmp = cur->_next; cur->_next = tmp->_next; delete tmp; } } void Print() { Node*cur = _head; while (cur != NULL) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; }private: Node* _head; Node*_tail;};void TestsList(){ sList s; s.PushBack(1); s.PushBack(2); s.PushBack(3); s.PushBack(4); s.Print(); s.PopBack(); s.PopBack(); s.PopBack(); s.PopBack(); s.Print(); s.PushFront(4); s.PushFront(3); s.PushFront(2); s.PushFront(1); s.Print(); s.PopFront(); s.PopFront(); s.PopFront(); s.PopFront(); s.Print(); s.PushBack(1); s.PushBack(2); s.PushBack(3); s.PushBack(4); s.Print(); Node*tmp = s.Find(3); s.Insert(tmp, 3); s.Print(); s.Erase(tmp); s.Print();}int main(){ TestsList(); system("pause"); return 0;}
双链表的实现
#include<iostream>#include<assert.h>using namespace std;typedef int DataType;struct ListNode{ ListNode* next; ListNode* prev; DataType data; ListNode(DataType x) :next(NULL) , prev(NULL) , data(x) {}};typedef ListNode Node;class List{public: List() :_head(NULL) , _tail(NULL) {} List(const List &L) :_head(NULL) , _tail(NULL) { copy(L); } void copy(const List &L) { Node*cur = L._head; while (cur) { PushBack(cur->data); cur = cur->next; } } List &operator=(const List &L) { if (this != &L) { Destory(); copy(L); return *this; } } void Destory() { if (_head == NULL) { Node*cur = _head; while (_head) { Node*cur = _head; cur = cur->next; delete cur; } } } ~List() { Destory(); } void PushBack(DataType x) { if (_head == NULL) { Node *tmp = new Node(x); tmp->next = tmp->prev=NULL; _head = _tail = tmp; } else { Node*tmp = new Node(x); _tail->next = tmp; tmp->prev = _tail; _tail = tmp; } } void PopBack() { if (_head == NULL) { return; } else if (_head->next==NULL) { delete _head; _head = _tail = NULL; } else { Node*tmp =_tail; _tail = _tail->prev; _tail->next = NULL; delete tmp; } } 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) { return; } else if (_head->next == NULL) { _head = _tail = NULL; } else { Node*tmp = _head; _head = _head->next; _head->prev = NULL; delete tmp; } } void Insert(Node* pos, DataType x) { assert(pos); if ((pos == _head)||(pos->prev==NULL)) { PushFront(x); } else { Node*tmp = new Node(x); Node*front = pos->prev; tmp->prev = front; tmp->next = pos; front->next = tmp; pos->prev = tmp; } } void Erase(Node* pos) { assert(pos); if (pos ==0||pos->prev) { PopFront(); } else if (pos == _tail||pos->next==NULL) { PopBack(); } else { Node*Front = pos->prev; Node*Last = pos->next; Front->next = Last; Last->prev = Front; delete pos; } } void Reserve() { Node*tmp = _head; while (tmp) { swap(tmp->prev, tmp->next); tmp = tmp->prev; } swap(_head, _tail); } Node* Find(DataType x) { Node*tmp = _head; while(tmp!=NULL) { if (tmp->data == x) { return tmp; tmp = tmp->next; } } return NULL; } void Print() { Node*cur = _head; while (cur) { cout << cur->data << " "; cur=cur->next; } cout << endl; }private: Node* _head; Node* _tail;};void TestList1(){ List L; L.PushBack(1); L.PushBack(2); L.PushBack(3); L.PushBack(4); L.Print(); L.PopBack(); L.PopBack(); L.PopBack(); L.PopBack(); L.Print(); L.PushFront(4); L.PushFront(3); L.PushFront(2); L.PushFront(1); L.Print(); L.PopFront(); L.PopFront(); L.PopFront(); L.PopFront(); L.Print();}void TestList2(){ List L; L.PushFront(5); L.PushFront(4); L.PushFront(2); L.PushFront(1); L.Find(3); Node*tmp = L.Find(3); L.Insert(tmp,3); L.Print();}int main(){ //TestList1(); TestList2(); system("pause"); return 0;}
阅读全文
0 0
- 单链表和双链表的实现
- 双链表的表示和实现
- 单链表的实现和练习
- 单链表的设计和实现
- 单链表的表示和实现
- 编程实现单链表和双链表的插入节点
- Java实现单链表和双链表
- Java实现单链表和双链表
- 双链表的插入和删除实现
- 单链表的实现和基本操作
- 单链表的插入和删除 C++实现
- 单链表的表示和实现 - 数据结构
- 线性单链表的实现和操作
- C++实现单链表的创建和打印
- 单链表实现多项式的存储和加法
- 单链表的头插法和尾插法实现代码
- 单链表的建立,实现和操作
- 单链表和顺序表的实现
- iOS崩溃 捕获异常处理(二)
- HTTP请求的参数详解
- Ubuntu上搭建robotframework
- 城市列表
- Openlayers实现WFS服务及单击要素查询
- 单链表和双链表的实现
- Mac 修改hostname&computer name
- Typescript+dva搭建开发环境
- Java中的内存处理机制和final、static、final static总结
- 线段树--poj2828 Buy tickets
- Jenkins中节点运行job文件的传递
- 三层交换机 VS 二层交换机
- Maven多模块项目搭建
- LuoguP1196_银河英雄传说_KEY