新手 求问 C++数据结构顺序表的逆置怎么显示顺序表的输出长度

数据结构的C++实现之队列的顺序存储结构(循环队列) - 数据结构与算法 - 编程入门网
数据结构的C++实现之队列的顺序存储结构(循环队列)
队列(Queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。是一种先进先出的线性表(FIFO)。
允许插入的一端称为队尾,允许删除的一端称为队头。我们在《栈的顺序存储结构》中发现,栈操作的top指针在Push时增
大而在Pop时减小,栈空间是可以重复利用的,而队列的front、rear指针都在一直增大,虽然前面的元素已经出队了,但它
所占的存储空间却不能重复利用。但大多数程序并不是这样使用队列的,一般情况下出队的元素就不再有保存价值了,这些
元素的存储空间应该回收利用,由此想到把队列改造成环形队列(Circular Queue):把queue数组想像成一个圈,front和
rear指针仍然是一直增大的,当指到数组末尾时就自动回到数组开头,就像两个人围着操场赛跑,沿着它们跑的方向看,从
front到rear之间是队列的有效元素,从rear到front之间是空的存储位置,如果front追上rear就表示队列空了,如果rear
追上front就表示队列的存储空间满了。故一般我们将其实现为循环队列,当出队列时就不需要全部进行移动,只需要修改
队头指针,也可以解决&假溢出&的问题。
示例程序:(改编自《大话数据结构》)
#include&iostream>
#define MAXSIZE 20
typedef int ElemT
typedef struct
ElemType data[MAXSIZE];
/* 头指针 */
/* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
bool InitQueue(SqQueue *pq)
cout && &Init Queue ...& &&
pq->front = 0;
pq->rear = 0;
bool ClearQueue(SqQueue *pq)
cout && &Clear Queue ...& &&
pq->front = 0;
pq->rear = 0;
bool QueueEmpty(SqQueue Sq)
return (Sq.front == Sq.rear); /* 队列空的标志 */
int QueueLength(SqQueue Sq)
/* 队列的当前长度 */
return (Sq.rear - Sq.front + MAXSIZE) % MAXSIZE;
/* 返回头元素 */
bool GetHead(SqQueue Sq, ElemType *pe)
if (QueueEmpty(Sq))
*pe = Sq.data[Sq.front];
cout && &Get Head Item & && *pe &&
bool EnQueue(SqQueue *pq, ElemType Elem)
/* 队列满 */
if (QueueLength(*pq) == MAXSIZE)
cout && &EnQueue Item & && Elem &&
pq->data[pq->rear] = E/* 将元素赋值给队尾 */
pq->rear = (pq->rear + 1) % MAXSIZE;/* rear指针向后移一位置, */
/* 若到最后则转到数组头部 */
bool DeQueue(SqQueue *pq, ElemType *pe)
if (QueueEmpty(*pq))
*pe = pq->data[pq->front];/* 将队头元素赋值给*pe */
cout && &DeQueue Item & && *pe &&
pq->front = (pq->front + 1) % MAXSIZE;/* front指针向后移一位置, */
/* 若到最后则转到数组头部 */
bool QueueTraverse(SqQueue Sq)
cout && &Queue Traverse ...& &&
for (int i = 0; Sq.front + i & Sq. i = (i + 1) % MAXSIZE)
cout && Sq.data[i + Sq.front] && ' ';
int main(void)
SqQueue Sq;
InitQueue(&Sq);
for (int i = 0; i & 5; i++)
EnQueue(&Sq, i);
QueueTraverse(Sq);
GetHead(Sq, &result);
DeQueue(&Sq, &result);
DeQueue(&Sq, &result);
if (!QueueEmpty(Sq))
cout && &Queue Length: & && QueueLength(Sq) &&
QueueTraverse(Sq);
单是顺序存储,若不是循环队列,算法的时
间性能是不高的,但循环队列也面临着数组可能溢出的问题。c++顺序表的实现-中国学网-中国IT综合门户网站
> c++顺序表的实现
c++顺序表的实现
转载 编辑:李强
为了帮助网友解决“c++顺序表的实现”相关的问题,中国学网通过互联网对“c++顺序表的实现”相关的解决方案进行了整理,用户详细问题包括:RT,我想知道:c++顺序表的实现,具体解决方案如下:解决方案1:用模板类来做~~~急通过对数据库的索引,我们还为您准备了:问:#include&stdio.h& #include&malloc.h& #defineMAXLEN 100 typedefint E...答:#include #include #define MAXLEN 100 typedef int ElemT typedef struct list { ElemType data[MAXLEN]; int leng...===========================================问:#include&iostream.h& #include&stdlib.h& #include&time.h& #include&m...答:这是C++编的,封装在class Linearlist中,调试过了,没有问题。 #include class Linearlist {private: struct Node { Node * }; Node * public: Linearlist()//构造函数 { Node *p...===========================================问:#include&iostream.h& #include&stdlib.h& #include&time.h& #include&m...答:int Locatelist(Sqlist &L,Elemtype e) //定位 {int i=1; Elemtype *p=L. while(i===========================================问:用C++语言实现“顺序表”的面向对象抽象数据类型。顺序表类seqlist中包含...答:/*用C++语言实现“顺序表”的面向对象抽象数据类型。顺序表类seqlist中包含一下方法: a)void init(int n) 能够接收用户输入的n个值以初始化顺序表。 b)void randinit()随机产生n(n 0) { delete [] } } void SeqList::init( int n ) { l...===========================================问:运行老是出错.将元素添加到栈时,若元素&=4个就老是出错. #include&iostr...答:这是我做的 你拿去看下吧 我这个是正确的 #include class sqstack { private: int * public: sqstack(int size) {maxsize= elem=new int[maxsize]; top=0; } ~sqstack(){delete []} ...===========================================问:#include &iostream.h& #include &stdlib.h& #define Maxsize 10 int a[...答:把exchange函数里的for(int i=0;i===========================================问:并用主函数验证并上机实现。答:#include typedef int ElemT struct NodeType { ElemT NodeType * }; class LinkList { private: NodeType *H public: LinkList();//构造 ~LinkList();//析构 void create();//建表 void insert();/...===========================================问:麻烦最好用结构体实现顺序表的查找、要完整的,谢谢……答:#include typedef struct node { int date[100]; }linkNint main ( ) { linkN int i,n,schar,set=0; cout===========================================
本文欢迎转载,转载请注明:转载自中国学网: []
用户还关注用C++版数据结构写出一个顺序队列要求:键盘输入10个入队,例如输入输出求大神帮忙_百度知道
用C++版数据结构写出一个顺序队列要求:键盘输入10个入队,例如输入输出求大神帮忙
提问者采纳
队列文件:#ifndef QUEUE_H#define QUEUE_H#include &iostream&using std::using std::using std::template &class Type&class Queue { friend ostream & operator&& (ostream &, const Queue&Type& &);public: static const int DefaultS Queue(int = DefaultSize); //创建容量MaxQueueSize空队列 Queue(Type [], int, int = DefaultSize); ~Queue() {delete []} bool IsFull();
//若元素数等于队列容量则返true否则返false void Add(const Type &);
//若IsFull()true调用外处理函数QueueFull()否则item插入队尾 bool IsEmpty()
//若队列元素数等于0则返true否则返false Type * Delete(Type &);
//若IsEmpty()true调用QueueEmpty()并返0否则删除队列前段元素并返其指针 void Empty();
//清空队列 void QueueFull();
//扩充1倍存储空间 void QueueEmpty();
//提示队列已空元素能队private: int front, Type * int maxS};template &class Type&const int Queue&Type&::DefaultSize = 10;template &class Type&Queue&Type&::Queue(int pMaxSize) { queue = new Type [pMaxSize]; maxSize = pMaxS front = rear = -1;}template &class Type&Queue&Type&::Queue(Type pArray[], int pSize, int pMaxSize) { queue = new Type [pMaxSize]; for (int i = 0; i & pS i++)
queue[i] = pArray[i]; } front = -1; rear = pSize - 1; maxSize = pMaxS}template &class Type&bool Queue&Type&::IsFull() { if ( rear == maxSize - 1 ) }template &class Type&bool Queue&Type&::IsEmpty() const { if ( rear == front ) }template &class Type&void Queue&Type&::Add(const Type & pX) { if (IsFull())
QueueFull();
queue[++rear] = pX; } else {
queue[++rear] = pX; }}template &class Type&Type * Queue&Type&::Delete(Type & pX) { if (IsEmpty()) {
QueueEmpty();
return 0; } else {
pX = queue[++front];
return &pX; }}template &class Type&void Queue&Type&::QueueEmpty() { cout && &队列空能进行队操作& &&}template &class Type&void Queue&Type&::QueueFull() { Type * newQueue = new Type [maxSize * 2]; for ( int i = front + 1; i &= i++ ) {
newQueue[i] = queue[i]; } maxSize = maxSize * 2; delete [] queue = newQ cout && &队列已满现已增加空间原2倍 & && maxS}template &class Type&void Queue&Type&::Empty() { front = rear = -1;}template &class Type&ostream & operator&& (ostream & pOutput, const Queue&Type& & pQ) { if (pQ.IsEmpty())
cout && &空队列& &&
return pO } for ( int i = pQ.front + 1; i &= pQ. i++ )
pOutput && pQ.queue[i] && & &; } cout && return pO}#endif
来自团队:
其他类似问题
为您推荐:
键盘输入的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁数据元素之间的关系是什么 线性表的定义线性表:简称表,是N(N≥0)个具有相..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
数据结构(C++版)之线性表
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口数据结构顺序表有关问题 求解 ! - C++当前位置:& &&&数据结构顺序表有关问题 求解 !数据结构顺序表有关问题 求解 !&&网友分享于:&&浏览:0次数据结构顺序表问题 求解 !!!请求大虾们帮助!!程序在C++6.0中调试没有错误,但是运行却输出不了正确结果,输出的错误结果如下:i值不合法i值不合法i值不合法i值不合法La=i值不合法i值不合法i值不合法i值不合法Lb=Lc=不知道是程序运行的哪一部分出错,我对了很多遍代码了都不知道什么问题!!#include&string.h&#include&ctype.h&#include&malloc.h&#include&limits.h&#include&stdio.h&#include&stdlib.h&#include&io.h&#include&math.h&#include&process.h&#include&iostream.h&#define TRUE 1#define define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE –1typedef int Stypedef int Btypedef int ElemT#define LIST_INIT_SIZE 100 // 线性表存储空间的初始分配量#define LISTINCREMENT 10 // 线性表存储空间的分配增量typedef struct &{ &
ElemType * // 存储空间基址&
& //当前表的长度&
& //当前分配的存储容量(以sizeof(ElemType)为单位) &}SqLStatus InitList(SqList &L)& { // 构造一个空的顺序线性表 &
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); &
if(!L.elem) &
exit(OVERFLOW); // 存储分配失败 &
L.length=0; // 空表长度为0 &
L.listsize=LIST_INIT_SIZE; // 初始存储容量 &
return OK;}void ListTraverse(SqList &L) &{ // 初始条件:顺序线性表L已存在 &
// 操作结果:依次输出L的每个数据元素 &
ElemType *p; & &
for(i=1;i&=L.i++) &
printf(&%5d&,*p);
printf(&\n&); &}Status ListInsert(SqList &L,int i,ElemType e)&
&{ // 初始条件:顺序线性表L已存在,i的合法值: 1≤i≤ListLength(L)+1 &
// 操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1 &
ElemType *newbase,*q,*p; &
if(i&1||i&L.length+1){ &
printf(&i值不合法\n&); &
return ERROR; &
if(L.length&=L.listsize) // 当前存储空间已满,增加分配 &
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));& //重新分配内存快 &
if(!newbase) &
exit (OVERFLOW); // 存储分配失败 &
L.elem= // 新基址
L.listsize+=LISTINCREMENT; // 增加存储容量 &
q=&(L.elem[i-1]); // q为插入位置 &
for(p=&(L.elem[L.length-1]);p&=q;--p) // 插入位置及之后的元素右移 &
{*(p+1)=*p; &
*q=e; // 插入e &
++L. }// 表长增1 &
return OK; &}Status GetElem (SqList &L,int i,ElemType &e) &{ // 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)。操作结果:用e返回L中第i个数据元素的值 &
if(i&1||i&L.length) &
return ERROR; &
e=*(L.elem+i-1);//e=L.elem[i-1]; &
return OK; &}Status ListLength(SqList &L){ return L.}void MergeList(SqList La,SqList Lb,SqList &Lc){ ElemType ai, &
int i=1,j=1,k=0, La_len, Lb_
//i,j开始分别指向La,Lb的
//第一个元素
InitList(Lc); //创建空表Lc &
La_len=ListLength(La);
Lb_len=ListLength(Lb);
while((i&=La_len)&&(j&=Lb_len)) // La和Lb均非空 &
GetElem(La,i,ai); &
GetElem(Lb,j,bj); &
if(ai&=bj){ListInsert(Lc, ++k, ai);
else{ListInsert(Lc, ++k, bj);
while(i&=La_len)
//说明表Lb已经为空了,只需将La中剩下的 &
//元素插入Lc中了。 &
GetElem(La,i++,ai); &
ListInsert(Lc,++k,ai); &
while(j&=Lb_len)
//说明表La已经为空了,只需将Lb中剩下的 共&2&页:
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有}

我要回帖

更多关于 数据结构顺序表的实现 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信