Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCS-200 Data Structure and Algorithms

Similar presentations


Presentation on theme: "CSCS-200 Data Structure and Algorithms"— Presentation transcript:

1 CSCS-200 Data Structure and Algorithms
Lecture 12-13

2 THE STACK

3 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).

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

5 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.

6 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.

7

8 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.

9 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.

10 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.

11 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.

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

13 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.

14 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.

15 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.

16 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).

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

18 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?

19 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?

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

21 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.

22 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.

23 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.

24

25

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

27 Initialize front and rear

28 Queue is empty now!! rear == front

29 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.

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

31 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

32 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

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

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

35 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

36 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

37 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

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

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

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

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

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

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

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

45 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.

46 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.

47 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

48 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; }

49 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.

50 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

51 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.

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

53 Arriving Customers’ File
Here are a few lines from the input file. <- customer 1 <- customer 2 “ ” means Customer 1 arrives 30 minutes after bank opens and will need 10 minutes for his transaction. “ ” means customer arrives one hour and one minute after bank opens and transaction will take 9 minutes.

54 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.

55 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.

56 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.


Download ppt "CSCS-200 Data Structure and Algorithms"

Similar presentations


Ads by Google