chapter 3 : stacks 3.1 understand stacks 3.2 implement the operation of stack by : suzila yusof
TRANSCRIPT
CHAPTER 3 : STACKS3.1 Understand Stacks
3.2 Implement the operation of stack
By : Suzila Yusof
CHAPTER 3 : STACKSPART I
By : Suzila Yusof
STACK MODEL
A stack is a list with the restriction that insertions and deletions can be performed in only one position >>end of the list >>top
We must first decide which end of the stack is to be designed as its top (where item can be added or deleted)
New item may be put (added) onto the top of the stack while items which are already at the top of the stack may be removed (deleted).
Item are removed from a stack in the reversed order of that in which they were inserted into the stack.
EXAMPLE OF STACKS
Stack of coins
Stack of books
Stack of plates
BASIC STACK OPERATIONS
Two basic stack operations are :
i. Push is the term use to insert an element into the stack.
ii. Pop is the term use to delete or retrieve an element from a stack.
BASIC STACK OPERATIONS ~ PUSH
Push - adds an item at the top of the stack
New item becomes the top
A potential problem that may occur with the
push operation is stack overflow.
Stack overflow is the condition resulting from
trying to push(add) an item onto a full stack.
BASIC STACK OPERATIONS ~ POP
Pop - removes the item at the top of the
stack
The next(older) item in the stack becomes
the top
A potential problem that occur with the pop
operation is stack underflow.
Stack underflow is the condition resulting
from trying to pop (remove) an item from an
empty stack.
LAST-IN-FIRST-OUT PROTOCOL
A stack using Last-In-First-Out (LIFO) protocol Figure 4.1 shows a stack of items (A, B, C, D,
E ).
Figure 4.1
E
D
C
B
A
TOP
LAST-IN-FIRST-OUT PROTOCOL
If new item is to be added(push) to the stack, it will be placed on top of E.
F
E
D
C
B
A
TOP
PUSH
F
LAST-IN-FIRST-OUT PROTOCOL
If an item is to be removed (pop) from the stack F will be deleted (popping) first.
E
D
C
B
A
TOPPOP
F
LAST-IN-FIRST-OUT PROTOCOL
Entries are removed from the stack in the reverse order they were put into the stack.
E
D
C
B
A
TOPPOP
E, D, C, B, A
LAST-IN-FIRST-OUT PROTOCOL
One common explanation for this terminology is the operation of a spring-loaded stack of plates in a cafeteria:
LAST-IN-FIRST-OUT PROTOCOL
This stack functions in the following way : if a plate is added to the stack,
those below it are pushed down and cannot be accessed.
If a plate is removed from stack, those below it pop up one position.
The stack becomes empty when there are no plates in it.
The stack is full if there is no room in it for more plates.
STACK IMPLEMENTATION
There are ways to implement a stack.
One is by static implementation using an
array
The other is by dynamic implementation
using pointers (linked list)
LINKED LIST IMPLEMENTATION OF STACK
A linked list is used to implement a stack dynamically.
Its size can grow or shrink during program execution.
When a stack is represented as a linked list, the first entry of the linked list will be at the top of the stack.
The second entry of the linked list will be the second entry of the stack, and so on.
The advantage of a linked list implementation over an array implementation is that, there is no limit on the number of entries one can add to the stack
LINKED LIST IMPLEMENTATION OF STACK
Basically, the push operation inserts an item at the top of stack, i.e.: in front (head) of the linked list.
The pop operation does the reverse ; it deletes an element from the top of the stack, i.e.: from the front (head) of the linked list.
ARRAY IMPLEMENTATION OF STACK
An alternative way to implement a stack is by using an array.
For array implementation, we need to declare the array size in advance.
Figure 4.2 illustrates an array representation of a stack.
45
68
99
stack[4]
stack[3]
stack[2]
stack[1]
stack[0]
TOP
Figure 4.2
The pointer variable TOP in the figure points to the top of the stack (STACK).
The variable MAXSTACK is used to store the maximum number of elements that the stack can hold.
Item are stored from the bottom of the stack (array) beginning at STACK[0].
ARRAY IMPLEMENTATION OF STACK
THE PUSH OPERATION
Before executing the push function (operation), we must test for overflow condition.
If the stack is full (i.e., when TOP+1=MAXSTACK) it is said to overflow.
A stack overflow is the condition resulting from trying to push (insert) an item onto a full stack.
To illustrate, let the variable TOP maintain the index of the current top of stack where insertions may occur. The push operation may be implemented using the following algorithm:
ALGORITHM FOR THE PUSH OPERATION
1. Check for stack overflow. If stack overflow
occurs, disallow further push operations.
2. Increment TOP pointer variable (i.e., TOP =
TOP+1) and assign new item to topmost
stack element (i.e., STACK[TOP] = ITEM).
3. Exit.
THE POP OPERATION
Before executing the pop operation, we must test for stack underflow condition. If the stack is empty, (i.e., when TOP = -1) then an underflow has occurred.
A stack underflow is the condition resulting from trying pop (remove) an item from an empty stack.
To illustrate, let the variable TOP maintain the index of an the current top of stack where deletions may occur.
The pop operation may be implemented using the following algorithm:
ALGORITHM FOR THE POP OPERATION
1. Check for stack underflow. If stack underflow occurs, disallow further pop operations.
2. Assign item pointed to as the topmost stack element (i.e., ITEM=STACK[TOP]) for saving the value of item which one to pop(remove).
3. Decrement TOP pointer variable by one (i.e., TOP=TOP-1).
4. Exit