1 / 22

CHAPTER 3 : STACKS

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.

feleti
Download Presentation

CHAPTER 3 : STACKS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : SuzilaYusof

  2. CHAPTER 3 : STACKS PART I By : SuzilaYusof

  3. 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.

  4. Example of stacks Stack of coins Stack of books Stack of plates

  5. 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.

  6. 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.

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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

  12. Last-In-First-Out Protocol • One common explanation for this terminology is the operation of a spring-loaded stack of plates in a cafeteria:

  13. 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.

  14. 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)

  15. 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

  16. 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.

  17. 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

  18. 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].

  19. 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:

  20. 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.

  21. 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:

  22. 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

More Related