数据结构3 Stack and Queue

编程入门 行业动态 更新时间:2024-10-07 02:19:37

<a href=https://www.elefans.com/category/jswz/34/1769880.html style=数据结构3 Stack and Queue"/>

数据结构3 Stack and Queue

数列实现栈

struct  StackRecord {int     Capacity ;              /* size of stack,栈的大小 */int     TopOfStack;          /* the top pointer ,栈顶位置,-1表示空栈*/ElementType  *Array;    /* array for stack elements,存放栈的元素的数组 */} ; 

存放栈的大小,栈顶位置,栈的数组

push,pop前都必须要检查是否是合法操作。

空栈不能pop,满栈不能push

空栈头指向第一个元素,尾指向第一个元素之前一个元素。

链表实现栈

用链表实现的时候,pop push都是从头结点开始的。(一个dummy head指向头结点)

同样注意检查操作是否合法。

应用

括号匹配,

后序表达式的计算:遇到操作数入栈,遇到操作符出栈两个数,结果入栈

中序转前序:

遇到操作数输出,

遇到操作符,和栈顶的操作符比较,如果优先级比栈顶高,则入栈,否则出栈一个元素,继续比较。

括号入栈前优先级最高,入栈后优先级最低。

左括号"("直到遇到右括号才允许出栈。

相同的运算优先级,左结合时前一个优先级更高(+,-,* ,/)右结合(^,点)

数列实现队列

struct  QueueRecord {int     Capacity ;   /* max size of queue */int     Front;          /* the front pointer */int     Rear;           /* the rear pointer */int     Size;  /* Optional - the current size of queue */ElementType  *Array;    /* array for queue elements */} ; 

包括 记录队列数组的大小,首元素位置,尾元素位置,队列的大小,记录队列的数组

一开始rear指向front前一个元素,表示队列是空的。

插入第一个元素,rear和front指向第一个元素。

以后每插入一个元素,rear往后移动

注意进入的顺序,从rear(后面)进队列,从front(前方)出队列

Circular Queue

栈的形状是环形,首尾相邻

初始时,rear仍然指向front的前方

注意当入栈达到栈的最大数量,下一个将从0开始入栈

题目

1.Push 5 characters ooops onto a stack. In how many different ways that we can pop these characters and still obtain ooops?

ooo出入栈顺序随意,之后push( p ),pop( p ),push( o ),pop( o )。注意出栈不能比入栈多。
可以入出入出入出,入出入入出出,入入出入出出,入入出出入出,入入入出出出,5个

2.Represent a queue by a singly linked list. Given the current status of the linked list as 1->2->3 where x->y means y is linked after x. Now if 4 is enqueued and then a dequeue is done, the resulting status must be:
A.1->2->3
B.2->3->4
C.4->1->2
D.the solution is not unique

注意出前出后入,B

3.Suppose that an array of size 6 is used to store a circular queue, and the values of front and rear are 0 and 4, respectively. Now after 2 dequeues and 2 enqueues, what will the values of front and rear be?
A.2 and 0
B.2 and 2
C.2 and 4
D.2 and 6

注意环栈最大索引不超过5,索引顺序是012345012345…所以选A

4.Given the popping sequence of a stack as {a, b, c, d, e}. Among the following, the impossible pushing sequence is:
A.c b a e d
B.e a b c d
C.d e a c b
D.e d a b c
ABCD一个一个push,注意到底哪个是push,哪个是pop,C

5.Since the speed of a printer cannot match the speed of a computer, a buffer is designed to temperarily store the data from a computer so that later the printer can retrieve data in order. Then the proper structure of the buffer shall be a:
A.graph
B.queue
C.stack
D.tree

注意print,存储之间的关系是先进先出,故选队列。

6.Suppose that an array of size m is used to store a circular queue. If the head pointer front and the current size variable size are used to represent the range of the queue instead of front and rear, then the maximum capacity of this queue can be:
(5分)
A.cannot be determined
B.m+1
C.m
D.m-1
当空队列的时候,rear指向第一个前一个,即最后一个,fornt指向第一个,当满队列的时候,发现rear和front也是这样指的,因此环队列应该要少存一个,防止搞不清,但是当用元素个数的时候,发现不会有这种情况,故是m个

更多推荐

数据结构3 Stack and Queue

本文发布于:2024-02-28 07:26:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据结构   Stack   Queue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!