数据结构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个

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值