8/8/2019 DS L8 Stack&Queue
1/23
Data Structure
Stacks and Queues
8/8/2019 DS L8 Stack&Queue
2/23
Introduction to Stack
An stack is a ordered list in which insertion and deletions
are made at one end.
The location of stack end is stored in a variable
called top.
Given a stack S=(a0, a1, , an-1), we say a0 is the
bottom element, an-1 is the top element.
8/8/2019 DS L8 Stack&Queue
3/23
Stack
Last-In-First-Out (LIFO)
Add (A)
Add (B)
Add (C)
Add (D)
Add (E)
Pop ()
Which one is popped by the function Pop()?
A
B
C
D
E
Add a new item on the top of stack.
Get the item at the top of stack and
remove it from the stack.top
0
1
2
3
4
5
8/8/2019 DS L8 Stack&Queue
4/23
Examples of Stack
Stack
0 1 2 3 4
0
1
2
3
4
(0, 0)
(0, 1)
(1, 1)
(1, 0)
(2, 0)
(2, 1)
(3, 1)
(3, 0)(4, 0)
(4, 1)
(3, 2)
8/8/2019 DS L8 Stack&Queue
5/23
System Stack
System stack is used by a program at runtime to process
function calls.
Whenever a function is invoked, the program create a
structure referred to as an activation recordorstack
frame.
8/8/2019 DS L8 Stack&Queue
6/23
System Stack
- Frame pointer (fp): a pointer to current stack frame.
- Placed on top of the system stack
Previous frame pointer:
- a pointer to the previous stack frame of the invoking
function.
Return address:
- The location of the statement to be executed after the
function
terminates.
8/8/2019 DS L8 Stack&Queue
7/23
System Stack
local varaibles
return address
previous frame pointer
main:
al:
fp
return address
previous frame pointer
return address
void al()
{
return;
}
int main ()
{
al();
return 0;
}
previous frame pointer
return address
8/8/2019 DS L8 Stack&Queue
8/23
The Stack ADT
class Stack
{
public:
Stack(int=10);~ Stack();
void ush(int d);
int Pop();
//Check if the stack is emptybool IsEmpty();
};
8/8/2019 DS L8 Stack&Queue
9/23
Implementing Stack Using Array
class Stack
{
private:Resize();
int top, capacity;
int *stackarray;};
8/8/2019 DS L8 Stack&Queue
10/23
Constructor for Stack
The variable top indicate next element to be popped.
- Initially, top is assigned -1.
Stack::Stack(int s)
{
capacity= s;
top = -1;allocate memory space of size s forstackarray;
}
8/8/2019 DS L8 Stack&Queue
11/23
Push() and Pop()
Pop
- Immediately return the value at the top.
For example:
int d= stackarray[top];
top--;
return d;
Before returning the value, first check if the stack is empty.If so, throw an exception.
8/8/2019 DS L8 Stack&Queue
12/23
Push() and Pop()
Push:
Note that if the stack is full, invoke Resize() to enlarge the
capacity of array
Since top indicates the element to be popped, you mustincrease top by 1 first when pushing a new element.
void Stack::Push(int num)
{
if (top+1 >= capacity)Resize();
top++;
stackarray[top] = num;
}
8/8/2019 DS L8 Stack&Queue
13/23
How to Resize the Array?
void Stack::Resize()
{
newsize = capacity* 2;
Allocate a new array of size newsize;
Copy elements from stackarrayto the new array;
deallocate stackarray;
assign the pointer of the new array to stackarray;
capacity= newsize;
}
8/8/2019 DS L8 Stack&Queue
14/23
Output Stack Elements
A
B
C
D
E
E D C B A
The order is reversed and the stack becomes empty!
E
D
C
B
A
Buffer
EDCBA
8/8/2019 DS L8 Stack&Queue
15/23
The Queue ADT
class Queue
{
public:
Queue (int=10);
~ Queue();
void Push(int d);
int Pop();
//Check if the queue is empty
bool IsEmpty();
bool IsFull();
};
8/8/2019 DS L8 Stack&Queue
16/23
Queue
First-In-First-Out (FIFO)
Push (A)
Push (B)
Push (C)
Push (D)
Push (E)
Pop ()A
B
C
D
E
rearfront
8/8/2019 DS L8 Stack&Queue
17/23
Implementation Using Array
Suppose the length of array is n.
This approach runs into a problem when rear equals
to n-1.
Shifting all elements to the left could take O(n) time.
A A B A B C
rear rear rear
B C
rearfront
B C D
rearfront
front
8/8/2019 DS L8 Stack&Queue
18/23
Circular Queue
A
B C
D
0 1 2 3 4 5 6 7
A B C D
0
1
2 3
4
5
67
rear
front
rear
rear rear
front
front (1)
Push (A)
Push (B)
Push (C)
Push (D)
Pop ()Pop ()
Pop ()
Push (E)
Push (F)Push (G)
Push (H)
Push (I)
rear
rear
frontfront
E
FG
H
I
E
rear
F
rear
G
rear
H I
8/8/2019 DS L8 Stack&Queue
19/23
Implementing Circular Queue
class Queue
{
private:Resize();
int front, rear;
int top, capacity;
int *queuearray;
};
8/8/2019 DS L8 Stack&Queue
20/23
Constructor for Circular Queue
Initially, rearand frontare assigned 0.
(rear+1) indicates the next location to insert; (front+1)
indicates the next location to insert.
This gives a situation to determine if the queue is empty.
Queue is empty: rear == front
8/8/2019 DS L8 Stack&Queue
21/23
Constructor for Circular Queue
0
1
2 3
4
5
67
rear
front
0
1
2 3
4
5
67
front
rear
A
B C
D
E
FGrear
H
How do know whether the queue is
empty or full?
8/8/2019 DS L8 Stack&Queue
22/23
Constructor for Circular Queue
Queue:: Queue(int s)
{
capacity= s;
front= rear= 0;allocate memory space of size s forqueuearray;
}
8/8/2019 DS L8 Stack&Queue
23/23
IsEmpty() and Pop()
Before returning the value, first check if
the queue is empty.
bool Queue:: IsEmpty(){
if (front== rear)
return true;
return false;
}