Lecture No.09 Data Structures Dr. Sohail Aslam

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

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.
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.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
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.
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Data Structures - Queues
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
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’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Linked Data Structures
STACKS & QUEUES for CLASS XII ( C++).
Elementary Data Structures
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Lecture No.06 Data Structures Dr. Sohail Aslam
Lecture No.03 Data Structures Dr. Sohail Aslam
CSE 373: Data Structures and Algorithms
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Memory Organization Process 1 (browser) Code Process 3 (word)
CSC 143 Queues [Chapter 7].
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
CE 221 Data Structures and Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures and Algorithms
CSCS-200 Data Structure and Algorithms
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures & Programming
Presentation transcript:

Lecture No.09 Data Structures Dr. Sohail Aslam

Memory Organization Process 1 (browser) Code Process 3 (word) Static data Process 4 (excel) Stack End of lecture 8 Process 2 (dev-c++) Windows OS Heap

Stack Layout during a call Here is stack layout when function F calls function G: Parameters(F) Local variables(F) Return address(F) Parameters(G) Parameters(F) Local variables(F) Return address(F) Parameters(G) Local variables(G) Return address(G) Parameters(F) Local variables(F) Return address(F) sp sp sp At point of call During execution of G After call

Queues A stack is LIFO (Last-In First Out) structure. In contrast, a queue is a FIFO (First-In First-Out ) structure. A queue is a linear structure for which items can be only inserted at one end and removed at another end.

Queue Operations Enqueue(X) – place X at the rear of the queue. Dequeue() -- remove the front element and return it. Front() -- return front element without removing it. IsEmpty() -- return TRUE if queue is empty, FALSE otherwise

Implementing Queue Using linked List: Recall Insert works in constant time for either end of a linked list. Remove works in constant time only. Seems best that head of the linked list be the front of the queue so that all removes will be from the front. Inserts will be at the end of the list.

Implementing Queue Using linked List: rear front rear front 1 7 5 2 1

Implementing Queue Using linked List: dequeue() rear front rear front 1 7 5 2 1 7 5 2 dequeue() rear front rear front 1 7 5 2 7 5 2

Implementing Queue Using linked List: enqueue(9) front rear front rear 1 7 5 2 1 7 5 2 enqueue(9) rear front rear front 7 5 2 9 7 5 2 9

Implementing Queue int dequeue() { int x = front->get(); Node* p = front; front = front->getNext(); delete p; return x; } void enqueue(int x) Node* newNode = new Node(); newNode->set(x); newNode->setNext(NULL); rear->setNext(newNode); rear = newNode;

Implementing Queue int front() { return front->get(); } int isEmpty() return ( front == NULL );

Queue using Array If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive. This is because we may have to shift up to “n” elements. For the stack, we needed only one end; for queue we need both. To get around this, we will not shift upon removal of an element.

Queue using Array front rear 6 5 7 1 3 2 4 front rear 1 7 5 2

Queue using Array enqueue(6) front rear 1 7 5 2 6 1 7 5 2 6 1 2 3 4 5 1 2 3 4 5 6 7 front rear 4

Queue using Array enqueue(8) front rear 1 7 5 2 6 8 1 7 5 2 6 8 1 2 3 1 2 3 4 5 6 7 front rear 5

Queue using Array dequeue() front rear 7 5 2 6 8 7 5 2 6 8 1 2 3 4 5 6 1 2 3 4 5 6 7 front rear 1 5

Queue using Array dequeue() front rear 5 2 6 8 5 2 6 8 1 2 3 4 5 6 7 1 2 3 4 5 6 7 front rear 2 5

Queue using Array enqueue(9) enqueue(12) enqueue(21) ?? front rear 5 2 6 8 9 12 5 2 6 8 9 12 1 2 3 4 5 6 7 front rear 2 7 enqueue(21) ??

Queue using Array We have inserts and removal running in constant time but we created a new problem. Cannot insert new elements even though there are two places available at the start of the array. Solution: allow the queue to “wrap around”.

Queue using Array Basic idea is to picture the array as a circular array. 6 5 7 1 3 2 4 8 9 12 front front rear 2 5 2 6 8 9 12 rear 7

Queue using Array enqueue(21) void enqueue(int x) { 1 2 front 8 size front rear 21 7 2 12 5 5 2 6 8 9 12 21 9 2 6 3 rear noElements 8 6 7 5 4 void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; noElements = noElements+1; }

Queue using Array enqueue(7) int isFull() { return noElements == size; 1 2 front 8 size front rear 21 7 7 2 12 5 5 2 6 8 9 12 21 7 9 2 6 3 1 rear noElements 8 6 8 5 4 int isFull() { return noElements == size; } int isEmpty() return noElements == 0;

Queue using Array dequeue() int dequeue() { int x = array[front]; 1 4 front 8 size front rear 21 7 7 2 12 6 8 9 12 21 7 9 6 3 1 rear noElements 8 6 6 5 4 int dequeue() { int x = array[front]; front = (front+1)%size; noElements = noElements-1; return x; }

Use of Queues Out of the numerous uses of the queues, one of the most useful is simulation. A simulation program attempts to model a real-world phenomenon. Many popular video games are simulations, e.g., SimCity, FlightSimulator Each object and action in the simulation has a counterpart in real world.

Uses of Queues If the simulation is accurate, the result of the program should mirror the results of the real-world event. Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. Let us look at an example. Suppose there is a bank with four tellers.

Simulation of a Bank A customer enters the bank at a specific time (t1) desiring to conduct a transaction. Any one of the four tellers can attend to the customer. The transaction (withdraw, deposit) will take a certain period of time (t2). If a teller is free, the teller can process the customer’s transaction immediately and the customer leaves the bank at t1+t2. End of lecture 9