数据结构:栈和队列

章节主题

•堆栈及其应用

•基于数组的堆栈实现

•基于参考的堆栈实现•代数表达式 - 堆栈应用程序

•队列及其应用

•基于阵列的队列实现

•基于参考的队列实现

 

Chapter Topics

• Stacks and Their Applications

• Array Based Implementation of Stacks

• Reference Based Implementation of Stacks • Algebraic Expressions – Stack Application

• Queues and Their Applications

• Array Based Implementation of Queues

• Reference Based Implementation of Queues

 

堆栈及其应用(Stacks and Their Applications)

栈:后进先出

  • 堆栈是允许以下操作的项集合:

- boolean empty()检查堆栈是否为空。

- void push(E o)将一个对象o添加到堆栈中。

- E pop()删除并返回最近添加到堆栈中的项目。

  • 堆栈是Last-First-Out容器:添加的最后一项是第一个要删除的项目。

 

Stack peek()操作

•大多数堆栈支持"E peek()"操作:这会返回但不会从堆栈中删除最近添加到堆栈中的项目

•peek()返回下一次调用pop()时将删除的项目

 

The Stack peek() Operation

• Most stacks support a "E peek() "operation: this returns, but does not remove from the stack, the item most recently added to the stack

• peek() returns the item that will be removed by the next call to pop()

 

堆栈的应用

•许多项目集合自然被视为堆栈:

- 自助餐厅里的一叠盘子。

- 汽车挤满了狭窄的车道。

- 返回执行程序中方法调用的地址。

 

Applications of Stacks

• Many collections of items are naturally regarded as stacks:

– A stack of plates in a cafeteria.

– Cars packed in a narrow driveway.

– Return addresses of method calls in an executing program.

 

在自助餐厅的盘子

•板材堆叠成一列。

•在板的顶部添加新板。

•从板的顶部移除板,使得最后添加的板是第一个被移除的板。

 

Plates in a Cafeteria

• Plates are stacked in a column.

• A new plate is added to the top of the column of plates.

• Plates are removed from the top of the column of plates, so that the last plate added is the first removed.

 

方法返回地址

•程序通常会生成方法调用链。 

•调用的最后一个方法是第一个返回的方法。

•编译器使用堆栈来维护用于执行堆栈中的方法的返回地址列表。

地址m2

m1的地址

void m1(){m2();}

void m2(){m3();}

void m3(){//做某事}

•方法m3返回m2。

•方法m2返回m1。

 

Method Return Addresses

• Programs often make chains of method calls. 

• The last method called is the first to return.

•The compiler uses a stack to maintain the list of return addresses for executing methods in a stack.

address of m2

address of m1

void m1(){ m2();}

void m2(){ m3();}

void m3(){// do something}

• Method m3 returns to m2.

• Method m2 returns to m1.

 

Java Collection Framework Stack类

•JCF提供堆栈的通用实现:

- Stack <E>()(构造函数)

 - E push(E元素)

- E pop()

- E peek()

- 布尔值空()

•JCF堆栈可用于创建String,数值或任何其他类型的堆栈。

•以下示例演示了如何使用JCF Stack类来处理字符串。

 

Java Collection Framework Stack Class

• The JCF provides a generic implementation of a stack:

– Stack<E>() (constructor)

 – E push(E element)

– E pop()

– E peek()

– boolean empty()

• The JCF stack can be used to create stacks of String, numeric values, or any other types.

• Following example demonstrates the use of the JCF Stack class to work with strings.

 

基于数组的堆栈实现(Array Based Implementations of Stacks)

堆栈的数组实现

•可以使用数组来保存正在存储的项目,从而实现堆栈。

•可以实现推送,以便将项目存储在数组的末尾。

•pop可以实现,因此它返回数组末尾的项目。

•变量top可用于跟踪数组的实际末尾,其中将添加下一个项目。

•需要实现堆栈的其他常见操作。 例如,isEmpty()。

 

Array Implementation of Stacks

• A stack can be implemented by using an array to hold the items being stored.

• push can be implemented so it stores items at the end of the array.

• pop can be implemented so it returns the item at the end of the array.

• A variable top can be used to track the actual end of the array, where the next item will be added.

• Other common operations of a stack need to be implemented. For example, isEmpty().

 

 

堆栈溢出(Stack Overflow)

尝试将值推送到堆栈(如果已满)的数组称为堆栈溢出异常时抛出的异常。

 

The exception thrown when an attempt is made to push a value onto a stack whose array if full is called a stack overflow exception.

 

Peek()

•将引用返回到项目顶部

•堆栈为空时抛出的异常。

 

Peek()

• Return the reference to the top the item

• The exception thrown when the stack is empty.

 

基于参考的堆栈实现(Reference Based Implementations of Stacks)

 

通过使用列表的头部作为堆栈的“顶部”,链接列表可用于实现堆栈。

•push(E o)添加一个新项目作为链表的新头。

•E pop()删除并返回链表的头部。

 

A linked list can be used to implement a stack by using the head of the list as the “top” of the stack.

• push(E o) adds a new item as the new head of the linked list.

• E pop() removes and returns the head of the linked list.

 

push(E o)

•创建一个空堆栈。

•将元素推入空堆。

创建一个以原栈顶元素为继reference的新元素。(c中就是创建一个尾指针指向原栈顶元素的新元素)

•将栈顶设为新元素。

 

push(E o)

• Create an empty stack.

• Push Carol into the empty stack.

Create Debby with next reference to Carol.

• Set top to be Debby.

 

pop()

  • 如果堆栈为空,则Pop抛出异常。

  • 从堆栈中删除栈顶元素。

    • 记住栈顶元素。

    • 将栈顶元素设为第二元素(顶部的下一个节点)。

    • 将栈顶元素的后继设置为null以将其返回到系统。

 

pop()

  • Pop throws exception if the stack is empty.

  • Remove Brad from the stack.

    • Remember Brad, the top.

    • Set top to be Alan, the next node of the top.

    • Set next of Brad to be null to return it to the system.

 

isEmpty()和Peek()

•如果头指针顶部为空,则堆栈为空。

 •返回时不删除存储在栈顶的元素。

•如果堆栈为空,则引发异常。

 

isEmpty() and Peek()

• The stack is empty if the head pointer top is null. • Return without removing the element stored in

the head of the list.

• Throws an exception if the stack is empty.

 

 

实现

大小


效率


基于数组

固定尺寸

•如果已达到堆栈的大小限制,则阻止推送操作将项目添加到堆栈

 

基于Reference

动态尺寸

•不限制堆栈的大小

效率更高

 

Implementation

Size

Efficiency

Array-based

Fixed size

• Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached

 

Reference- based

Dynamic size

• Does not put a limit on the size of the stack

More efficient

 

 

代数表达式 - 堆栈应用(Algebraic Expressions – Stack Application)

 

代数表达式的三种语言 - 中缀表达式

•操作符出现在其操作数之间

•示例:a + b - 前缀表达式

•操作符出现在操作数之前

•示例:+ a b - 后缀表达式

•操作符出现在操作数之后•示例:a b +

 

Three languages for algebraic expressions – Infix expressions

• An operator appears between its operands

• Example: a + b – Prefix expressions

• An operator appears before its operands

• Example: + a b – Postfix expressions

• An operator appears after its operands • Example: a b +

 

完全带括号的中缀表达式

•完全括号表达式 

- 简单的语法

<infix> = <identifier> | (<infix> <operator> <infix>)

<operator> = + | - | * |/

<identifier> = a | b | ...... |ž

- 对程序员来说不方便

 

Fully Parenthesized InfixExpressions

• Fully parenthesized expressions 

– A simple grammar

<infix> = <identifier> | (<infix> <operator> <infix> ) 

<operator> = + | - | * | /

<identifier> = a | b | ... | z

– Inconvenient for programmers

 

完全带括号的中缀

表达式

•完全带括号的中缀代数表达式是一个中缀代数表达式,其中每个运算符及其参数都包含在括号中,如下所示:

没有完全括号化

完全括号化

a

( a )

a + b

( a + b )

a + b * c

( a +( b * c ))

( a + b )* c

(( a + b )* c )

a +( b * c )- d

(( a +( b * c )- d )

• A fully parenthesized infix algebraic expression is an infix algebraic expression where every operator and its arguments are contained in parentheses, as seen in following:

Not Fully Parenthesized

Fully Parenthesized

a

( a )

a + b

( a + b )

a + b * c

( a +( b * c ))

( a + b )* c

(( a + b )* c )

a +( b * c )- d

(( a +( b * c )- d )

完全带括号的中缀

表达式

•为避免歧义,中缀表示法通常需要

- 优先规则

- 关联规则 - 括号

•完全括号表达式不需要 - 优先规则

- 结合规则

 

Fully Parenthesized Infix

Expressions

• To avoid ambiguity, infix notation normally requires

– Precedence rules

– Rules for association – Parentheses

• Fully parenthesized expressions do not require – Precedence rules

– Rules for association

 

代数表达式

•将完全括号的中缀表达式转换为前缀表单

- 将每个操作员移动到由其相应的左括号标记的位置

- 删除括号 - 示例

•中缀表达式:((a + b)* c)•前缀表达式:* + a b c

 

Algebraic Expressions

• To convert a fully parenthesized infix expression to a prefix form

– Move each operator to the position marked by its corresponding open parenthesis

– Remove the parentheses – Example

• Infix expression: ((a + b) * c) • Prefix expression: * + a b c

 

将完全带括号的中缀表达式转换为后缀形式

- 将每个操作符移动到由其对应的右括号标记的位置

- 删除括号 - 示例

•中缀形式:((a + b)* c)•后缀形式:a b + c *

 

To convert a fully parenthesized infix expression to a postfix form

– Move each operator to the position marked by its corresponding closing parenthesis

– Remove the parentheses – Example

• Infix form: ((a + b) * c) • Postfix form: a b + c *

 

前缀和后缀表达式

 - 永远不需要

•优先规则

•关联规则

•括号

- 有

•简单的语法表达

•简单的识别和评估算法

 

Prefix and postfix expressions – Never need

• Precedence rules • Association rules • Parentheses

– Have

• Simple grammar expressions

• Straightforward recognition and evaluation algorithms

 

语法(Grammar)

<prefix> = <identifier> | <operator> <prefix> <prefix>

<operator> = + | - | * | / <identifier> = a | b | ...... |ž

 

一种评估前缀表达式的算法(An algorithm that evaluates a prefix expression)

evaluatePrefix(strExp)

ch = first character of expression strExp Delete first character from strExp

if(ch is an identifier){

             return value of the identifier

       }else if(ch is an operator named op) {

             operand1 = evaluatePrefix(strExp)

             operand2 = evaluatePrefix(strExp)

             return operand1 op operand2

}

 

在高级别,转换前缀的算法(At high-level, an algorithm that converts a prefix)

expression to postfix form

   if (exp is a single letter) {

      return exp

}else {

      return postfix(prefix1) +

             postfix(prefix2) +

}

 

将前缀表达式转换为后缀表达式(Convert a prefix expression to a postfix expression)

递归算法:

A recursive algorithm:

convert(pre)

ch = first character of pre Delete first character of pre if(ch is a lowercase letter){

return ch as a string }else{

}

 

评估Postfix表达式

•后缀计算器

- 要求您输入后缀表达式•示例:2,3,4,+,*

- 输入操作数时,计算器•将其推入堆栈

- 输入运算符时,计算器•将其应用于堆栈的前两个操作数

•从堆栈中弹出操作数

•将操作结果推送到堆栈

 

Evaluating Postfix Expressions

• A postfix calculator

– Requires you to enter postfix expressions • Example: 2, 3, 4, +, *

– When an operand is entered, the calculator • Pushes it onto a stack

– When an operator is entered, the calculator • Applies it to the top two operands of the stack

• Pops the operands from the stack

• Pushes the result of the operation on the stack

 

评估Postfix表达式

•评估作为字符串输入的后缀表达式

- 简化假设

•字符串是语法正确的后缀表达式

•没有一元操作符号

•不存在取幂运算符

•操作数是表示整数值的单个小写字母

 

Evaluating Postfix Expressions

• To evaluate a postfix expression which is entered as a string of characters

– Simplifying assumptions

• The string is a syntactically correct postfix expression

• No unary operators are present

• No exponentiation operators are present

• Operands are single lowercase letters that represent integer values

 

评估中缀表达式

•评估中缀表达式

- 将中缀表达式转换为后缀形式 

- 评估后缀表达式

 

Evaluating Infix Expressions

• To evaluate an infix expressions

– Convert the infix expression to postfix form

 – Evaluate the postfix expression

 

将中缀表达式转换为等效后缀表达式

•可以通过首先转换为等效的后缀表达式来评估中缀表达式

•关于从中缀转换为后缀的事实

- 操作数总是保持相同的顺序

- 操作员将仅相对于操作数“向右”移动

- 删除所有括号

 

Converting Infix Expressions to Equivalent Postfix Expressions

• An infix expression can be evaluated by first being converted into an equivalent postfix expression

• Facts about converting from infix to postfix

– Operands always stay in the same order with respect to one another

– An operator will move only “to the right” with respect to the operands

– All parentheses are removed

 

队列及其应用(Queues and Their Applications)

 

队列

•队列是一个集合,允许以先进先出顺序添加和删除元素。

•元素将按照添加顺序从队列中删除。

 

Queues

• A Queue is a collection that allows elements to be added and removed in a First-In-First-Out order.

• Elements are removed from the queue in the same order in which they are added.

 

队列应用程序

•许多应用程序的客户端需要服务,并在服务后离开。

•到达的客户端将添加到队列中。

•客户端从队列中删除,接收服务并离开。

 

Queue Applications

• Many applications have clients that arrive needing service, and depart after being served.

• Clients who arrive are added to a queue.

• Clients are removed from the queue, receive service, and leave.

 

队列应用程序

•打印服务器可以为计算机网络中的许多工作站提供服务。 打印服务器使用队列来保存等待发送到打印机的打印作业请求。

•模拟通过收费站的汽车将使用一个队列来保持排队的汽车以支付通行费。

 

Queue Applications

• A print server may service many workstations in a computer network. The print server uses a queue to hold print job requests waiting to be sent to the printer.

• A simulation of cars going through a toll booth would use a queue to hold cars lining up to pay toll.

 

队列操作

•enqueue(E x):将项添加到队列中

•E dequeue():从队列中删除并返回一个项目

•E peek():返回但不删除下次调用dequeue()将返回的项目

•boolean empty():检查队列是否为空

 

Queue Operations

• enqueue(E x) : adds an item to the queue

• E dequeue() : remove and return an item from the queue

• E peek() : return, but do not remove, the item that will be returned by the next call to dequeue()

• boolean empty() : check to see if the queue is empty

 

队列的实现

•基于数组的实现使用数组来保存队列元素。

•链接列表实现使用链接列表来保存队列元素。

 

Implementation of Queues

• An array-based implementation uses an array to hold queue elements.

• A linked list implementation uses a linked list to hold queue elements.

 

阵列的概念视图

•队列可以被视为项目的线性序列

- 在一端添加新项目(队列的后部)

- 从另一端(队列的前面)删除新项目

 

Conceptual View of an Array

• A queue can be viewed as a linear sequence of items where

– new items are added at one end (the rear of the queue)

– new items are removed from the other end (the front of the queue)

 

跟踪队列的大小

•跟踪队列中的元素数量有助于确定队列是否为空,以及队列是否已满。

•整数变量用于跟踪大小:

int size = 0;

•变量大小以入队递增,并由出队递减。

 

Tracking the Size of the Queue

• Tracking the number of elements in the queue is useful in determining if the queue is empty, and when the queue is full.

• An integer variable is used to track the size:

int size = 0;

• The variable size is incremented by enqueue, and decremented by dequeue.

 

实施入队

•enqueue的正确实现必须 - 增加size变量。

- 在后面添加新项目。

- 后部增加,当后部到达阵列末端时环绕为0。

 

Implementation of enqueue

• The correct implementation of enqueue must – Increment the size variable.

– Add the new item at rear.

– Increment rear and wrap around to 0 when rear reaches the end of the array.

 

出列的实施

必须正确实现dequeue - 减小size变量。

- 取下前面的物品。

- 当前端到达阵列末端时,增加前沿并回绕到0。

 

Implementation of dequeue

The correct implementation of dequeue must – Decrement the size variable.

– Remove an item at front.

– Increment front and wrap around to 0 when front reaches the end of the array.

 

基于参考的队列实现(Reference Based Implementation of Queues)

 

循环链表可用于实现队列。

•链接列表的末尾lastNode用作队列的后面。

•链表的头部用作队列的前端。

 

A circular linked list can be used to implement a queue.

• The end of the linked list , lastNode, is used as the rear of the queue.

• The head of the linked list is used as the front of the queue.

 

实施入队

•enqueue的正确实现必须

1.在后面添加新项目。

2.设置lastNode引用。

如果队列为空,

 

Implementation of enqueue

• The correct implementation of enqueue must 

1. Add the new item at rear.

2. Set lastNode reference.

If the queue is empty,

 

实施入队

1.创建下一个引用前面的Debby,Alan。

2.将lastNode的下一个设置为Debby。 

3.将lastNode设置为Debby。

 

Implementation of enqueue

1. Create Debby who next references front, Alan.

2. Set next of lastNode to Debby. 

3. Set lastNode to Debby.

 

出列的实施

•必须正确实施出列 - 删除前面的项目。

- 设置lastNode的下一个引用。

•如果队列为空,则dequeue将失败。

 

Implementation of dequeue

• The correct implementation of dequeue must – Remove an item at front.

– Set next reference of lastNode.

• dequeue will fail if the queue is empty.

 

基于改进参考的队列实现

•链表可用于实现队列。

•链表的头部用作队列的前端。

•链接列表的末尾用作队列的后部。

 

Alterative Reference Based Implementation of Queues

• A linked list can be used to implement a queue.

• The head of the linked list is used as the front of the queue.

• The end of the linked list is used as the rear of the queue.

 

基于数组与基于参考比较

 

基于数组的实现

固定尺寸

•防止入队操作

如果数组已满,则将项添加到队列中

 

基于参考的实现

动态尺寸

•不对此施加限制

入队行动

更有效率

 

 

Array based vs Reference based

 

 

Array based implementation

Fixed size

• Prevents the enqueue operation

from adding an item to the queue if the array is full

 

Reference based implementation

Dynamic size

• Does not impose this restriction on

the enqueue operation

More efficient

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值