C++实现双链表
来源:互联网 发布:法本法硕备考经验知乎 编辑:程序博客网 时间:2024/05/15 02:24
C++实现双链表
#include<iostream>#include<assert.h>using namespace std;typedef int DataType;struct ListNode{ DataType _data; ListNode *_next; ListNode *_prev; ListNode(const DataType x) :_data(x), _next(NULL), _prev(NULL) {}};class List{ typedef ListNode Node;public: List() :_head(NULL), _tail(NULL) { } 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 = _tail->_next; } } 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 Display() { if (_head == NULL) return; else { Node* cur(_head); while (cur != NULL) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; } } void PopBack() { if (_head == NULL||_head==_tail) { delete _tail; _head = _tail = NULL; } else { _tail = _tail->_prev; delete _tail->_next; } } void PopFront() { if (_head == NULL || _head == _tail) { delete _tail; _head = _tail = NULL; } else { _head = _head->_next; delete _head->_prev; } } void Destroy() { if (_head == _tail) { delete _tail; _head = _tail = NULL; } else { while (_head->_next != NULL) { _head = _head->_next; delete _head->_prev; } delete _tail; _head = _tail = NULL; } } Node* Find(DataType x) { if (_head == NULL) return NULL; else { Node *cur(_head); while (cur != NULL) { if (cur->_data == x) { break; } cur = cur->_next; } return cur; } } void Insert(Node* pos, DataType x) { assert(pos); if (pos == _head) { PushFront(x); } else { Node *tmp = new Node(x); tmp->_next = pos; pos->_prev->_next = tmp; tmp->_prev = pos->_prev; pos->_prev = tmp; } } void Erase(Node* pos) { assert(pos); if (pos == _head) { _head = _head->_next; delete pos; } else { pos->_next->_prev = pos->_prev; pos->_prev->_next = pos->_next; delete pos; pos = NULL; } } void Reverse()//定义了一个NewHead 让其从第一个一直走到最后一个,将其再给_head { if (_head == _tail) { return; } else { ListNode* cur = _head; ListNode* tmp = cur; ListNode* NewHead = NULL; while (cur) { tmp = cur; cur = cur->_next; tmp->_next = NewHead; tmp->_prev = cur; NewHead = tmp; } _head = NewHead; } }private: Node *_head; Node *_tail;};void test(){ List l1; l1.PushBack(0); l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); /*l1.PushFront(2); l1.PushFront(1); l1.PushFront(0);*/ /*l1.PopBack(); l1.PopBack(); l1.PopBack();*/ //l1.PopFront(); //l1.PopFront(); //l1.PopFront(); //l1.Destroy(); //l1.Find(1); //l1.Insert(l1.Find(5), 9); //l1.Insert(l1.Find(1), 6); //l1.Insert(l1.Find(3), 5); //l1.Insert(l1.Find(0), 9); //l1.Erase(l1.Find(1)); //l1.Erase(l1.Find(10)); l1.Reverse(); l1.Display();}int main(){ test(); system("pause"); return 0;}
0 0
- C语言实现双链表
- C实现双链表
- C实现循环双链表
- c语言实现双链表
- c语言实现双链表
- c 双链表的实现
- C语言实现双链表
- 双链表 C语言实现
- C语言实现双链表
- 双链表(C实现)
- C语言实现双链表
- c语言实现双链表
- c语言实现双链表
- 一个双链表的C实现
- 一个双链表的C实现
- C语言实现一个双链表
- 双链表的C语言实现
- C语言 实现一个双链表
- 二、数据存储
- tensorflow线性模型以及Wide deep learning
- 完数
- 京东实习笔试——拍卖
- Spark常用函数之RDD基本转换
- C++实现双链表
- 0409 css基本写法
- 华为上机题之提取不重复的句子
- PE 题目 NBUT 1226A Simple Triangle
- nVIDIA Jetson TX1 关闭启动时串口日志方法
- 三、数据库入门和listview的基本使用
- JAVA的数据类型
- 模板实现链表队列
- 亲密数