数据结构-线性表的一些基础操作 c++代码
来源:互联网 发布:卡尔曼滤波跟踪算法 编辑:程序博客网 时间:2024/05/17 10:53
//线性表的顺序存储结构template <class T>class Linearlist{public:Linearlist(int MaxListSize == 10);~Linearlist() {delete []element;}bool IsEmpty() const {return length == 0;}bool IsFull() const {return length == MaxSize;}int Length() const {return length;}bool Find(int k, T &item) const;int search(const T &item) const;void Delete(int k, T &item);void Insert(int k, const T &item);private:int MaxSize, length, T *element;};template <class T>Linearlist<T>::Linearlist(int MaxListSize == 10){MaxSize = MaxListSize;element = new T[MaxSize]; //申请规模为MaxSize的数组空间length = 0; // 初始时没有真正的表结点,故表长度为0}//存取:将下标为k的结点的字段值赋给item并返回true,若不存在则返回false;template <class T>bool Linearlist<T>::Find(int k, T &item) const{if (k < 0 || length == 0 || k >length-1){cout << " unreasonable position or empty list!" << endl;return false;}else{item = element[k];return true;}}//查找:在表中查找字段值为item的结点并返回其下标;若表中没有item,则返回-1;template <class T>int Linearlist<T>::search(const T &item) const{for (int k = 0; k <= length-1; k++){if(element[k] == item)return k;}return -1;}//删除表中下表为k的结点并将其值赋给itemtemplate <class T>void Linearlist<T>::Delete(int k, T &item){if (find(k, item)) //若找到下标为k的结点,则将其后面所有结点均向前移动一个位置{for (int i=k; i<length; i++){element[i] = element[i+1];}item = element[k];length--;}}//在下表为k的结点后插入itemtemplate <class T>void Linearlist<T>::Insert(int k, const T &item){if(IsFull()) //若表已满,无法插入新结点{cout << "The list is full!" << endl;exit(1);}else if (k<0 || k>length-1){cout << "The node does not exist!" << endl;exit(1);}else{for (i=length-1; i>=k+1; i--){element[i+1] = element[i];}}element[k+1] = item;length++;}//单链表结点结构SLNodetemplate<class T>struct SLNode{T data; //数据域SLNode<T> *next; //指针域SLNode(SLNode *nextNode = NULL){next = nextNode;}SLNode(const T &item, SLNode *nextNode = NULL){data = item;next = nextNode;}};// SLNode 的定义参考博文数据结构-栈的一些基础操作c++代码//单链表的链式存储结构template <class T>class SLList{public:SLList(){head = new SLNode<T>()}; //构造函数,构造一个只有哨位结点的空表SLList(T &item); //构造函数~SLList();bool IsEmpty() const {return head->next == NULL;};int length() const;//bool Find(int k, T &item) const;int Search(const T &item) const;void Delete(int k, T &item);void Insert(int k, const T &item);private:SLNode<T> *head;SLNode<T> *tail;SLNode<T> *currptr;};//单链表的构造函数,生成一个只有哨位结点的空表template<class T>SLList<T>::SLList(){head = tail = currptr = new SLNode<T>();size = 0;}//单链表的构造函数,生成含有哨位结点和一个表结点的表template<class T>SLList<T>::SLList(T &item){tail = currptr = new SLNode<T>(item); //生成一个表结点head = new SLNode<T>(currptr); //生成哨位结点size = 1;}//单链表的析构函数template <class T>SLList<T>::~SLList(){while (!IsEmpty()){currptr = head->next;head->next = currptr->next;delete currptr;}delete head;}//算法Insert//在当前结点后插入一个data域值为item的结点template <class T>void SLList<T>::Insert(const T &item){currptr->next = new SLNode<T>(item, currptr->next);if (tail == currptr)tail = currptr->next;size++;}//在表尾插入一个data域值为item的结点template <class T>void SLList<T>::InsertFromTail(const T &item){tail->next = new SLNode<T>(item, NULL);tail = tail->next;size++;}//在哨位结点后插入template <class T>void SLList<T>::InsertFromHead(const T &item){if(IsEmpty()){head->next = new SLNode<T>(item, head->next);tail = head->next;}else{head->next = new SLNode<T>(item, head->next);}size++;}//算法Delete//删除当前结点的后继结点并将其data值返回给变量de_itemtemplate <class T>bool SLList<T>::Delete(T &de_item){if(currptr == tail || IsEmpty()){cout << "No next node or empty list!";return false;}SLNode<T> *temp = currptr->next;currptr->next = temp->next;size--;de_item = temp->data;if(temp == tail)tail = currptr;delete temp;return true;}// 删除哨位结点后的第一个真正表结点并将其data值返回给变量de_itemtemplate <class T>bool SLList<T>::DeleteFromHead(T &de_item){if (IsEmpty()){cout << "Empty list!";return false;}SLNode<T> *temp = head->next;head->next = temp->next;size--;de_item = temp->data;if (temp == tail){tail = head;}delete temp;return true;}//删除表尾结点并将其data值返回给变量de_itemtemplate <class T>bool SLList<T>::DeleteFromTail(T &de_item){if (IsEmpty()){cout << "Empty list!";return false;}//令当前指针指向表尾结点的前驱结点SetEnd();Prev();de_item = tail->data;currptr->next = NULL;size--;delete tail;tail = currptr;return true;}
0 0
- 数据结构-线性表的一些基础操作 c++代码
- 数据结构基础--线性表操作
- 数据结构中线性表的一些基本操作
- 数据结构-栈的一些基础操作c++代码
- 【数据结构】线性表顺序结构的操作---C/C++语言
- 【数据结构】线性表顺序结构的操作---C/C++语言
- C语言数据结构之线性表的基本操作
- 数据结构之对线性表的操作(C语言版)
- 基于c语言,严蔚敏版数据结构的线性表节本操作
- 数据结构 线性表的基本操作(C语言)
- C语言数据结构动态线性表的操作集
- 数据结构中线性表的c语言代码实现
- 数据结构 线性表的操作
- 数据结构---线性顺序表操作(c++)
- 【数据结构】C语言线性表操作
- 数据结构伪C代码:2.线性表
- 数据结构之线性结构的一些操作总结
- 数据结构--线性表操作
- php 数组找评委跳水打分习题
- hdu 1166 树状数组 线段树
- Uva 10006 Carmichael Numbers (快速幂)
- 栈,堆,全局,文字常量,代码区总结
- Js 冒泡事件阻止
- 数据结构-线性表的一些基础操作 c++代码
- Java IO流
- poj1222 EXTENDED LIGHTS OUT(YY+高斯消元)
- 一个java初学者的课堂总结
- 【学习笔记之汇编语言】【二】寄存器
- Java统计字符串中出现次数最多的字符及其次数
- Libevent时间管理
- 不水好好学——HTML、CSS、Javascript之间的联系
- 约瑟夫环问题 实习1-1.2