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

Slides:



Advertisements
Similar presentations
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Advertisements

C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Reported By: Michelle B. Alambra. Topics What is a stack? Stack Representation Stack Operations Example of a Stack.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Summary of lectures (1 to 11)
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Object Oriented Data Structures
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks And Queues Chapter 18.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures & Algorithms
CHP-3 STACKS.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Stacks and Queues Chapter 4.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
STACKS.
Pointers and Linked Lists
Stacks, Queues, and Deques
Data Structures and Database Applications Stacks in C#
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks, Queues, and Deques
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Chapter 4 Stacks and Queues
Stack Implementations
Presentation transcript:

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

S TACK M ODEL 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.

E XAMPLE OF STACKS Stack of coins Stack of books Stack of plates

B ASIC S TACK 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.

B ASIC S TACK O PERATIONS ~ P USH 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.

B ASIC S TACK OPERATIONS ~ P OP 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.

L AST -I N -F IRST -O UT P ROTOCOL 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

L AST -I N -F IRST -O UT P ROTOCOL 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

L AST -I N -F IRST -O UT P ROTOCOL If an item is to be removed (pop) from the stack F will be deleted (popping) first. E D C B A TOP POP F

L AST -I N -F IRST -O UT P ROTOCOL Entries are removed from the stack in the reverse order they were put into the stack. E D C B A TOP POP E, D, C, B, A

L AST -I N -F IRST -O UT P ROTOCOL One common explanation for this terminology is the operation of a spring-loaded stack of plates in a cafeteria:

L AST -I N -F IRST -O UT P ROTOCOL 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.

S TACK I MPLEMENTATION 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)

L INKED L IST I MPLEMENTATION O F S TACK 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

L INKED L IST I MPLEMENTATION O F S TACK 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.

A RRAY I MPLEMENTATION O F S TACK 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 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]. A RRAY I MPLEMENTATION O F S TACK

T HE P USH O PERATION 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:

A LGORITHM F OR T HE PUSH O PERATION 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.

T HE POP O PERATION 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:

A LGORITHM F OR T HE POP O PERATION 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