Académique Documents
Professionnel Documents
Culture Documents
Objectives
Introduction
Define a stack
Define a queue
Operations
Stack operations
Queue operations
Java interfaces
PureStack
interface
PureQueue
interface
Implementations
Array-based
implementation
s
Applications
Stack
applications
Queue
applications
What is a stack?
A stack is a finite sequence of
elements in which the only element
that can be removed is the element
that was most recently inserted.
A stack is a data structure that keeps objects in
Last-In-First-Out (LIFO) order
Objects are added to the top of the stack
Only the top of the stack can be accessed
Operations on a Stack
Operation
Description
push
pop
peek
isEmpty
size
Pop
Remove the element at the top of the
stack push an element push another
empty stack
pop
top
top
top
B
A
top
Stack Operations
1) push add data onto the stack
2) Algorithm :
- Increase top by 1
- Add new item into top location
Stack Operations
1) pop remove data onto the stack
2) Algorithm :
- Store the top data into a
temporary storage
- Decrease top by one
Stack Operations
1) stackEmpty check whether the
stack is empty or not
2) Algorithm :
- Is top = -1 ?
true
(stack is empty!)
If yes, return
Stack Operations
1) stackFull check whether the stack
is full or not
2) Algorithm :
- Is top = stackSize -1 ? If yes,
return true
(stack is full!)
Stack Operations
Assume a simple stack for integers.
Stack s = new Stack();
s.push(12);
s.push(4);
s.push( s.top() + 2 );
s.pop()
s.push( s.top() );
//what are contents of stack?
10
Stack Operations :
push operation
j avaxc
cxav a j
OUTPUT
c
x
a
v
a
j
Exercise
Show the output and the contents of stacks S and T(using
diagrams)after each of the following sequence operations:
a. push(S,2);
b. push(S,5);
c. push(T,3);
d. push(T,4);
e. pop(topItem,S);
f. System.out.println(topItem);
g. getTop(topItem,T);
h. System.out.println (topItem);
i. push(S,topItem);
j. push(S,5);
k. pop(topItem,T);
l. System.out.println (topItem);
m. push(T,7);
Lab Exercise
Problem :
Convert a decimal value to a binary form.
Instruction:
Write a main() to accept a decimal value
from the user. Convert the decimal value
to its binary form. During the conversion,
call the push() to push the binary value 1
or 0 on to the stack. Lastly call pop()
repeatedly to pop the data out from the
stack and display.
Interface
A list of abstract methods and constants
must be public
constants must be declared as final static
The PureStack
Interface
/**
* Removes the top element from this PureStack object.
*
* @return the element removed.
* @throws NoSuchElementException if this PureStack
*
object is empty.
*/
E pop();
/**
* Returns the top element on this PureStack object.
*
* @return the element returned.
* @throws NoSuchElementException if this PureStack
*
object is empty.
*/
E peek();
} // interface PureStack
Generic Types
What is this <E> in the interface definition?
It represents a generic type
For generality, we can define a class ( for interface)
based on a generic type rather than as actual type
Example: a Stack for object of type E
Implementing an Interface
One or more classes can implement
an interface, perhaps differently
A class implements the interface by
providing the implementations
(bodies) for each of the abstract
methods
Uses the reserved word implements
followed by interface name
Stack implementation
issues
What do we need to implement a
stack?
A data structure (container) to hold
the data elements
Something to indicate the top of
the stack
Implementation of Stacks
Any list implementation could be
used to implement a stack
Arrays (static: the size of stack is given
initially)
Linked lists (dynamic: never become
full)
Array Implementation of a
Stack
The container will be an array to hold the
data elements
Data elements are kept contiguously at one end
of the array
Array Implementation of a
Stack
A stack s with 4 elements
0
..
s
stack
4
top
6
..
stack
5
top
Array Implementation of a
Stack
6
..
stack
4
top
6
..
stack
3
top
stack
size
s.push(41);
s.push(56);
s.push(32);
s.push(17);
stack
4
size
41 56
32 17
s.pop();
s.pop();
s
stack
2
size
41 56
Managing Capacity
An array has a particular number of cell
when it is created (its capacity), so arrays
capacity is also the stacks capacity
What happens when we want to push a
new element onto a stack that is full?
The push method could throw an exception
It could return status indicator (i.e. a boolean
value true or false, that indicates whether the
push was successful or not)
It could automatically expand the capacity of
the array.
Stack Applications
Stack Applications
Used of stack in computing
Word Processors , editors
To check expressions or string of text for
matching parentheses / brackets
i.e.
if (a==b)
{ c=(d+e) * f; }
To implement undo operations
Keeps track of the most recent operations
Compilers
Old compilers:
Infix
language
Machine
Machine
Infix
Postfix
a+b
ab+
a+b*c
abc*+
a*b+c
ab*c+
(a + b) * c
ab+c*
if its an operator if the stack is empty, push it on the stack else pop operato
greater or equal precedence and append them to the postfix string, stopping w
a ( is reached, an operator of lower precedence is reached, or the stack is em
then push the operator on the stack
. if its a ) pop operators off the stack, appending them to the postfix string,
a ( is encountered and pop the ( off the stack
. when the end of the infix string is reached pop any remaining operators off
stack and append them to the postfix string
char Stack
empty
empty
-(
-(
-(*
-(*
-(+
-(+
-*
-*(
-*(
-*(-*(-*(-/
-*(-/
empty
Postfix String
null
7
7
7
72
72
723
723*
723*5
723*5+
723*5+
723*5+
723*5+8
723*5+8
723*5+84
723*5+84
723*5+842
723*5+842/-*-
Rule U
1
3
2
1
3
3
3
1
4
3
2
1
3
1
3
1
4&5
Balancing Symbols
openers [ ( { and closers ] ) }
public class A
public static void main(String[ args
System.out PRINTln( "Hello" );
for( int j = 0; j < 6 j++ ) j++
doub x = 0.0;
inTeger j = 0;
System.out.println( "Goodbye" );
}
}
Example
Process these characters, which represent only the
openers and closers in a short Java program:
{{([])}}
As the first four characters are read all openers
push each onto the stack
[
(
{
{
(
{
{
( matches )
Then ' )' is found, so pop the stack
Since the top symbol '(' matches the
closer ')', no error needs to be reported.
The stack now has two opening curly
brakets
{
{
Evaluation of Postfix
Expressions
Evaluate
3 4 - 5 3 * -
s.push(3);
3
s.push(4);
right = s.pop(); // found operator - so pop
left = s.pop();
-1
s.push(left - right);
3 - 4
s.push(5);
s.push(3);
right = s.pop();
left = s.pop();
s.push(left * right);
5 * 3
5
-1
15
-1
Continue with 15
-1
Queue
Queue
A queue is a finite sequence of elements in
which:
Insertion occurs only at the back;
Deletion occurs only at the front.
A Conceptual View of a
Queue
Rear of Queue
(or Tail)
Adding an Element
Front of Queue
(or Head)
Removing an Element
67
Queue Operations
1. Enqueue : Add an element to the tail of the
queue
Dequeue
Remove an element from the front of the queue
Remove
(Dequeue)
front
rear
Insert
(Enqueue)
Front
Matt
Back
Front
Andrew
Back
Front
Matt
Front
Dequeue
Andrew
Enqueue Samira
Samira
Back
Andrew
Samira
Back
/**
* Returns the front element in this PureQueue
* object.
*
* @return the element returned.
*
* @throws NoSuchElementException if
*
PureQueue object is empty.
*
*/
E front();
} // interface PureQueue
Queue Example
Operation
enqueue(5)
enqueue(3)
dequeue()
enqueue(7)
dequeue()
front()
dequeue()
dequeue()
isEmpty()
enqueue(9)
enqueue(7)
size()
enqueue(3)
enqueue(5)
dequeue()
Output
3
7
7
error
true
Q
(5)
(5, 3)
(3)
(3, 7)
(7)
(7)
()
()
()
(9)
(9, 7)
(9, 7)
(9, 7, 3)
(9, 7, 3, 5)
(7, 3, 5)
75
Implementation of Queue
There are many ways to implement a
queue:
1. Array
2. Circular queue
3. Linked List
back
front
front
Enqueue(3)
back
back
Enqueue(6)
front
Enqueue(9)
back
back
front
Dequeue()
front
Dequeue()
back =
-1
front
Dequeue()
Array implementation of
queues
A queue is a first in, first out (FIFO) data
structure
This is accomplished by inserting at one end
(the back) and deleting from the other (the
front)
0
1
2
3
4
5
6
7
myQueue:
front = 0
17
23
97
44
back = 3
front = 0
Initial queue:
17
23
97
44
After insertion:
17
23
97
44
333
23
97
44
333
After deletion:
front = 1
back = 4
Notice how the array contents crawl to the right as elements are
inserted and deleted
This will be a problem after a while!
Circular arrays
We can treat the array holding the queue
elements as circular (joined at the ends)
myQueue:
44
55
back = 1
5
11
6
22
7
33
front = 5
44
55
66
77
88
11
rear = 4
22
33
front = 5
front = 5
44
55
66
77
88
11
count = 8
rear = 4
6
22
7
33
front = 5
44
55
66
77
rear = 3
5
11
6
22
7
33
front = 5
Applications of Queues
Operating systems use queues to
sequence tasks waiting for a scarce
resource
Printer queue
Tasks waiting for CPU
85