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.

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

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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
CHAPTER 7 Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
CS Data Structures II Review COSC 2006 April 14, 2017
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,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd  Each node contains a value  and a link (pointer or reference)
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CSC 231 Stacks 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Stacks and Queues MR. Mohammad Alqahtani.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
STACKS & QUEUES for CLASS XII ( C++).
Elementary Data Structures
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Stacks and Queues.
Queues Queues Queues.
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists.
Stacks, Queues, and Deques
Linked Lists.
Presentation transcript:

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 were inserted A queue is a first in, first out (FIFO) data structure –Items are removed from a queue in the same order as they were inserted A deque is a double-ended queue--items can be inserted and removed at either end

Array implementation of stacks To implement a stack, items are inserted and removed at the same end (called the top) Efficient array implementation requires that the top of the stack be towards the center of the array, not fixed at one end To use an array to implement a stack, you need both the array itself and an integer The integer tells you either: –Which location is currently the top of the stack, or –How many elements are in the stack

Pushing and popping If the bottom of the stack is at location 0, then an empty stack is represented by top = -1 or count = 0 To add (push) an element, either: –Increment top and store the element in stk[top], or –Store the element in stk[count] and increment count To remove (pop) an element, either: –Get the element from stk[top] and decrement top, or –Decrement count and get the element in stk[count] top = 3 or count = stk:

After popping When you pop an element, do you just leave the “deleted” element sitting in the array? The surprising answer is, “it depends” –If this is an array of primitives, or if you are programming in C or C++, then doing anything more is just a waste of time –If you are programming in Java, and the array contains objects, you should set the “deleted” array element to null –Why? To allow it to be garbage collected! top = 2 or count = stk:

Sharing space Of course, the bottom of the stack could be at the other end top = 6 or count = stk: Sometimes this is done to allow two stacks to share the same storage area topStk2 = stks: topStk1 = 2

Error checking There are two stack errors that can occur: –Underflow: trying to pop (or peek at) an empty stack –Overflow: trying to push onto an already full stack For underflow, you should throw an exception –If you don’t catch it yourself, Java will throw an ArrayIndexOutOfBounds exception –You could create your own, more informative exception For overflow, you could do the same things –Or, you could check for the problem, and copy everything into a new, larger array

Pointers and references In C and C++ we have “pointers,” while in Java we have “references” –These are essentially the same thing The difference is that C and C++ allow you to modify pointers in arbitrary ways, and to point to anything –In Java, a reference is more of a “black box,” or ADT Available operations are: –dereference (“follow”) –copy –compare for equality There are constraints on what kind of thing is referenced: for example, a reference to an array of int can only refer to an array of int

Creating references The keyword new creates a new object, but also returns a reference to that object For example, Person p = new Person("John") –new Person("John") creates the object and returns a reference to it –We can assign this reference to p, or use it in other ways

Creating links in Java class Cell { int value; Cell next; Cell (int v, Cell n) { value = v; next = n; } } Cell temp = new Cell(17, null); temp = new Cell(23, temp); temp = new Cell(97, temp); Cell myStack = new Cell(44, temp); myStack:

Linked-list implementation of stacks Since all the action happens at the top of a stack, a singly- linked list (SLL) is a fine way to implement it The header of the list points to the top of the stack myStack: Pushing is inserting an element at the front of the list Popping is removing an element from the front of the list

Linked-list implementation details With a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem) Underflow can happen, and should be handled the same way as for an array implementation When a node is popped from a list, and the node references an object, the reference (the pointer in the node) does not need to be set to null –Unlike an array implementation, it really is removed-- you can no longer get to it from the linked list –Hence, garbage collection can occur as appropriate

Array implementation of queues A queue is a first in, first out (FIFO) data structure This is accomplished by inserting at one end (the rear) and deleting from the other (the front) To insert: put new element in location 4, and set rear to 4 To delete: take element from location 0, and set front to myQueue: rear = 3front = 0

Array implementation of queues Notice how the array contents “crawl” to the right as elements are inserted and deleted This will be a problem after a while! After insertion: After deletion: rear = 4 front = Initial queue: rear = 3 front = 0

Circular arrays We can treat the array holding the queue elements as circular (joined at the ends) myQueue: rear = 1 front = 5 Elements were added to this queue in the order 11, 22, 33, 44, 55, and will be removed in the same order Use: front = (front + 1) % myQueue.length; and: rear = (rear + 1) % myQueue.length;

Full and empty queues If the queue were to become completely full, it would look like this: If we were then to remove all eight elements, making the queue completely empty, it would look like this: myQueue: rear = 4front = myQueue: rear = 4front = 5 This is a problem!

Full and empty queues: solutions Solution #1: Keep an additional variable Solution #2: (Slightly more efficient) Keep a gap between elements: consider the queue full when it has n-1 elements myQueue: rear = 4front = 5count = myQueue: rear = 3front = 5

Linked-list implementation of queues In a queue, insertions occur at one end, deletions at the other end Operations at the front of a singly-linked list (SLL) are O(1), but at the other end they are O(n) –Because you have to find the last element each time BUT: there is a simple way to use a singly-linked list to implement both insertions and deletions in O(1) time –You always need a pointer to the first thing in the list –You can keep an additional pointer to the last thing in the list

SLL implementation of queues In an SLL you can easily find the successor of a node, but not its predecessor –Remember, pointers (references) are one-way If you know where the last node in a list is, it’s hard to remove that node, but it’s easy to add a node after it Hence, –Use the first element in an SLL as the front of the queue –Use the last element in an SLL as the rear of the queue –Keep pointers to both the front and the rear of the SLL

Enqueueing a node 17 Node to be enqueued To enqueue (add) a node: Find the current last node Change it to point to the new last node Change the last pointer in the list header 2344 last first 97

Dequeueing a node To dequeue (remove) a node: –Copy the pointer from the first node into the header last first

Queue implementation details With an array implementation: –you can have both overflow and underflow –you should set deleted elements to null With a linked-list implementation: –you can have underflow –overflow is a global out-of-memory condition –there is no reason to set deleted elements to null

Deques A deque is a double-ended queue Insertions and deletions can occur at either end Implementation is similar to that for queues Deques are not heavily used You should know what a deque is, but we won’t explore them much further

Stack ADT The Stack ADT, as provided in java.util.Stack : –Stack() : the constructor –boolean empty() –Object push(Object item) –Object peek() –Object pop() –int search(Object o): Returns the 1-based position of the object on this stack Note: You are not allowed to use java.util.Stack (or similar classes in java.util ) when your assignment says to implement your own stack –You may use these classes in later assignments, if you simply need a stack in the course of doing other things

A queue ADT Here is a possible queue ADT: –Queue() : the constructor –boolean empty() –Object enqueue(Object item) : add at element at the rear –Object dequeue() : remove an element from the front –Object peek() : look at the front element –int search(Object o): Returns the 1-based position from the front of the queue Java does not provide a queue class

A deque ADT Here is a possible deque ADT: –Deque() : the constructor –boolean empty() –Object addAtFront(Object item) –Object addAtRear(Object item) –Object getFromFront() –Object getFromRear() –Object peekAtFront() –Object peekAtRear() –int search(Object o): Returns the 1-based position from the front of the deque Java does not provide a deque class

The End