FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advanced Piloting Cruise Plot.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Chapter 3: Linked List Tutor: Angie Hui
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Determine Eligibility Chapter 4. Determine Eligibility 4-2 Objectives Search for Customer on database Enter application signed date and eligibility determination.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
ZMQS ZMQS
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Data Structures Using C++
LIST PROCESSING.
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
ABC Technology Project
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
A SMALL TRUTH TO MAKE LIFE 100%
PSSA Preparation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
How Cells Obtain Energy from Food
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Presentation transcript:

FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

FIFO Queues Stacks are last-in first-out queues. Another widely used model is first-in first-out (FIFO) queues. Examples of uses of FIFO queues: 2

FIFO Queues Stacks are last-in first-out queues. Another widely used model is first-in first-out (FIFO) queues. Examples of uses of FIFO queues: Program execution: – Requests for access to memory, disk, network... Resource allocation: – Forwarding network traffic in network switches and routers. Search algorithms. – More details later in the course 3

Put and Get The FIFO queue supports insert and delete as follows: – insert, push, put: This is what we call the insert operation when we talk about FIFO queues. It puts an item "at the end of the line". – delete, pop, get: This is what we call the delete operation when we talk about FIFO queues. It removes the item that is "at the head of the line". How can we implement FIFO queues? 4

FIFO Queues Using Lists A FIFO queue is essentially a list. put(queue, item) inserts that item at the END of the list. get(queue) removes (and returns) the item at the beginning of the list. What is the running time of these operations? 5

FIFO Queues Using Lists A FIFO queue is essentially a list. put(queue, item) inserts that item at the END of the list. get(queue) removes (and returns) the item at the beginning of the list. Both operations take O(1) time. – Assumption: the list data type contains a pointer to the last element. – Our new implementation at lists.c satisfies that assumption. 6

Queue Versus Stack Implementation Implementation-wise, compare: – list-based queue implementation. – list-based stack implementation. What are the key differences? 7

Queue Versus Stack Implementation Implementation-wise, compare: – list-based queue implementation. – list-based stack implementation. The only difference is in insertions. – Queues insert at the end of the list. – Stacks insert at the beginning of the list. It is very easy to implement queues starting from our list-based stack implementation. – Rename push to put. – Rename pop to get. – Change the get function to insert at the end. 8

FIFO Queues Using Arrays How do we implement queues using arrays? The stack definition looked like this: typedef struct stack_struct * Stack; struct stack_struct { int max_size; int top_index; void ** items; }; What changes do we need to accommodate queues? 9

FIFO Queues Using Arrays How do we implement queues using arrays? A queue can be defined like this: typedef struct queue_struct * queue; struct queue_struct { int max_size; int start_index; int end_index; void ** items; }; end_index tells us where to put a new item. start_index tells us where to remove an item from. 10

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get(): returns 15 put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get(): returns 20 put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get(): returns 30 get() queueposition start_index end_index

Examples of Put and Get put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get(): returns queueposition start_index end_index

Book Implementation (Array-Based) static Item *q; static int N, head, tail; void QUEUEinit(int maxN) { q = malloc((maxN+1)*sizeof(Item)); N = maxN+1; head = N; tail = 0; } int QUEUEempty() { return head % N == tail; } void QUEUEput(Item item) { q[tail++] = item; tail = tail % N; } Item QUEUEget() { head = head % N; return q[head++]; } 22

Limitations of This Implementation ??? 23

Limitations of This Implementation Same as for stacks. Only one queue object can be used in the entire code. Queues can only store objects of a single data type: – Specified in Item.h, where type Item is defined using a typedef. It is easy to adapt the stacks_arrays.c code to obtain an array-based implementation of queues that does not have these two limitations. Possible homework... 24

More on Queues … Later Done with queues for now. We will see queues and queue-related topics quite a bit more in this course. 25