Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 QUEUE v2 by www.asyrani.com. Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.

Similar presentations


Presentation on theme: "CHAPTER 5 QUEUE v2 by www.asyrani.com. Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping."— Presentation transcript:

1 CHAPTER 5 QUEUE v2 by www.asyrani.com

2 Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping mall on Sunday (peak hour) –When it is time for you to pay, you need to queue up. –A lot of people (homogeneous items of elements) in front of you.

3 How does it works? Queues has two ends Elements are removed from the other end. Elements are added at one end.

4 How does it works? Queues has two ends After this guy pay bill at the cashier, He/she is the one That first got out. We called him/her as FIRST IN, FIRST OUT (FIFO)

5 So, what operations involved?

6 Before you queue up at the cashier to pay your groceries, you need to make sure the queue line exists. Make sure the cashier is available. Thus the queue line is available InitializeQueue: initialize the queue to an empty state. 1

7 So, what operations involved? But, sometimes you got no luck at all because when the time you arrives at the counter. The cashier says to you “Sorry sir, my shift is over and you must queue up in another queue line. I need to go back and feed my kittens” destroyQueue: removes all elements from the queue, leaving the Queue empty. 2

8 So, what operations involved? Also, maybe you arrives at the empty queue line but the cashier is there doing nothing. So, it is the checking the empty queue process isEmptyQueue: check wether the Queue empty, if empty it return the value true, otherwise it return value false. 3

9 So, what operations involved? And sometimes the queue is also full (well the security prevent you from enter a full parking space) isFullQueue: check whether the Queue is full, if full it return true, otherwise it return false. 4

10 So, what operations involved? So, when the queue line is not full but it is ok to queue up. You will queue up. addQueue/enQueue adds new element to the rear of Queue,input consists of Queue and new element. Queue must exist and must not be full. 5

11 So, what operations involved? After one of the guy in front of you have paid, then this guy will go away. removeQueue/deQueue: removes the front element from the Queue and store the front element to deqElement. Input consists of the Queue and location where the front element will be stored. Queue must exist and must not empty. 6

12 Queue Operations InitializeQueue: initialize the queue to an empty state. destroyQueue: removes all elements from the queue, leaving the Queue empty. isEmptyQueue: check wether the Queue empty, if empty it return the value true, otherwise it return value false. isFullQueue: check whether the Queue is full, if full it return true, otherwise it return false. addQueue:/enQueue adds new element to the rear of Queue,input consists of Queue and new element. Queue must exist and must not be full. removeQueue/deQueue: removes the front element from the Queue and store the front element to deqElement. Input consists of the Queue and location where the front element will be stored. Queue must exist and must not empty.

13 Enqueue/Dequeue Enqueue 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 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.

14 Example : I/O buffers: queues, scrolls, deques 2. Queue here 1. Open file 3. Get input Input Buffer queues input for the CPU

15 Example : Scheduling queues in a multi-user computer system  Printer queue: When files are submitted to a printer, they are placed in the printer queue. The printer software executes an algorithm something like: for (...;...;...) { while (printerQueue.empty()) sleep 1; printFile = printerQueue.removeQ(); Print(printFile); }

16 Probably uses a priority queue: Items with lower priority are behind all those with higher priority. (Usually a new item is inserted behind those with the same priority.) Example : CPU Scheduling

17 1. Resident queue:On disk, waiting for memory 2. Ready queue:In memory — has everything it needs to run, except the CPU 3. Suspended queue:Waiting for I/O transfer or to be reassigned the CPU 1 2 3 Other Tough Queues:

18 But how to implement queue? Implement the queue as a line or 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?

19 Let us look at example Declare Four (4) space for queue up process

20 Let us look at example Declare Four (4) space for queue up process..and name each space 0 1 2 3 I’ve named it 0,1,2,3

21 Let us look at example Declare Four (4) space for queue up process..and name each space 0 1 2 3 I’ve named it 0,1,2,3 For starting, Front and Rear Share the same place. Why? Because at the moment all four spaces are empty. Example : When you found that the queue line is empty. You are the first and the last customer queue up. Got it? Front/Rear

22 Let us look at example Now.. I want to use special command to put variable there. Let say q.Enqueue([number]) Example : q.Enqueue(2) So what q.Enqueue(2) do? 0 1 2 3

23 Let us look at example Now, we have used the command to put number “2” inside the empty space. q.Enqueue(2) Front Rear -> 2 0 1 2 3

24 Let us look at example Then, I need to put more numbers q.Enqueue(3) Front-> 2 0 Rear-> 3 1 2 3 q.Enqueue(5) Front-> 2 0 3 1 Rear-> 5 2 3

25 Let us look at example Watch that Rear has moved (position updated) after we added few new numbers. q.Enqueue(3) Front-> 2 0 Rear-> 3 1 2 3 q.Enqueue(5) Front-> 2 0 3 1 Rear-> 5 2 3

26 Let us look at example Now, we want to use another command q.Dequeue(item) What is that? “Because the guy in front of you used old credit card So before waiting for card Transaction is processed, the queue is moved its position into a guy behind him.” q.Enqueue(5) Front-> 2 0 3 1 Rear-> 5 2 3

27 Let us look at example We used twice q.Dequeue command. q.Dequeue(2) 2 0 Front-> 3 1 Rear-> 5 2 3 q.Dequeue(3) 2 0 3 1 Front Rear -> 5 2 3

28 Let us look at example And then q.Enqueue command q.Enqueue(10) 2 0 3 1 Front-> 5 2 Rear-> 10 3

29 Let us look at example Suddenly!!!!!! What to do??? It’s already full. q.Enqueue(20)?? 2 0 3 1 Front-> 5 2 Rear-> 10 3

30 Let us look at example Stay calm. Let us “wrap the queue” by convert it into “ring queue” 2 0 3 1 Front-> 5 2 Rear-> 10 3 0 1 2 3 2 3 5 FRONT REAR

31 Let us look at example Since we only have four(4) space in ring queue. It will put the new data in circle.

32 Let us look at example We already have four people queue up (full). -Ahmad (First), Samad (Second), Ali (Third), and Abu (Fourth) -FRONT is Ahmad, REAR is Abu

33 Let us look at example FRONTREAR AHMAD SAMAD ALI ABU THEN SOMEONE COMING!!! HIS NAME IS OPTIMUS PRIME

34 Let us look at example 0 1 2 3 AHMAD SAMAD ALI ABU FRONT REAR THEN SOMEONE COMING!!! HIS NAME IS OPTIMUS PRIME

35 Let us look at example 0 1 2 3 OPTIMUS PRIME SAMAD ALI ABU FRONT REAR OPTIMUS PRIME WILL BE PUT AT “0”!! And REAR also change!!

36 Back to example… Then, 0 1 2 3 20 3 5 10 FRONT REAR q.Enqueue(20) Rear-> 20 0 3 1 Front-> 5 2 10 3

37 Back to example… In coding style if (rear == maxQueue – 1) // Meaning at location “3” rear = 0; //put it back at location “0” else rear = rear + 1; // else put after the last one //we also can use rear = (rear + 1) % maxQueue

38 What if the space has been occupied (full)? Case 1 q.Enqueue(30) Rear-> 20 0 3 1 Front-> 5 2 10 3

39 Queue is empty now!! rear == front

40 QUEUE 0 0 A [0][1][2][3][99][98][97] front rear addQueue A

41 QUEUE 0 2 ABC [0][1][2][3][99][98][97] front rear addQueue B, C

42 QUEUE 1 2 ABC [0][1][2][3][99][98][97] front rear A deqElement deQueue

43 QUEUE Circular Queue [99][0]

44 QUEUE 98 99 XY [0][1][2][3][99][98][97] front rear addQueue Zrear=(rear+1)%maxQueue

45 QUEUE 98 0 XY [0][1][2][3][99][98][97] front rear Z

46 How to program it in C++? First, start your Visual C++ or XCode C++

47 How to program it in C++? First, start your Visual C++ or XCode C++

48 Let’s Code As usual, we declare maximum content in queue

49 Let’s Code -Build a private inside class queue -Declare three variables -int t[MAX] for an array -int al for rear queue -int dl for front queue

50 Let’s Code As usual, a constructor to set up dl and al to -1.

51 Let’s Code -This function add item into queue 1.For starting, we check whether both dl and al is -1. If still empty, we set it to become 0 for both. 2.Then, we add data t[al] = item. 3.For example, if item = 60. Then t[0] = 60. because al = 0 and dl also equal to 0

52 Example add(50) For starting, the dl = al = -1 And we send add (60) 1.Thus command involves:- if(dl==-1 && al==-1) { dl++; al++; } 2. So, dl = al = 0 3. Next command:- t[al] = item 4. So, t[0] = 60 add(60) FRONT (dl) REAR (al) -> 60 0 1 2 3 4

53 Let’s Code -Otherwise if al and dl is not equal to -1 (meaning some data already inside the queue list, so 1.We update the al to al++. 2.If the queue list is full then we send message “Queue is Full” and set back al to al— 3.But, we still add t[al] = item and we replace the

54 Example add(50) For starting, the dl = 0, al = 2 And we send add (30) 1.Thus command involves:- al++; if(al==MAX) { cout<<"Queue is Full\n"; al--; return; } 2. So, al = 3. and check the MAX 3. Next command:- t[al] = item 4. So, t[3] = 30 Add(30) FRONT (dl) -> 60 0 50 1 REAR (al) -> 40 2 3 4 Add(30) FRONT (dl)-> 60 0 50 1 40 2 REAR (al)-> 30 3 4

55 Let’s Code -This function remove the queue

56 Let’s Code -This function remove the queue

57 Let’s Code -This function remove the queue

58 Let’s Code -This function remove the queue

59 Let’s Code

60

61 Queues vs. Stacks Stacks are a LIFO container Store data in the reverse of order received Queues are a FIFO container Store data in the order received Stacks then suggest applications where some sort of reversal or unwinding is desired. Queues suggest applications where service is to be rendered relative to order received. Stacks and Queues can be used in conjunction to compare different orderings of the same data set. From an ordering perspective, then, Queues are the “opposite” of stacks Easy solution to the palindrome problem

62 Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. able was I ere I saw elba madam ere madam We will read the line of text into both a stack and a queue. Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

63 Example: recognizing palindromes

64 Easy Palindrome Check #include “Stack.h”; //include class definition of Stack #include “Queue.h”; //include class definition of Queue #include using namespace std; int main(){ StackS; QueueQ; charch; cout << “Enter string to be tested as palindrome: “; while (cin >> ch){ S.push(ch); Q.addq(ch); } bool pal = true; while (!Q.empty() && pal) { pal = Q.front() == S.pop(); Q.removeq(); } if (pal) cout << “Palindrome!!” << endl; return0; }

65 The end…


Download ppt "CHAPTER 5 QUEUE v2 by www.asyrani.com. Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping."

Similar presentations


Ads by Google