欢迎使用CSDN-markdown编辑器
2016-06-22 12:03
344 查看
传一段C++实现的链表以及派生的栈,数据结构代码。希望高手指点。
节点定义
链表定义
栈结构
节点定义
#define TYLIST(T) ListNode<T>* //template<typename T> //struct ListNode{ // //数据成员 // struct ListNode<T> *pre, *next;//双向列表 // T info; // //构造函数 // ListNode(){}; // ListNode(T e, TYLIST(T) p1, TYLIST(T) p2) :info(e), pre(p1), next(p2){};//在函数形参之后大括号之前:是初始化操作 // //操作接口 // TYLIST(T) Insertpre(T const&el);//在当前节点之后插入节点 // TYLIST(T) Insertnext(T const&el);//插入操作,在当前节点之后插入节点 //};//节点类 //template<typename T> //TYLIST(T) ListNode<T>::Insertpre(T const &el) //{ // TYLIST(T) N = new ListNode(el,pre,this); // pre->next = N; // pre = N;//即,在this和this->pre之间插入N,建立相互之间的链接 // return N; //} //template<typename T> //TYLIST(T) ListNode<T>::Insertnext(T const &el) //{ // TYLIST(T) N= new ListNode(el,this,next); // next->pre = N; // next = N; // return N; //} template<class T> class ListNode { public: ListNode<T> *pre, *next; T info; //构造函数 ListNode(){}; ListNode(T e, TYLIST(T) p1, TYLIST(T) p2) :info(e), pre(p1), next(p2){};//在函数形参之后大括号之前:是初始化操作 //操作接口 TYLIST(T) Insertpre(T const&el);//在当前节点之后插入节点 TYLIST(T) Insertnext(T const&el);//插入操作,在当前节点之后插入节点 }; template<typename T> TYLIST(T) ListNode<T>::Insertpre(T const &el) { TYLIST(T) N = new ListNode(el,pre,this); pre->next = N; pre = N;//即,在this和this->pre之间插入N,建立相互之间的链接 return N; } template<typename T> TYLIST(T) ListNode<T>::Insertnext(T const &el) { TYLIST(T) N= new ListNode(el,this,next); next->pre = N; next = N; return N; }
链表定义
#include"ListNode.h" template<typename T> class List{ public: List(); List(List<T> const&L,int lo,int hi);//拷贝构造函数 ~List();//析构函数 //对外接口 int Size()const; bool IsEmpty(); T& operator[](int r)const;//对[]的重载 TYLIST(T) First()const;//返回首节点 TYLIST(T) Last()const;//返回最后一个节点 bool IsDisordered();//是否有序 //插入 void Assign(T *const p,int n);//把p中的n个元素复制到list中 TYLIST(T) Insertpre(TYLIST(T) p, T const&el);//在p 节点之前插入元素 TYLIST(T) Insertaft(TYLIST(T) p, T const&el);//在p节点之后插入元素el //查找 TYLIST(T) Find(T const&el);//查找某个元素 -- 无序 TYLIST(T) Find(T const&el, int lo, int hi);//某个区间查找 --无序 TYLIST(T) Find(T const&el, int n, TYLIST(T)p); TYLIST(T) Search(T const&el);//有序情况下查找el,并返回一个合适的位置能够插入el TYLIST(T) Search(T const&el,int n,TYLIST(T) p);//有序情况下查找el,并返回一个合适的位置能够插入el,p节点之前的序列中查找 //删除 bool Remove(TYLIST(T) p);//删除某个节点,成功返回true //排序 void Sort(); //去重 int Deduplicate();//无序 int Uniquify();//有序 void Reverse();//倒序 //遍历 int Traverse(bool(*vist)(T &));//函数指针 void Traverse(); template<typename TP> void Traverse(TP &v);//函数对象 private: int _size_; TYLIST(T) _head_, *_tail_;//内部变量加以下划线开始,私有不能被继承,只能类和类的友元函数使用 protected: void _Init();//初始化,类,子类,友元 bool _IsIn(const TYLIST(T) p);//某个节点在不在列表中 void _Clear();//清空所有节点 bool Even(T&el);//打印每个元素 void InsertSort(); }; template<typename T> bool Even(T&el)//是否是偶数 { return (el % 2)?false:true; } template<typename T> int List<T>::Traverse(bool(*vist)(T &)) { TYLIST(T) p = First(); int r = 0; while (p != _tail_) { if (vist(p->info)) r++; p = p->next; } return r; } template<typename T> template<typename TP> void List<T>::Traverse(TP &v) { TYLIST(T) p = First(); int r = 0; while (p != _tail_) { if (v(p)) r++; p = p->next; } } template<typename T> bool List<T>::_IsIn(const TYLIST(T) p) { TYLIST(T) ptr=_head_->next; while (ptr != _tail_) { if (ptr == p) return true; ptr = ptr->next; } return false; } template<typename T> List<T>::List() { _size_ = 0;//空链表 _head_ = new ListNode<T>; _tail_ = new ListNode<T>; _head_->next = _tail_; _head_->pre = NULL; _tail_->next = NULL; _tail_->pre = _head_; } template<typename T> int List<T>::Size()const { return _size_; } template<typename T> TYLIST(T) List<T>::First()const { return _head_->next; } template<typename T> TYLIST(T) List<T>::Last()const { return _tail_->pre; } template<typename T> List<T>::List(List<T> const&L, int lo, int hi) { //static List<T> LL = L;//此时定义的static 局部对象,在析构是会出问题,会析构两次 _size_ = L.Size(); _head_ = new ListNode<T>; _tail_ = new ListNode<T>; _head_->pre = NULL; TYLIST(T) temhead = _head_; TYLIST(T) temp = L.First(); int tem = _size_; while (tem--&&temp!=NULL) { TYLIST(T) p = new ListNode<T>; p->next = NULL; p->info = temp->info; p->pre = temhead; temhead->next = p; temhead = p; temp = temp->next; } _tail_->next = NULL; _tail_->pre = temhead; temhead->next = _tail_; } template<typename T> bool List<T>::IsEmpty() { return _size_ == 0; } template<typename T> bool List<T>::IsDisordered() { TYLIST(T) p, temp; bool order = true; if (Size() < 3) return order;//当节点数在两个以内时 默认有序 p = First(); temp = p->next; if (p->info < temp->info) order = false;//升序时order置0 p = temp; temp = p->next;//当节点数在3个以上包括3时,是有效的 while (p != NULL) { if (p->info < temp->info&&order == true) return false;//此时出现逆序 if (p->info >= temp->info&&order == false) return false; p = temp; tem = p->next; } return true; } template<typename T> T& List<T>::operator[](int r)const { TYLIST(T) p = _head_; if (r < _size_)//防止越界 { int temr = r; while (r--) p = p->next; return p->next->info; } } template<typename T> void List<T>::Assign(T *const p, int n) { TYLIST(T) ptr; _size_ = n; int i = 0; ptr = _head_; while (i<n) { TYLIST(T) temp = new ListNode<T>; temp->info = p[i++]; temp->next = NULL; temp->pre = ptr; ptr->next = temp; ptr = temp; } ptr->next = _tail_; _tail_->pre = ptr; _tail_->next = NULL; } template<typename T> TYLIST(T) List<T>::Insertpre(TYLIST(T) p, T const&el) { _size_++; return p->Insertpre(el); } template<typename T> TYLIST(T) List<T>::Insertaft(TYLIST(T) p, T const&el) { _size_++; return p->Insertnext(el); } template<typename T> TYLIST(T) List<T>::Find(T const&el , int lo,int hi)//[lo,hi) { int i = 0; if(IsEmpty()) return NULL; if (lo >= _size_ || hi <= lo||hi>=_size_)// 为保证安全操作 return NULL; TYLIST(T) temp = _head_->next;//temp指向第一个节点 while (i < lo)//搜索下界 { temp = temp->next; i++; } while (i < hi) { if (temp->info == el) return temp; temp = temp->next; i++; } return NULL; } template<typename T> TYLIST(T) List<T>::Find(T const&el) { return Find(el,0,_size_); } template<typename T> TYLIST(T) List<T>::Find(T const&el,int n,TYLIST(T)p)//p 的前 n个节点中搜索 { while (n-- >= 0) if ((p = p->pre)->info == el) return p; return NULL; } template<typename T> bool List<T>::Remove(TYLIST(T) p) { //如果传入的节点不在列表内部 if(!_IsIn(p)) return false; TYLIST(T) temn, *temp; temp = p->pre; temn = p->next; temp->next = temn; temn->pre = temp; _size_--; delete[]p; } template<typename T> void List<T>::Sort() { InsertSort(); } template<typename T> void List<T>::InsertSort() { //将链表分为2部分 TYLIST(T) ptr;//r 为分界 ptr = _head_->next;//初始化; int n = 0; while (ptr != _tail_) { Insertaft(Search(ptr->info, n, ptr),ptr->info); ptr = ptr->next; Remove(ptr->pre); n++; } } template<typename T> TYLIST(T) List<T>::Search(T const&el) { Search(el, _size_, _tail_); } template<typename T> TYLIST(T) List<T>::Search(T const&el,int n,TYLIST(T)p)//不包括p { while (n-- >= 0) if ((p=p->pre)->info <= el) break; return p; } template<typename T> int List<T>::Deduplicate() { TYLIST(T) ptr; ptr = First(); int n=0,r=0; while (ptr != _tail_) { TYLIST(T) p = Find(ptr->info, n, ptr); (p!=NULL) ? (Remove(p),r++) : n++; ptr = ptr->next; } return r; } template<typename T> int List<T>::Uniquify() { TYLIST(T) p, *q; p = First(); q = p->next; int r = 0; while (p!=Last()) { if (q->info == p->info) { TYLIST(T) tem = p; p = p->next; Remove(tem); r++; } p = q; q = p->next; } return r; } template<typename T> List<T>::~List() { TYLIST(T) p, *temp; p = _head_; while (p!=NULL) { temp = p->next; delete[]p; p = temp; } }
栈结构
#include "Vector.h" template<class T> class Stack :public List<T>{ public: void Push(T x); T Pop(){ return PopBack()->info; } T top(); ///bool IsEmpty(); }; template<class T> void Stack<T>::Push(T x) { Insertaft(Last(), x); } template<class T> T Stack<T>::top() { //int n = Size(); return First()->info; //return[]; }
相关文章推荐
- 数据结构 -- 简单体会
- HDU5320 Fan Li
- 《Java数据结构与算法》笔记-CH4-5不带计数字段的循环队列
- 数据结构 : Hash Table
- halcon学习笔记——(10)从数字图像基础到halcon数据结构概述
- 《Java数据结构与算法》笔记-CH4-4循环队列
- 数据结构知识点归纳
- 一步一步写平衡二叉树(AVL树)
- 9-12-哈希查找表/散列表-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
- MySQL索引背后的数据结构及算法原理
- 《数据结构》创建二叉树
- 汇编实现排序——简单选择排序
- 数据结构 - 树和二叉树的基本运算实现
- Redis 数据结构
- Java数据结构和算法——数组、单向链表、双向链表
- 【数据结构与算法】十六 二叉树遍历 Breadth-First-Search 广度优先
- 面向对象的数据结构--链 list
- 数据结构实验之链表六:有序链表的建立
- Java 数据结构之有序数组,二分查找法
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版