chapter 3 : stacks 3.1 understand stacks 3.2 implement the operation of stack by : suzila yusof

22
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

Upload: duane-ray

Post on 23-Dec-2015

234 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

CHAPTER 3 : STACKS3.1 Understand Stacks

3.2 Implement the operation of stack

By : Suzila Yusof

Page 2: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

CHAPTER 3 : STACKSPART I

By : Suzila Yusof

Page 3: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack 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.

Page 4: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

EXAMPLE OF STACKS

Stack of coins

Stack of books

Stack of plates

Page 5: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 6: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 7: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 8: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 9: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 10: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 11: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 12: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

LAST-IN-FIRST-OUT PROTOCOL

One common explanation for this terminology is the operation of a spring-loaded stack of plates in a cafeteria:

Page 13: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 14: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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)

Page 15: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 16: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 17: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 18: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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

Page 19: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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:

Page 20: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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.

Page 21: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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:

Page 22: CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof

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