Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Stacks, Queues, and Linked Lists
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS Data Structures II Review COSC 2006 April 14, 2017
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 18: Stacks and Queues
Stacks, Queues, and Deques
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
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks Dr. Youssef Harrath
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 3 Introduction to Collections – Stacks Modified
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Chapter 18: Stacks and Queues
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
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.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
STACKS & QUEUES for CLASS XII ( C++).
Sections 3.4 Formal Specification
Data Structures Using C++ 2E
Stacks.
Stacks and Queues Chapter 4.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Data Structures Array Based Stacks.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Data Structures Array Based Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks, Queues, and Deques
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures

Stacks Lists of homogeneous elements Addition and deletion of elements occur only at one end, called the top of the stack The middle elements of the stack are inaccessible Computers use stacks to implement method calls Real life examples?

Stacks Figure 17-1 Various types of stacks

Stacks Stacks are also called Last In First Out (LIFO) data structures Operations performed on stacks Push: adds an element to the stack Pop: removes an element from the stack Peek: looks at the top element of the stack

Stacks Figure 17-3 Stack operations

Stacks Figure 17-4 UML diagram of the interface StackADT

StackException Class Adding an element to a full stack and removing an element from an empty stack would generate errors or exceptions Stack overflow exception Stack underflow exception Classes that handle these exceptions StackException extends RunTimeException StackOverflowException extends StackException StackUnderflowException extends StackException

Implementation of Stacks as Arrays The array implementing a stack is an array of reference variables Each element of the stack can be assigned to an array slot The top of the stack is the index of the last element added to the stack To keep track of the top position, declare a variable called stackTop

Implementation of Stacks as Arrays Figure 17-5 UML class diagram of the class StackClass

Implementation of Stacks as Arrays Figure 17-6 Example of a stack

Stacks (Methods) Default constructor Method initializeStack public StackClass() { maxStackSize = 100; stackTop = 0; //set stackTop to 0 //create the array list = (T[]) new Object[maxStackSize]; } Method initializeStack public void initializeStack(){ for (int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0;

Stacks (Methods) Method isEmptyStack Method isFullStack public boolean isEmptyStack(){ return (stackTop == 0); } Method isFullStack public boolean isFullStack() { return (stackTop == maxStackSize);

Stacks (Methods) Method push Method pop public void push(T newItem) throws StackOverflowException { if (isFullStack()) throw new StackOverflowException(); list[stackTop] = newItem; //add newItem stackTop++; //increment stackTop } Method pop public void pop() throws StackUnderflowException { if (isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null;

Stacks (Methods) Method peek public T peek() throws StackUnderflowException { if (isEmptyStack()) throw new StackUnderflowException(); return (T) list[stackTop - 1]; }

Linked List Implementation of Stacks Arrays have fixed sizes Only a fixed number of elements can be pushed onto the stack Dynamically allocate memory using reference variables Implement a stack dynamically Similar to the array representation, stackTop is used to locate the top element stackTop is now a reference variable

Linked Implementation of Stacks (continued) Figure 17-13 Nonempty linked stack

Stacks (Methods) Default constructor Method initializeStack public LinkedStackClass() { stackTop = null; } Method initializeStack public void initializeStack() {

Stacks (Methods) Method isEmptyStack Method isFullStack public boolean isEmptyStack() { return (stackTop == null); } Method isFullStack public boolean isFullStack() { return false;

Stacks (Methods) Method push Method pop public void push(T newElement) { StackNode<T> newNode; //reference variable newNode = new StackNode<T>(newElement, stackTop); //insert before stackTop stackTop = newNode; } Method pop public void pop() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); stackTop = stackTop.link; //advance stackTop

Stacks (Methods) Method peek public T peek() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); return stackTop.info; }