c++实现单链表,双向链表,顺序表的接口(非模板)
2017-07-18 22:33
369 查看
在以前使用c实现单链表的时候,没有使用函数的复用去完成相应的接口操作,这次使用尽量多的复用去完成链表和顺序表的操作(包括故意使用insert()去复用pushback()这个逻辑是错的,只是为了 体现复用的简便)
单链表
struct SListNode { SListNode* _next; DataType _data; SListNode(DataType x) :_data(x) ,_next(NULL) {} }; class SList { typedef SListNode Node; public: SList() :_head(NULL) , _tail(NULL) {} SList(const SList& s) :_head(NULL) , _tail(NULL) { Node*cur = s._head; while (cur->_next) { PushBack(cur->_data); cur = cur->_next; } } SList& operator=(const SList& s) { Node*tmp=_head; if(tmp) { tmp=tmp->next; delete _head; _head=tmp; } _head=NULL; Node*cur = s._head; while (cur->_next) { PushBack(cur->_data); cur = cur->_next; } } ~SList() { while (_head != _tail) { Node*tmp = _head; delete[] _head; _head = tmp; } delete[] _tail; _head = _tail = NULL; } void PushBack(DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } _tail->_next = new Node(x); _tail = _tail->_next; } void PopBack() { if (_head == NULL) return; else if (_head = _tail) { delete _head; _tail = _head = NULL; } else { Node*tmp = _head; while (tmp->_next->_next) { tmp = tmp->_next; } delete[]_tail; _tail = tmp; _tail->_next = NULL; } } void PushFront(DataType x) { Node*tmp = new Node(x); tmp->_next = _head; _head = tmp; } void PopFront() { if (_head == NULL) return; else if (_head = _tail) { delete _head; _tail = _head = NULL; } else { Node*tmp = _head->_next; delete[] _head; _head = tmp; } } // 插入一个节点在pos的前面 void Insert(Node* pos, DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } else if (_head == _tail) { _head = new Node(x); _head->_next = _tail; } else { Node *tmp = _head; while (tmp->_next != pos) { tmp = tmp->_next; } Node*tail = new Node(x); tmp->_next = tail; tail->_next = pos; } } void Erase(Node* pos) { if (_head == NULL) return; else if (_head = _tail) { delete[] _head; _head = _tail = NULL; } else if (pos->_next == NULL) { PopBack(); } Node *tmp = _head; while (tmp->_next != pos) { tmp = tmp->_next; } tmp->_next = pos->_next; delete[] pos; } void Print() { Node*tmp = _head; while (tmp) { cout << tmp->_data; } cout << endl; } private: Node* _head; Node* _tail; };
双向链表
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; public: List() : _head(NULL) , _tail(NULL) {} List(const List&s) :_head(NULL) ,_tail(NULL) { Node*cur = s._head; while (cur->_next) { PushBack(cur->_data); cur = cur->_next; } } List& operator=(const List& s) { SList(const SList& s) :_head(NULL) , _tail(NULL) { Node*cur = s._head; while (cur->_next) { PushBack(cur->_data); cur = cur->_next; } } SList& operator=(const SList& s) { Node*tmp=_head; if(tmp) { tmp=tmp->next; delete _head; _head=tmp; } _head=NULL; Node*cur = s._head; while (cur->_next) { PushBack(cur->_data); cur = cur->_next; } } ~List() { while (_head != _tail) { Node*tmp = _head; delete[] _head; _head = tmp; } delete[] _tail; _head = _tail = NULL; } void PushBack(DataType x) { Insert(_tail, x); } void PopBack() { Erase(_tail); } void PushFront(DataType x) { Insert(_head, x); } void PopFront() { Erase(_head); } // 在pos的前面插入一个 void Insert(Node* pos, DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } else if (_head == _tail) { _head = new Node(x); _head->_next = _tail; _tail->_prev = _head; } else { Node*tmp = new Node(x); Node*prev = pos->_prev; prev->_next = tmp; tmp->_prev = prev; tmp->_next = pos; pos->_prev = tmp; } } void Erase(Node* pos) { if (_head == NULL) return; else if (_head = _tail) { delete[] _head; _head = _tail = NULL; } else if (pos->_next == NULL) { Node *tmp = pos->_prev; delete[] _tail; _tail = tmp; } else if (pos->_prev == NULL) { Node *tmp = pos->_next; delete _head; _head = tmp; } else { Node*prev = pos->_prev; Node*next = pos->_next; prev->_next = next; next->_prev = prev; delete[] pos; } } Node* Find(DataType x) { Node*tmp = _head; while (tmp) { if (tmp->_data == x) { return tmp; } tmp = tmp->_next; } return NULL; } void Reverse() { Node *tmp = _head; Node *cur = _head; while (_head) { cur->_next = _head->_prev; cur->_prev = _head->_next; _head = _head->_next; } _tail = tmp; } private: Node* _head; Node* _tail; };
顺序表
typedef int DataType; class SeqList { public: SeqList(size_t size=1) { _array = (DataType*)malloc(sizeof(size)*size); _size = size; _capacity = size; } SeqList(const SeqList& s) { _array = (DataType*)malloc(_size); for (size_t i = 0; i++; i < _size) { _array[i] = s._array[i]; } _size = s._size; _capacity = s._size; } ~SeqList() { free (_array); } void Swap(SeqList s) { swap(_array, s._array); _size = s._size; _capacity = s._capacity; } void PushBack(DataType x) { CheckCapcacity(); _array[_size] = x; _size++; } void PopBack() { _size--; } void PushFront(DataType x) { CheckCapcacity(); for (size_t i = _size; i--; i > 0) { _array[_size] = _array[_size - 1]; } _array[0] = x; } void PopFront() { for (int i = 1; i++; i < _size) { _array[i] = _array[i - 1]; } _size--; } void Insert(int pos, DataType x) { CheckCapcacity(); if (pos == 0) { PushFront(x); } else { for (int i = _size; i--; i > pos) { _array[i] = _array[i - 1]; } _array[pos] = x; } _size++; } void Erase(size_t pos) { if (pos == _size - 1) PopBack(); for (int i = pos; i++;i<_size) { _array[i] = _array[i + 1]; } _size--; } DataType& operator[](size_t pos) { return _array[pos]; } void CheckCapcacity() { if (_size = _capacity) { DataType*tmp = new DataType(_capacity * 2); for (size_t i = 0; i <= _size; i++) { tmp[i] = _array[i]; } swap(tmp, _array); _capacity *= 2; } } void Print() { for (size_t i = 0; i++; i < _size) { cout<<_array[i]; } cout << endl; } private: DataType* _array; size_t _size; size_t _capacity; };
相关文章推荐
- C++模板及用模板实现双向循环链表,顺序表
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- C++实现顺序表及双向链表
- 【C++】实现顺序表、单链表和双向链表
- c++模板实现双向链表
- C++模板实现双向循环链表(有带头结点)
- C++模板实现双向链表
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- c++实现顺序表、单链表和双向链表
- C++ 学习练手 - 双向链表的模板实现
- 【C++】模板实现带头节点的双向循环链表
- 用模板实现顺序表和带头结点的双向循环链表
- c++模板实现双向链表
- C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
- C++实现双向链表
- C++利用链表与模板实现栈
- 用C++实现双向链表
- 双向链表的相关操作C++实现
- 数据结构(2)单链表 c++ 模板实现
- 模板实现双向链表