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’

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
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CHAPTER 7 Queues.
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.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
Stacks.
Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Stacks and Queues Introduction to Computing Science and Programming I.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
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,
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.
Stacks And Queues Chapter 18.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
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.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
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.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Basic Data Types Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Lecture 21 Stacks and Queues Richard Gesick.
CSC 143 Stacks [Chapter 6].
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.
Presentation transcript:

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’ item I gave you on hold; this is more important, so rush it out first.” We’ll end up doing the last item first (last in, first out).

The Stack Push Pop

Idea: a “Last In, First Out” (LIFO) data structure Behaviors: Push: Add to top of stack Pop: Remove from top of stack (and return that top value) Top: Return topmost item (but leave it on the stack) Is_Full: is it full? Is_Empty: is it empty? Initialize: empty stack Properties

The Stack as a Logical Data Structure The stack is an idea It implies a set of logical behaviors It can be implemented various ways –Using a linked list or a tree or an array In this example, we’ll focus on dynamic implementations using dynamic data...

Stacks: Dynamic Implementation A linked list with restricted set of operations to change its state: only modified from one end top

public void Push (int value) // Purpose: push one value onto stack // Pre: top points to null-terminated list // Post: the list has one node added public int Pop (void) // Purpose: Pop a value off stack; // if empty, throw an exception // Pre: top points to a null-terminated list // Post: list has one fewer, return top data Application Programmer Interface

Push Create new node Add it to the front top

Push Create new node Add it to the front top 4 temp ?

Push Create new node Add it to the front top 4 temp

Push Create new node Add it to the front top

Pop Capture the first value (to return) Remove the first node (move top to next) top

Pop Capture the first value (to return) Remove the first node (move top to next) top value = 4

Pop Capture the first value (to return) Remove the first node (move top to next) top value = 4

Pop Capture the first value (to return) Remove the first node (move top to next) top value (4) is returned

Summary: Stack Allow us to model “last-in, first-out” behavior Can be implemented using different data types Behavior is important (and defines a Stack) –Push to the front –Pop from the front –(from the same end)

Queues

Some Examples Waiting in line –At the grocery store –At the movies Ordering items –Bills to pay –Making pizzas We can use a queue to model each of these.

The Queue Enqueue Dequeue

Idea: a “First In, First Out” (FIFO) data structure Behaviors: Enqueue: Add to end of queue Dequeue: Remove from front of queue (and return that front value) Top: Return front-most item (but leave it in the queue) Is_Full: is it full? Is_Empty: is it empty? Initialize: empty queue Properties of Queues

The Queue as a Logical Data Structure The queue is an idea It implies a set of logical behaviors It can be implemented various ways –Using a linked list or a tree or an array In this example, we’ll focus on dynamic implementations using dynamic data...

Queues: Dynamic Implementation A linked list with restricted set of operations to change its state: only modified by adding to one end and deleting from the other tail head

Application Programmer Interface public void Enqueue (int value) // Pre: Q is initialized // Purpose: Add a value to the tail // Post: Q has new element at tail public int Dequeue (void) // Pre: Q is initialized // Purpose: Remove first value from Q and // return it via ‘value’ OR indicate // that Q is empty via exception // Post: element that was at head has been // removed OR throw exception

Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head

Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

Dequeue Capture the first value (to return) Remove the first node (move head to next) 4 17 head 42 tail

Dequeue Capture the first value (to return) Remove the first node (move head to next) value = head 42 tail

Dequeue Capture the first value (to return) Remove the first node (move head to next) value = head 42 tail

Dequeue Capture the first value (to return) Remove the first node (move head to next) value (4) is returned 17 head 42 tail

An Example Program Reverse Polish Notation (RPN) Calculator Enter two operands Perform operation Maintain stack of numbers

Summary: Queues Allow us to model “first-in, first-out” behavior Can be implemented using different data types Behavior is important (and defines a Queue) –Enqueue to end –Dequeue from front –(or vice-versa – just do at opposite ends)

FIN