C++实现单链表
来源:互联网 发布:鼎捷软件股票 编辑:程序博客网 时间:2024/06/11 02:57
花了点时间再次把单链表,以及相关各种操作函数实现了。
#include<iostream>#include<assert.h>using namespace std;template<typename T>struct Node{ Node<T>* _next; T _value; Node(const T& value = T()) :_value(value) , _next(NULL) {}};template<typename T>class LinkList{ typedef Node<T> Node; friend ostream& operator<<(ostream& os, const LinkList& l);public: LinkList() :_head(BuyNode()) {} ~LinkList() { DestoryList(_head); } LinkList(const LinkList& l) :_head(BuyNode()) { _Copy(_head, l); } //找第一个值为value的节点 //返回值这个看自己设计,这里我想让它除了可以判断是否在链表中,如果在的话返回前一个节点方便进行插入 pair<Node*, bool> find(const T& value) { assert(_head->_next); Node* cur = _head; while (cur) { Node* prev = cur; cur = cur->_next; if (cur!=NULL&&cur->_value == value) { return make_pair(prev, true); } } //走到这里证明没找到,直接返回就行 return make_pair(cur, false); } //这里通过下标来插入 void insert(const T& value, int pos) { Node* cur = _head; while (--pos) { if (cur->_next == NULL) { cur->_next = BuyNode(value); return; } cur = cur->_next; } Node* newnode = BuyNode(value); newnode->_next = cur->_next; cur->_next = newnode; } //这里是通过节点位置来进行插入 void insert(const T& value,Node* pos) { assert(pos); Node* newnode = BuyNode(value); newnode->_next = pos->_next; pos->_next = newnode; } bool remove(const T& value)//这里就用到了刚才find的返回值 { pair<Node*, bool> ret = find(value); if (ret.second == false) return false; Node* prev = ret.first; Node* del = prev->_next; prev->_next = del->_next; delete del; return true; } void push_front(const T& value) { Node* cur = BuyNode(value); cur->_next = _head->_next; _head->_next = cur; } void pop_front() { Node* del = _head->_next; if (NULL != del) { _head->_next = del->_next; } else { return; } delete del; } void push_back(const T& value) { Node* cur = _head; while (cur->_next) { cur = cur->_next; } cur->_next = BuyNode(value); } void pop_back() { Node* cur = _head; Node* parent = NULL; while (cur->_next) { parent = cur; cur = cur->_next; } delete cur; parent->_next = NULL; } T& operator[](int pos) { Node* cur = _head->_next; while (--pos >= 0) { assert(cur != NULL); cur = cur->_next; } return cur->_value; } LinkList& operator=(const LinkList& l) { this->erase(); _Copy(_head, l); return *this; } Node* GetHead() //给外界实现一个接口 { return _head; } size_t size() { size_t count = 0; Node* cur = _head; //不包括头节点 while (cur=cur->_next) { count++; } return count; } bool erase() { return DestoryList(_head); } bool empty() { return NULL == _head->_next; }protected: bool DestoryList(Node* node) { Node* cur = node->_next; if (cur == NULL) return false; node->_next = NULL; while (cur) { Node* del = cur; cur = cur->_next; delete del; } return true; } void _Copy(Node* head,const LinkList<T>& l) { Node* cur = l._head->_next; Node* newnode = head; while (cur) { newnode->_next = BuyNode(cur->_value); newnode = newnode->_next; cur = cur->_next; } } Node* BuyNode(const T& value=T()) { Node* node = new Node(value); return node; }protected: Node* _head;};template<typename T>ostream& operator<<(ostream& os, LinkList<T>& l){ Node<T>* cur = l.GetHead(); while (cur = cur->_next) { os << cur->_value << " "; } os << endl; return os;}
随便实现了下,没有太认真。。。可能里面会有bug,有发现的可以说下,谢谢。
1 0
- 单链表(C#)实现
- 用C实现单链表
- C实现单链表
- C语言实现单链表
- 数据结构---单链表c实现
- C语言实现单链表
- 单链表的C实现
- C语言单链表实现
- 单链表实现(C++)
- C语言实现单链表
- 单链表C语言实现
- c语言单链表实现
- 单链表反转 c实现
- 单链表之C实现
- 单链表-C实现-初级
- 单链表 C语言实现
- 单链表实现-c
- 【C++】单链表的实现
- 安卓异常android.widget.TextView cannot be cast to android.widget.Button
- 希尔排序算法
- 硬盘变成RAW 修复
- invalid nib registered for identifier。。。nib must contain exactly one top level
- java类的初始化顺序
- C++实现单链表
- android在代码中设置View的属性
- 从误用TreeSet到思考Java有序集合对相等和顺序比较一致性的要求
- Putty 使用中出现的问题
- 折半查找,也称二分查找、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。
- 2017 华东师范大学网赛 F.丽娃河的狼人传说( 贪心)
- 子元素上的margin作用到了父元素上
- [设计模式]观察者模式(Observer)
- linux下的socket基础编程