COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Chapter 6 Queues and Deques.
Stack & Queues COP 3502.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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 16 Stacks and Queues Saurav Karmakar Spring 2007.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
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.”
1 / 23 COP 3540 Data Structures with OOP Chapter 4 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 STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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’
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Data Structures & Algorithms
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
COP 3540 Data Structures with OOP
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“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.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Stacks.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Data Structure By Amee Trivedi.
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Chapter 19: Stacks and Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
Presentation transcript:

COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Abstract Data Structures An array is a concrete structure  The Java language defines Structure Rules for storing and retrieving values Limitations  Uses of an array are limitless Limited only by the programmers imagination  Sometimes we require better-defined structures Abstract data structures  Improve the structure  Additional storage and retrieval rules  Additional limitations 1/20/2014 Jim Littleton - COP35382

Abstract Data Structures Abstract data structures  Commonly implemented using arrays  Can be implemented using other structures Abstract data structure examples  Stacks  Queues  Priority queues Each abstract data structure  Solves a particular set of problems  Has a unique set of advantages and disadvantages 1/20/2014 Jim Littleton - COP35383

Access (interface) Arrays  Directly accessed via an index (immediate)  Search through values sequentially or logically  Only one item can be accessed at a time Abstract data structures  An interface controls access to the values Direct access is NOT possible by user (programmer)  Implementation of the interface controls Access to values How values are stored, retrieved and deleted 1/20/2014 Jim Littleton - COP35384

Stacks Stack properties  Access to only one item at a time  Follows the Last In First Out (LIFO) logical process Think of any natural “stack” of items  Stack of dishes  Stack of coins  Stack of papers  Four basic operations (know these!) Push – place an item on top of the stack Pop – remove an item off the top of the stack Overflow – procedure to follow when the stack is full Underflow – procedure to follow when the stack is empty 1/20/2014 Jim Littleton - COP35385

Stacks Code example pubic class Stack { private int maxSize; private int top = -1; private int[] array; public Stack(int size) { maxSize = size; array = new int[maxSize]; } public int pop() { return array[top--]; // Returns the topmost item } public void push(int item) { array[++top] = item; // Places item on top of the stack } public boolean isEmpty() { // Underflow operation return top == -1; // Test if stack is empty } public boolean isFull() { // Overflow operation return top == maxSize – 1; // Test if stack is full } 1/20/2014 Jim Littleton - COP35386

Stacks Important notes  Always call isEmpty() before calling pop() Avoids ArrayIndexOutOfBounds exception  Always call isFull() before calling push() Avoids ArrayIndexOutOfBounds exception Extremely useful in programming  Following a sequence that requires backtracking  Evaluating parenthesized expressions Arithmetical expressions 1/20/2014 Jim Littleton - COP35387

Stacks Example use of a stack  Reverse the letters of a word Non-stack example private class NonStackApp { public static void main(String[] args) { String s = “COP3538”; char[] c = new char[s.length()]; for(int x = 0; x < c.length; x++) { c[x] = s.charAt(x); } for(int x = c.length – 1; x >= 0; x--) { System.out.print(c[x]); } 1/20/2014 Jim Littleton - COP35388

Stacks Example use of a stack  Reverse the letters of a word Stack example private class StackApp { public static void main(String[] args) { Stack stack = new Stack(); String s = “COP3538”; for(int x = 0; x < s.length(); x++) { stack.push(s.charAt(x)); } while(!stack.isEmpty()) { System.out.print(stack.pop()); } 1/20/2014 Jim Littleton - COP35389

Stacks A stack is a very useful data structure  Don’t have to keep track of array indices  Limited access to the elements in the array Elements are accessible only via the push and pop methods Stack efficiency  Push and pop take O(1) time A constant value. Why??  Not dependent on the number of items in the stack  No comparisons or swaps necessary 1/20/2014 Jim Littleton - COP353810

Queues A First In First Out (FIFO) data Structure  Remember: A Stack is a LIFO data structure Can be implemented using several structures  Array  Linked-list  Etc. Very useful for many different applications  Print queues  Job queues  Ready queues  Keyboard queues Jim Littleton - COP /20/2014

Queues Queue Terminology (Know the terms!!)  Front Points to the first element in the queue  Rear Points to the last element in the queue  Linear queue Front pointer always exists before the rear pointer  Circular queue The front and rear pointers can wrap around the queue  Deques (double-ended queue) Inserts and removals can occur on either end  Overflow Attempting to add an element to a full queue  Underflow Attempting to remove an element from an empty queue Jim Littleton - COP /20/2014

Queues Queue methods  Insert() Method used to store data at the rear of the queue  Remove() Method used to retrieve data from the front of the queue  IsEmpty() Tests whether the queue is empty  The queue is considered empty if the front and rear are together  IsFull() Tests whether the queue is full  The queue is considered full if the rear equals the size of the queue  Not necessarily true for Circular queues Jim Littleton - COP /20/2014

Linear Queue public class LinearQueue { private int maxSize, front, rear, numElems; private int[] array; public Queue(int size) { maxSize = size; array = new int[maxSize]; front = numElems = 0; rear = -1; } public void insert(int value) { array[++rear] = value; numElems++; } public boolean isEmpty() { return numElems == 0; } Jim Littleton - COP /20/2014 public boolean isFull() { return numElems == maxSize; } public int remove() { numElems--; return array[front++]; } public int size() { return numElems; }

Circular Queue public class CircularQueue { private int maxSize, front, rear, numElems; private int[] array; public Queue(int size) { maxSize = size; array = new int[maxSize]; front = numElems = 0; rear = -1; } public void insert(int value) { if(rear == maxSize - 1) { rear = -1; // Wrap around } array[++rear] = value; numElems++; } public boolean isEmpty() { return numElems == 0; } Jim Littleton - COP /20/2014 public boolean isFull() { return numElems == maxSize; } public int remove() { if(front == maxSize) { front = 0; // Wrap around } numElems--; return array[front++]; } public int size() { return numElems; }

Queues Queue efficiency  Insert() and remove() occur in O(1) time Simply insert or remove at front and rear pointers  Minor adjustment of front and rear values  Even in the case when the pointers wrap the queue (circular queue) Not dependent on the number of items in the array No comparisons or swaps necessary Jim Littleton - COP /20/2014

Priority Queue A priority queue (pQueue)  Similar to a regular queue Insert in rear (becomes inconsequential) Remove from front  Significant differences Items are organized by a key field  i.e., Last name, N#, etc. New items are inserted in their correct positions  The position of existing items in the queue may change  Insertion time is slower for a pQueue compared to a regular queue  Remove time is the same for both queue types If the queue is full  An existing item must be removed before a new item is added Jim Littleton - COP /20/2014

Priority Queue Several useful applications  Scheduling queues Shortest job first Fewest resources first  Print queues Fewest pages first Jobs sent to faster printers first A pQueue is no longer a FIFO queue!  The FO item is based on the specified priority Jim Littleton - COP /20/2014

Priority Queue pQueue Code Jim Littleton - COP /20/2014 public class PriorityQueue { private int maxSize; private int numElems = 0; private int[] array; public PriorityQueue(int size) { maxSize = size; array = new int[maxSize]; } public void insert(int item) { int x; if(numElems == 0) { array[numElems++] = item; } else { x = numElems; while(x > 0 && item > array[x - 1]) { array[x] = array[x - 1]; x--; } array[x] = item; numElems++; } public boolean isEmpty() { return numElems == 0; } public boolean isFull() { return numElems == maxSize; } public int remove() { return array[--numElems]; } This block of code is identical to the code that performs the copies in the Insertion Sort!

Priority Queue pQueue Efficiency  Remove occurs at O(1) time (immediate)  Insert occurs at O(n) time Items have to be copied to new locations  To make room for the item being inserted Jim Littleton - COP /20/2014

Questions Jim Littleton - COP /20/2014