Linked List Implementation of Queues

Similar documents
infix expressions (review)

Fun facts about recursion

ITI Introduction to Computing II

PA3 Design Specification

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Dynamic Data Structures

Linked Structures. See Section 3.2 of the text.

Algorithms and Data Structures

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

DEEPIKA KAMBOJ UNIT 2. What is Stack?

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Summer Final Exam Review Session August 5, 2009

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

C27a: Stack and Queue

Function Call Stack and Activation Records

Linked Lists

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Lecture 10 Notes Linked Lists

Lecture 24 Notes Search in Graphs

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Lecture 10 Notes Linked Lists

LECTURE 11 TREE TRAVERSALS

csci 210: Data Structures Stacks and Queues

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

1 P age DS & OOPS / UNIT II

DNHI Homework 3 Solutions List, Stacs and Queues

LECTURE 17 GRAPH TRAVERSALS

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

CP2 Revision. theme: dynamic datatypes & data structures

Advanced Java Concepts Unit 2: Linked Lists.

Recursion. notes Chapter 8

Stacks and queues (chapters 6.6, 15.1, 15.5)

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Largest Online Community of VU Students

Problem with Scanning an Infix Expression

CS301 - Data Structures Glossary By

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

1. Two main measures for the efficiency of an algorithm are a. Processor and memory b. Complexity and capacity c. Time and space d.

LIFO : Last In First Out

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CMSC Introduction to Algorithms Spring 2012 Lecture 7

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

12 Abstract Data Types

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Garbage Collection: recycling unused memory

Lecture Notes on Queues

INSTITUTE OF AERONAUTICAL ENGINEERING

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Ashish Gupta, Data JUET, Guna

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Linked List. April 2, 2007 Programming and Data Structure 1

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

namibia UniVERSITY OF SCIEnCE AnD TECHnOLOGY

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Stacks and Queues. David Greenstein Monta Vista

CE204 Data Structures and Algorithms Part 2

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Advanced Java Concepts Unit 3: Stacks and Queues

2/22/2013 LISTS & TREES

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CS 206 Introduction to Computer Science II

Lecture 24 Search in Graphs

Linked lists (6.5, 16)

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

CSE 230 Intermediate Programming in C and C++

Department of Computer Science and Technology

+ Abstract Data Types

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Lecture 12 Hash Tables

Graph Traversals BFS & DFS 1 CS S-16

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Programs in memory. The layout of memory is roughly:

Lecture No.04. Data Structures

Chapter 10 Recursion

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Information Science 2

Transcription:

Outline queue implementation: linked queue application of queues and stacks: data structure traversal double-ended queues application of queues: simulation of an airline counter random numbers recursion examples of recursion 1

Linked List Implementation of Queues a queue can be implemented as either a singly-linked or doubly-linked list either way, the head of the list is usually the front of the queue, and the tail of the list is the back of the queue either way, we keep a head node (for removal) and a tail node (for insertion) there is a special case when the list is empty (method offer) or when removing the last element from the list (method poll) In-class exercise (individually or in small groups): write the code for either the offer or poll method (or if you have time, for both) for a queue implemented using a singly-linked list 2

Traversal of Data Structures in a linked list, each node has a link to at most one other node in a doubly-linked list, each node has a link to at most two other nodes there are more general data structures in which nodes can have links to multiple other nodes these data structures go by different names, including trees and graphs in some cases, we need to have a program start at one node and visit all the other nodes in the data structure: tree traversal or graph traversal 3

Breadth-First Traversal in general, I can start from a given node and put into a queue all the nodes it is connected to then, I repeatedly remove one node from the queue, visit it, and put into the queue all the nodes it (the new node) is connected to if I keep doing this, staying away from nodes that have already been visited, I will eventually visit all connected nodes this is called breadth-first traversal: visually arranging the first node at the top the nodes it is connected to right below it, the nodes they are connected to right below them, nodes are visited in left-to-right and top-to-bottom order 4

Depth-First Traversal for a traversal, I could push the nodes on a stack instead of adding them to a queue then, the node I will visit next is the node I put on the stack most recently that means visiting every node connected to the most recently visited node, before visiting any node that was pushed onto the stack earlier this is called depth-first traversal because the traversal tends to go top-to-bottom, then climb back up and explore the side branches 5

Double-Ended Queues sometimes, it is useful to be able to add and remove elements at either end of a queue this double-ended queue, or deque (pronounced "D-Q" or deck ), can do everything either a stack or a queue can do implementation, using either arrays or linked lists, is similar to the implementation of either a stack or a queue 6

Simulation of an Airline Counter two queues: regular and business class passengers random arrivals: during each minute, one passenger arrives with a given probability, arrivalrate this is computed by testing if (Math.random() < arrivalrate) { // passenger arrives one agent, requiring a time that is uniformly random between 0 and some defined maximum number of minutes every minute, check to see whether to add passengers to each queue check to see whether the agent is done taking care of the current passenger, and if so, select the next passenger if any update the time once a passenger is selected, that passenger's statistics (waiting time) must be updated if there are passengers in both queues, we can try different strategies for selecting the next passenger 7

Random Numbers tossing a fair coin is truly random -- there is no way to predict what the next toss will give computers do not find it easy to toss coins instead, beginning with a specific number (the seed), they apply a complicated function to yield a new number this new number is a pseudo-random value: it is computed, and therefore not random, but there is no easy way to predict the new number from the old (without knowing the exact function), and so it looks like a sequence of random numbers the initial seed can be a fixed value (e.g. 1), to give a repeatable sequence of random numbers (good for debugging code) or, the initial seed can be selected almost at random, e.g. the time of day when the program is run, to give a different sequence each time Java's Math.random() returns a double uniformly distributed between 0 and 1 Java Random by default is initialized to the current day and time, but the programmer can explicitly specify the seed 8

Implementation of Java methods: calls When method A calls method B, Java: saves the current location of execution on the Java stack evaluates the parameters pushes onto the stacks the values of the parameters starts executing the code for B the code for B pushes onto the stack the values of any local and loop variables 9

Implementation of Java methods: returns When method B returns, Java: saves the value being returned (if any) pops the parameter values from the Java stack pops the return location from the Java stack starts executing the code at the return location in our example, it resumes execution of A in-class discussion: could A and B be the same method? 10

Recursion supposing you had a java method call itself what would happen? for example: static int method multiply(int a, int b) { if (a == 1) { return b; } return b + multiply(a 1, b); } in-class exercise (everyone together): what does multiply (2, 5) return? why and how? 11

Recursion Explained all the information about a method call is on the Java stack so a method can call itself as many times as needed the parameter values and return locations are unique to each invocation of the method because they are saved on the stack which grows and shrinks as needed the caller may be the same or a different method, it doesn't matter 12

Many Types of Recursion recursion can be very convenient static int method multiply(int a, int b) { if (a < 0) return (multiply (-a, b)) if (a == 0) return 0; if (a == 1) return b; return b + multiply(a 1, b); } 13

Infinite Recursion just as loops can be infinite, so can recursion: void infinite() { } infinite(); calling infinite will eventually lead to an overflow of the Java stack it may take a while! 14

Making sure recursion terminates There must be one or more base case, which always return For any possible value of parameters, every recursive case must get closer to at least one of the base cases sometimes closer is easily defined, sometimes not but whenever this property is missing, there is the possibility of infinite recursion 15

Getting closer to the base cases static int factorial(n) { if (n <= 1) return 1; return n * factorial(n 1); } E last(linkednode<e> node) { if (node.next == null) return node.value; return last(node.next); note that last has infinite recursion on circular lists and fails if node is null 16

Recursive Methods for Linked Lists How many nodes are in the linked list? int countnodes(linkednode<e> node) { if (node == null) return 0; return 1 + countnodes(node.next); Is an element in the Linked List? boolean exists(linkednode<e> n, Object v) { if (node == null) return false; if (v.equals(node.value)) return true; return exists(n.next, v); recursion is a natural for linked lists! as long as the linked list has finite length 17

Fun facts about recursion every loop can be replaced by recursion but not every loop can be replaced by recursion (unless the loop uses an explicit stack) some things must be computed recursively: int ackermann(int m, int n) { } if (m == 0) return n + 1; if (n == 0) return ackermann(m 1, 1); return ackermann(m 1, ackermann(m, n 1)); sometimes recursion is the simplest way of thinking of problems can you imagine using recursion to evaluate infix expressions? 18