线性表(C++)
来源:互联网 发布:python gui wxpython 编辑:程序博客网 时间:2024/05/22 04:40
线性表ADT——单链表
- 单链表定义 单链表解释
无空表头的单链表
ADT LinearList {
数据:
0个或多个元素等不定长的线性序列(a[0],a[1],….a[n-1]…)
操作:
isEmpty(): 若线性表空,则返回true;否则返回false。
Length(): 返回表中的元素的个数。
Find(i,x): 在x中返回表中下表为i的元素a[i]. 若不存在,则返回false,否则返回true。
Search(x): 若x不在表中,则返回-1,否则返回x在表中的小标。
Insert(i,x): 在元素a[i]之后插入x。若i=-1,则x插在第一个元素a[0]前。若插入成 功,则返回true,否则返回false。
Delete(i): 删除元素a[i]。若删除成功,则返回true,否则返回false。
Update(i,x): 将元素a[i]的值修改为x。若修改成功,则返回true,否则返回false。
outPut(out): 把表送至输出流。
}
- 线性表类
#include <iostream>using namespace std;template <class T>class LinearList{public: virtual bool isEmpty() const=0; virtual int lenght() const=0; virtual bool Find(int i,T & x) const=0; virtual int Search(T x) const=0; virtual bool Insert(int i,T x)=0; virtual bool Delete(int i)=0; virtual bool update(int i,T x)=0; virtual void outPut(ostream& out) const=0;};
- Node类
template <class T> class SingleList;template<class T>class Node{private: T element; Node<T> * next; friend class SingleList<T>;//声明SingleList<T> 为Node的友元类,类的代码会在下面给出};
- SingleList类
template <class T>class SingleList:public LinearList<T>{public: SingleList(){first=NULL;n=0;} ~SingleList(); bool isEmpty() const; int lenght() const; bool Find(int i,T& x) const; int Search(T x) const; bool Insert(int i,T x); bool Delete(int i); bool update(int i,T x); void outPut(ostream& out) const;private: Node<T>* first; int n;//用来存储单链表中的元素的个数};template <class T>SingleList<T>::~SingleList(){ Node<T> *p; while(first){ p=first->next; delete first; first=p; }}template <class T>int SingleList<T>::lenght() const{ return n;}template <class T>bool SingleList<T>::isEmpty() const{ return n==0;}template <class T>bool SingleList<T>::Find(int i,T & x) const //在x中返回表中返回下表为i 的元素ai。若不存在,则返回false,否则返回true{ if(i<0||i>n-1){ cout<<"out of bounds"; return false; } Node<T> * p=first; for(int j=0;j<i;j++){ p=p->next; } //当循环结束后p指向是下表为i的元素 x=p->element; return true;}template <class T>int SingleList<T>::Search(T x) const//若x不在表中,则返回-1,否则返回x在表中的下表{ Node<T> * p=first; int i; for( i=0;p&&(p->element!=x);i++){ p=p->next; } if(p){ return i; } else{ return -1; }}template <class T>bool SingleList<T>::Insert(int i,T x)//在元素ai之后插入x。若i=-1,则x插在第一个元素a0前。若插入成功,则返回true,否则返回false{ Node<T> *q = new Node<T>; q->element=x; Node<T> *p=first; if(first==NULL){ first=q; q->next=NULL; } else{ if(i<-1||i>n-1){ cout<<"out of bounds"; return false; } for(int j=0;j<i;j++){ p=p->next; } if(i>-1){ if(i==n-1){ q->next=NULL; p->next=q; } else{ q->next=p->next; p->next=q; } } else{ q->next=first; first=q; } } n++; return true;}template <class T>bool SingleList<T>::Delete(int i)//删除下表为i的元素,若删除成功,返回为true,否则返回为false{ if(!n){ cout<<"underflow"; return false; } if(i<0||i>n-1){ cout<<"out of bounds"; return false; } Node<T> *p=first; Node <T> *q=first; for(int j=0;j<i-1;j++){ q=q->next; } if(i==0){ first=first->next; } else{ if(i==n-1){ p=q->next; q->next=NULL; } else{ p=q->next; q->next=p->next; } } n--; return true;}template <class T>bool SingleList<T>::update(int i,T x){ if(i<0||i>n-1){ cout<<"out of bounds"; return false; } Node<T> *p=first; for(int j=0;j<i;j++){ p=p->next; } p->element=x; return true;}template <class T>void SingleList<T>::outPut(ostream & out) const{ Node<T> *p=first; while(p){ out<<p->element<<" "; p=p->next; } out<<endl;}
- main()方法
int main(){ SingleList<int> s1 ; SingleList<int> s2; for(int i=0;i<5;i++){ s1.Insert(i-1,i); } for(int i=2;i<5;i++){ s2.Insert(i-3,i); } s1.outPut(cout); cout<<endl; cout<<s1.lenght(); cout<<endl; s2.outPut(cout); cout<<endl; cout<<s2.lenght(); return 0;}
阅读全文
0 0
- 线性表(C++)
- 数据结构-线性表 (C++)
- Objective-C 线性表
- 数据结构线性表c
- 线性顺序表C
- 线性链表C
- 线性表--单链表(C++)
- C - 数据结构 - 线性表
- C线性表
- 线性表 C
- 线性表(C++)
- 线性表(C++)
- C构造线性表
- C语言-线性表
- c 线性链表程序
- C语言实现线性表
- c语言线性表详解
- 线性表 C语言实现
- BZOJ1433:[ZJOI2009]假期的宿舍
- html笔记
- 信息化时代文件销毁,保密工作尤为重要
- 派生类的构造方法以及修饰符的作用
- 谈谈开源(一)
- 线性表(C++)
- java算法之选择排序
- PAT 乙级练习题1018. 锤子剪刀布 (20)
- 基于MAC OSX的串口助手源代码讲解(上)
- spring boot之统一错误异常处理
- MongoDB学习笔记之 第4章 MongoDB整合Spring
- spring boot整合mybatis
- sizeof与strlen的区别
- 算法竞赛入门经典第3章习题