Data Structures Lakshmish Ramaswamy.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)
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.
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.
Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented.
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.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structure Dr. Mohamed Khafagy.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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,
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
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.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
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.
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
CS505 Data Structures and Algorithms
G64ADS Advanced Data Structures
QueueStack CS1020.
Queues Rem Collier Room A1.02
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
Lecture 16 Stacks and Queues CSE /26/2018.
Lecture 9: Stack and Queue
Presentation transcript:

Data Structures Lakshmish Ramaswamy

Stack Data Structure Last-in-first-out paradigm Supports two operations Push – Insert an element at top Pop – Remove the element at top Can be implemented using ArrayLists or LinkedLists How?

Queue First-in-First-Out Paradigm Many applications Operations Buffer management Job schedulers in OS Operations enqueue Dequeue getFront Can be implemented using ArrayList or LinkedList

Queue

Implementation Using ArrayList/LinkedList enqueue(o) implemented as add(o) Added to the end dequeue() implemented as remove(0) getFirst() implemented as get(0) Problem with ArrayList implementation Every dequeue causes element shifting

Circular Queue Avoids element shifting on enqueue or dequeue Circle with numbered slots Slot numbers increase in clockwise fashion “Capacity” indicates maximum elements queue can hold Two pointers – Front and End -1 indicates empty queue Both pointers move in clockwise direction Wraparound on reaching end

Circular Queue Illustration Front C-1 1 2 End

Circular Queue Implementation public class cirQueue{ Object[] arr; int capacity; int front; int end; public cirQueue(int cpty){ capacity = cpty; arr = new Object[capacity]; front = -1; end = -1;}

Enqueue Method public boolean enqueue(Object o){ if(end == -1){ front = 0; arr[end] = o; return(true);} int newEnd = (end + 1)%capacity; if (newEnd == front) return(false); end = newEnd; return(true); }

Dequeue Method public Object dequeue(){ Object retobj; if(front == -1) return(null); retobj = arr[front]; if(front == end){ front = -1; end = -1;} else{ front = (front+1)%capacity;} return(retobj);}