CSCS-200 Data Structure and Algorithms

Slides:



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

Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
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.”
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
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.”
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)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
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.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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)
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Data Structures and Algorithms Stacks and Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Lecture No.10 Data Structures Dr. Sohail Aslam. Simulation of a Bank  A customer enters the bank at a specific time (t 1 ) desiring to conduct a transaction.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
Data Structures Using C++ 2E
Lecture No.09 Data Structures Dr. Sohail Aslam
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CC 215 Data Structures Queue ADT
Queues.
Lecture No.06 Data Structures Dr. Sohail Aslam
C++ Plus Data Structures
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Stacks, Queues, and Deques
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Memory Organization Process 1 (browser) Code Process 3 (word)
Stacks, Queues, and Deques
CSC 143 Queues [Chapter 7].
Stacks and Queues CSE 373 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Stacks and Queues CSE 373 Data Structures.
Using a Queue Chapter 8 introduces the queue data type.
CSI 1340 Introduction to Computer Science II
Queues.
Using a Queue Chapter 8 introduces the queue data type.
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues.
Presentation transcript:

CSCS-200 Data Structure and Algorithms Lecture 12-13

THE STACK

What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).

Stack Specification Definitions: (provided by the user) Operations MAX_ITEMS: Max number of items that might be on the stack ItemType: Data type of the items on the stack Operations MakeEmpty Boolean IsEmpty Boolean IsFull Push (ItemType newItem) Pop (ItemType& item)

Push (ItemType newItem) Function: Adds newItem to the top of the stack. Preconditions: Stack has been initialized and is not full. Postconditions: newItem is at the top of the stack.

Pop (ItemType& item) Function: Removes topItem from stack and returns it in item. Preconditions: Stack has been initialized and is not empty. Postconditions: Top element has been removed from stack and item is a copy of the removed element.

Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. End of lecture 5 You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Using Linked List For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively. Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last. Make sense to place stack elements at the start of the list because insert and removal are constant time. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Using Linked List No need for the current pointer; head is enough. 1 7 5 2 head top 1 7 5 2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Operation: List int pop() { int x = head->get(); Node* p = head; head = head->getNext(); delete p; return x; } top 2 5 7 1 head A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Operation: List void push(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(head); head = newNode; } head A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training. top 9 7 5 2 7 5 newNode 9 2 push(9)

Stack Operation: List All four operations take constant time. int top() { return head->get(); } int IsEmpty() return ( head == NULL ); All four operations take constant time. We implement IsFull() function only in array implementation because we expect enough memory will be available. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack: Array or List Since both implementations support stack operations in constant time, any reason to choose one over the other? Allocating and deallocating memory for list nodes does take more time than preallocated array. List uses only as much memory as required by the nodes; array requires allocation ahead of time. List pointers (head, next) require extra memory. Array has an upper limit; List is limited by dynamic memory allocation. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Lab Exercise Create a stack of integers with ten elements. Program should output as follows: Print the elements of the stack. Remove 5 elements from the stack. Print the elements of stack again. Insert 2 more elements. Print the elements of stack. Increase each element by 1 and print again.

What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: Elements are added at one end. Elements are removed from the other end. The element added first is also removed first (FIFO: First In, First Out).

Queue Initially q.rear = -1 When is queue empty? Q.rear < q.front Total Number of Elements = q.rear –q.front + 1

An Implementation Problem Consider a 5 element queue Initially q.front = 0 and q.rear = -1 After Inserting 3 elements delete two elements and again insert two elements. Can you insert any more?

One Solution After one removal move all elements one step up and fix front = 0. In this case no front variable is needed. But not an efficient solution. Why?

Queue Specification Definitions: (provided by the user) Operations MAX_ITEMS: Max number of items that might be on the queue ItemType: Data type of the items on the queue Operations MakeEmpty Boolean IsEmpty Boolean IsFull Enqueue (ItemType newItem) Dequeue (ItemType& item)

Enqueue (ItemType newItem) Function: Adds newItem to the rear of the queue. Preconditions: Queue has been initialized and is not full. Postconditions: newItem is at rear of queue.

Dequeue (ItemType& item) Function: Removes front item from queue and returns it in item. Preconditions: Queue has been initialized and is not empty. Postconditions: Front element has been removed from queue and item is a copy of removed element.

Implementation issues Implement the queue as a circular structure. How do we know if a queue is full or empty? Initialization of front and rear. Testing for a full or empty queue.

Make front point to the element preceding the front element in the queue (one memory location will be wasted).

Initialize front and rear

Queue is empty now!! rear == front

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 );

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

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

Simulation of a Bank It is possible that none of the four tellers is free in which case there is a line of customers are each teller. An arriving customer proceeds to the back of the shortest line and waits for his turn. The customer leaves the bank at t2 time units after reaching the front of the line. The time spent at the bank is t2 plus time waiting in line. End of lecture 9

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation of a Bank teller 1 teller 2 teller 3 teller 4

Simulation Models Two common models of simulation are time-based simulation and event-based simulation. In time-based simulation, we maintain a timeline or a clock. The clock ticks. Things happen when the time reaches the moment of an event.

Timeline based Simulation Consider the bank example. All tellers are free. Customer C1 comes in at time 2 minutes after bank opens. His transaction (withdraw money) will require 4 minutes. Customer C2 arrives 4 minutes after the bank opens. Will need 6 minutes for transaction. Customer C3 arrives 12 minutes after the bank opens and needs 10 minutes.

Timeline based Simulation Events along the timeline: Time (minutes) 1 2 3 4 5 6 7 8 10 11 12 13 14 15 C1 in C1 out C2 in C2 out C3 in

Timeline based Simulation We could write a main clock loop as follows: clock = 0; while( clock <= 24*60 ) { // one day read new customer; if customer.arrivaltime == clock insert into shortest queue; check the customer at head of all four queues. if transaction is over, remove from queue. clock = clock + 1; }

Event based Simulation Don’t wait for the clock to tic until the next event. Compute the time of next event and maintain a list of events in increasing order of time. Remove a event from the list in a loop and process it.

Event based Simulation Events Time (minutes) 1 2 3 4 5 6 7 8 10 11 12 13 14 15 C1 in C1 out C2 in C2 out C3 in Event 1: 2 mins C1 in Event 2: 4 mins C2 in Event 3: 6 mins C1 out Event 4: 10 mins C2 out Event 5: 12 mins C3 in

Event based Simulation Maintain a queue of events. Remove the event with the earliest time from the queue and process it. As new events are created, insert them in the queue. A queue where the dequeue operation depends not on FIFO, is called a priority queue.

Event based Bank Simulation Development of the C++ code to carry out the simulation. We will need the queue data structure. We will need the priority queue. Information about arriving customers will be placed in an input file. Each line of the file contains the items (arrival time,transaction duration)

Arriving Customers’ File Here are a few lines from the input file. 00 30 10 <- customer 1 00 35 05 <- customer 2 00 40 08 00 45 02 00 50 05 00 55 12 01 00 13 01 01 09 “00 30 10” means Customer 1 arrives 30 minutes after bank opens and will need 10 minutes for his transaction. “01 01 09” means customer arrives one hour and one minute after bank opens and transaction will take 9 minutes.

Simulation Procedure The first event to occur is the arrival of the first customer. This event placed in the priority queue. Initially, the four teller queues are empty. The simulation proceeds are follows: When an arrival event is removed from the priority queue, a node representing the customer is placed on the shortest teller queue.

Simulation Procedure If that customer is the only one on a teller queue, a event for his departure is placed on the priority queue. At the same time, the next input line is read and an arrival event is placed in the priority queue. When a departure event is removed from the event priority queue, the customer node is removed from the teller queue.

Simulation Procedure The total time spent by the customer is computed: it is the time spent in the queue waiting and the time taken for the transaction. This time is added to the total time spent by all customers. At the end of the simulation, this total time divided by the total customers served will be average time spent by customers. The next customer in the queue is now served by the teller. A departure event is placed on the event queue.