0% found this document useful (0 votes)
3 views

Ch3 - Stack Using Array (1) (2)

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Ch3 - Stack Using Array (1) (2)

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 3
Stack Data Structure Using Array

Faculty of Information Technology - Computer Science Department 3


Outline

 Stack Specifications Structure


 Stack Methods using Array
 Methods Implementation
 Application: a Desk Calculator

Faculty of Information Technology - Computer Science Department 4


Stack Specifications Structure

 A stack is an ordered list in which all insertions and deletions of entries are made at one
end, called the top of the stack.
 When we insert an item, we say that we push it onto the stack.

 When we delete an item, we say that we pop it from the stack.


 Last-In-First-Out(LIFO):

 The last item pushed onto a stack is the first item popped.

 A stack is a dynamic object, it expands and shrinks with each push and pop.

Faculty of Information Technology - Computer Science Department 5


An Example for Inserting and Deleting Elements in a Stack

top top
A M
Q Q top R top R
Q top top
Push Push(A Pop Pop Push Push
(Q) ) () () (R) (M)
empt
y

Faculty of Information Technology - Computer Science Department 6


An Example for Inserting and Deleting Elements in a Stack

Faculty of Information Technology - Computer Science Department 7


Abstract Data Type (ADT)
 An abstract data type (ADT) is a data type that is organized in such a way that
the specification of the objects and operations on the objects are separated
from the representation of the objects and implementation of the operations.

ADT Definition for Stack:


1. Objects:
 A finite ordered list with zero or more elements.
 top indicator indicates the current number of elements in the stack.

Faculty of Information Technology - Computer Science Department 8


ADT Definition for Stack:
2. Methods (operations) implemented using Array:
 isEmpty(): Check if the stack is empty.

 isFull(): Check if the stack is Full.

 push(): Add an item onto the top of a stack.

 pop(): Remove the topmost item from a stack.

 length(): Retrieve the size of a stack.

 peak(): Returns the top element on the stack but does not remove it from the
stack.

Faculty of Information Technology - Computer Science Department 9


Methods Implementation

10
Faculty of Information Technology - Computer Science Department
Methods Implementation

11
Faculty of Information Technology - Computer Science Department
Main Class

12
Faculty of Information Technology - Computer Science Department
Main Class - Outputs

Faculty of Information Technology - Computer Science Department 13


Application: a Desk Calculator
Terms:
 Operators: +, -, ×, /, %.
 Operands: 2,3,4,…
 Precedence of Operators:

1. unary +, - (positive,
negative)
2. ×, /, %
3. +, - .

Faculty of Information Technology - Computer Science Department 14


Application: a Desk Calculator
Infix Expression
 A binary operator is placed in between its two operands E.g.
2+3*4
Disadvantage:
 Use parentheses.
 Need precedence.
Postfix Expression
 Each operator appears after its operands E.g.: 234*+
 The order of operands is the same in infix and postfix.
Advantage
 Precedence has been considered.
Prefix Expression
 Each operator appears before its operands E.g.: +7*54
15
Faculty of Information Technology - Computer Science Department
Conversion of Infix Expressions to Postfix Expressions
1. Initialize the stack by pushing # (end token)
2. Scan the next token from the infix expression
3. Test the token

1. if the token is one operand, add it on the postfix exp.


2. if the token is a left-hand parenthesis, push it on the stack.
3. if the token is a right-hand parenthesis, pop all entries from the operator stack and add
them

on the postfix expression until the left-hand parenthesis is popped.


4. if the token is the end token, pop all entries and add them on the postfix expression.

5. otherwise, pop from the stack, and add on the postfix queue all operators whose

priority is higher or equal to the priority of the token and then push the token.
Faculty of Information Technology - Computer Science Department
4. Repeat steps 2 and 3 until the token is the end token. 16
Conversion of Infix Expressions to Postfix Expressions

17
Faculty of Information Technology - Computer Science Department
Evaluate the Postfix Expression
Step 1: Repeatedly get token from postfix expression
Step 2: If the token is an operand: Push the value to the stack
Step 3: If it is an operator: Pop 2 values, evaluate, and push the
result.

Ex : AB*CD-E/+ , given that A=5, B=3, C=8, D=6, E=2

18
Faculty of Information Technology - Computer Science Department

You might also like