240 likes | 391 Views
CHAPTER 3 : STACKS. 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof. CHAPTER 3 : STACKS. PART I By : Suzila Yusof. Stack Model.
E N D
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : SuzilaYusof
CHAPTER 3 : STACKS PART I By : SuzilaYusof
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 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. PUSH F TOP
Last-In-First-Out Protocol • If an item is to be removed (pop) from the stack F will be deleted (popping) first. POP TOP F
Last-In-First-Out Protocol • Entries are removed from the stack in the reverse order they were put into the stack. POP TOP 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. TOP Figure 4.2
Array Implementation Of Stack • 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].
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 PUSHOperation 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