Stacks and Queues.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Building Java Programs Chapter 14
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
TCSS 342, Winter 2005 Lecture Notes
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
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.
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.
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Building Java Programs
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Stacks and Queues.
Revised based on textbook author’s notes.
Chapter 15 Lists Objectives
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
CSE 373: Data Structures and Algorithms
Algorithms and Data Structures
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
Introduction to Data Structures
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Stacks and queues.
Building Java Programs Chapter 14
Stacks, Queues, and Deques
Stacks and Queues.
slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Building Java Programs
Building Java Programs Chapter 14
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
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.
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
slides created by Marty Stepp
Stacks.
Topic 15 Implementing and Using Stacks
More Data Structures (Part 1)
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
Stacks, Queues, and Deques
Stacks and Queues.
Presentation transcript:

Stacks and Queues

What are stacks and queues? Stacks and Queues are classic linear data structures. A linear data structure organizes data in a linear fashion. Question: What is the most basic linear data structure we’ve used? Answer : An array. Stacks and queues differ from arrays in that they have restrictions on the way we put items in and take items out 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

Queues queue: A linear data structure that manages data in a first-in, first-out manner. Example: A waiting line for service at the book store. A customer enters the queue at the back and moves forward. front back Customer 1 Customer 2 Customer 3 remove, peek add

Queues in Computer Science Operating systems: queue of print jobs to send to the printer queue of programs / processes to be run queue of network data packets to send Programming: modeling a line of customers or clients storing a queue of computations to be performed in order Real world examples: people on an escalator or waiting in a line cars at a gas station (or on an assembly line)

Basic Queue Operations add(obj) places a given object at the back of queue remove() removes an object from front of queue and returns it; throws a NoSuchElementException if queue is empty peek() returns front object from queue without removing it; returns null if queue is empty size() returns the number of objects in queue isEmpty() returns true if queue has no objects

Linked-list implementation of queues In a queue, insertions occur at the back of the list. Deletions occur at the front of the list. These operations can be easily implemented using a singly-linked list (SLL). You always need a pointer to the head of the list You can keep an additional pointer to the last item in the list. 44 97 23 17 head myQueue: last

Adding a node to the Queue 17 Node to add 23 44 last first 97

Removing a node from the Queue 44 97 23 17 last head

Programming with Queues using Java Collections Queues are naturally implemented as Linked Lists. When constructing a queue using Java Collections, you must use a new LinkedList object instead of a new Queue object. Queue<Integer> q = new LinkedList <Integer>(); q.add(42); q.add(-3); q.add(17); // front [42, -3, 17] back System.out.println(q.remove()); // 42

Exercises Write a method stutter that accepts a queue of integers as a parameter and replaces every element of the queue with two copies of that element. front [1, 2, 3] back becomes front [1, 1, 2, 2, 3, 3] back Write a method mirror that accepts a queue of strings as a parameter and appends the queue's contents to itself in reverse order. front [a, b, c] back becomes front [a, b, c, c, b, a] back

Stacks stack: A linear collection of objects. A stack manages data in a last-in, first-out manner. Example: Given a stack of trays, trays can only be added to the top of the stack and retrieved from the top of the stack. Last-In, First-Out ("LIFO") Elements are stored in order of insertion. Do NOT think of them as having indexes. Client can only add/remove/examine the last element added (the "top"). pop, peek push top 3 2 bottom 1 stack

Linked-list implementation of stacks A singly-linked list (SLL) is an obvious method for implementing a stack. A top pointer of the list will point to the top (back) of the stack. pushing is inserting an element at the top of the list. Popping is removing an element from the top of the list Actions may only happen at the top of a stack. 44 97 23 17 myStack top head

Stacks in computer science Programming languages and compilers: method calls are placed onto a stack (call=push, return=pop) compilers use stacks to evaluate expressions Matching up related pairs of things: find out whether a string is a palindrome examine a file to see if its braces { } match convert "infix" expressions to pre/postfix Sophisticated algorithms: searching through a maze with "backtracking" many programs use an "undo stack" of previous operations

Class Stack push(obj) Adds a given object to the top of stack pop() removes top object from stack and returns it; throws EmptyStackException if stack is empty peek() returns top object from stack without removing it; size() returns number of objects in stack isEmpty() returns true if stack has no objects

Implementation using Java Collections Example Stack Code Implementation using Java Collections Stack<String> s = new Stack<String>(); s.push("a"); s.push("b"); s.push("c"); // bottom ["a", "b", "c"] top System.out.println(s.pop()); // "c"

What is the following stack Logic Error? Problem: Suppose we're asked to write a method max that accepts a Stack of integers and returns the largest integer in the stack. // Assume the stack is not empty. public static void max(Stack<Integer> s) { int maxValue = s.pop(); while (!s.isEmpty()) { int next = s.pop(); maxValue = Math.max(maxValue, next); } return maxValue; The algorithm is correct, but what is wrong with the code?

Answer The code destroys the stack in figuring out its answer. To fix this, you must save and restore the stack's contents: public static void max(Stack<Integer> s) { Stack<Integer> backup = new Stack<Integer>(); int maxValue = s.pop(); backup.push(maxValue); while (!s.isEmpty()) { int next = s.pop(); backup.push(next); maxValue = Math.max(maxValue, next); } while (!backup.isEmpty()) { // restore s.push(backup.pop()); return maxValue;