08 ds and algorithm session_11

1,974 views

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,974
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In the problem statement, request numbers are generated in a descending order. As a result, in this case, the queue appears like a sorted list. To avoid confusion, tell the students that there is no such specific sequence in which the items are added in the queue.
  • Tell the students that a queue implemented in the form of a circular array is represented as a circular structure for the sake of clarity. However, the students should not get confused by looking at its circular structure. The basic structure of the array remains the same.
  • Ask the student to modify the algorithm to incorporate the queue overflow condition.
  • Ask the student to modify the above algorithm to check for queue empty condition.
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In this activity, you need to write a program to implement insert and delete operations on a queue implemented in the form of a circular array. You can use a data file provided to you, instead of typing the complete code. The data file that stores the complete program is stored at the given location: TIRM  Datafiles for Faculty  Chapter 07  Activities  CircularQueues_CSharp.txt TIRM  Datafiles for Faculty  Chapter 07  Activities  CircularQueues_C++.txt Also explain the program to students.
  • Faculty should ask the students “Will this algorithm work if there is only one node in the list?”
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • 08 ds and algorithm session_11

    1. 1. Data Structures and AlgorithmsObjectives In this session, you will learn to: Identify the features of queues Implement the different types of queues Apply queues to solve programming problems Store and search data by using hashing Ver. 1.0 Session 11
    2. 2. Data Structures and AlgorithmsDefining Queues Consider a situation where you have to create an application with the following set of requirements: Application should serve the requests of multiple users. At a time, only one request can be processed. The request, which came first should be given priority. However, the rate at which the requests are received is much faster than the rate at which they are processed. Therefore, you need to store the request somewhere until they are processed. How can you solve this problem? You can solve this problem by storing the requests in such a manner so that they are retrieved in the order of their arrival. A data structure called queue stores and retrieves data in the order of its arrival. A queue is also called a FIFO list. Ver. 1.0 Session 11
    3. 3. Data Structures and AlgorithmsDefining Queues (Contd.) Elements are of elements in which an element is from the Queue is a listinserted at the rear end and deleted inserted front end. at one end and deleted from the other end of the queue. FRONT REAR B A E C D Ver. 1.0 Session 11
    4. 4. Data Structures and AlgorithmsJust a minute A queue is also known as a __________ list. Answer: FIFO Ver. 1.0 Session 11
    5. 5. Data Structures and AlgorithmsIdentifying Various Operations on Queues Various operations implemented on a queue are: Insert Delete FRONT REAR B A E C D Ver. 1.0 Session 11
    6. 6. Data Structures and AlgorithmsIdentifying Various Operations on Queues (Contd.) • Insert: It refers to the addition of an item in the queue. Suppose you want to add an item F in the following queue. Since the items are inserted at the rear end, therefore, F is inserted after D. Now F becomes the rear end. FRONT REAR REAR F B A E C D F Ver. 1.0 Session 11
    7. 7. Data Structures and AlgorithmsIdentifying Various Operations on Queues (Contd.) • Delete: It refers to the deletion of an item from the queue. Since the items are deleted from the front end, therefore, item B is removed from the queue. Now A becomes the front end of the queue. FRONT FRONT REAR B A E C D F Ver. 1.0 Session 11
    8. 8. Data Structures and AlgorithmsJust a minute Queues are the data structures in which data can be added at one end called ______ and deleted from the other end called _________. Answer: rear, front Ver. 1.0 Session 11
    9. 9. Data Structures and AlgorithmsImplementing a Queue Using an Array Problem Statement: Consider a scenario of a bank. When the customer visits the counter, a request entry is made and the customer is given a request number. After receiving request numbers, a customer has to wait for some time. The customer requests needs to be queued into the system and processed on the basis of their arrival. You need to implement an appropriate data storage mechanism to store these requests in the system. Ver. 1.0 Session 11
    10. 10. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) How can a request this problem? positions, you need To keep tracksolve number, you need to perform the to insert you of the rear and front following steps: declare two solve this problem by implementing a queue. You can integer variables, REAR and FRONT. Let us implementvalue of REAR by FRONT are stores these Increment the a queue using 1. If the queue is empty, REAR andan array that set to –1. request numbers in theindex position REAR in the array. Insert the element at order of their arrival. FRONT = –1 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    11. 11. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Write an algorithm to insert values in a queue implemented through array. Algorithm to insert values in a queue: 1. If the queue is empty: a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. Ver. 1.0 Session 11
    12. 12. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Let us now insert request numbers 1. If the queue is empty: Requestin the following queue. number generated 3 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    13. 13. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    14. 14. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    15. 15. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 0 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    16. 16. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
    17. 17. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Ver. 1.0 Session 11
    18. 18. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) • If the queue is empty: Request number generated 5 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Ver. 1.0 Session 11
    19. 19. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REARREAR = 1 =0 310 0 1 2 3 4 Ver. 1.0 Session 11
    20. 20. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
    21. 21. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
    22. 22. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
    23. 23. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = REAR = 2 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
    24. 24. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
    25. 25. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
    26. 26. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
    27. 27. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = REAR = 3 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
    28. 28. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
    29. 29. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    30. 30. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    31. 31. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR =REAR = 4 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    32. 32. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 4 310 5 7 10 15 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
    33. 33. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) The requests stored in the queue are served on a first-come-first-served basis. As the requests are being served, the corresponding request numbers needs to be deleted from the queue. Ver. 1.0 Session 11
    34. 34. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Write an algorithm to delete an element from a queue implemented through array. Algorithm to delete an element from a queue: 1. Retrieve the element at index FRONT. 3. Increment FRONT by 1. Ver. 1.0 Session 11
    35. 35. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed Let us see how requests are 1. Retrieve the element at index FRONT. deleted from the queue once they 3. Increment FRONT by 1. get processed. FRONT = 0 REAR = 4 3 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
    36. 36. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 0 REAR = 4 310 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
    37. 37. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 0 FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Delete operation complete Ver. 1.0 Session 11
    38. 38. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. Retrieve the element at index FRONT. 3. Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
    39. 39. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
    40. 40. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = FRONT = 2 1 REAR = 4 10 7 10 15 0 1 2 3 4 Delete operation complete Ver. 1.0 Session 11
    41. 41. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) To you delete an insert or delete operation, you need to As implement elements from the queue, the queue moves incrementarray. down the the values of REAR or FRONT by one, respectively. The disadvantage of this approach is that the storage space However, these values are never never used again. in the beginning is discarded and decremented. Consider the following queue. FRONT = 3 REAR = 4 10 10 15 0 1 2 3 4 REAR is at the last index position. Therefore, you cannot insert elements in this queue, even though there is space for them. This means that all the initial vacant positions go waste. Ver. 1.0 Session 11
    42. 42. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) How can you solve this zero index position, every delete To maintain FRONT at problem? operation would requireproblem shift keep FRONT always at the One way to solve this you to is to all the succeeding elements in the array one position left. zero index position. Let Refer to the following queue. us implement a delete operation on the following queue. FRONT = 0 REAR = 3 3 10 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    43. 43. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) To maintain FRONT at zero index position, every delete operation would require you to shift all the succeeding elements in the array one position left. Let us implement a delete operation on the following queue. FRONT = 0 REAR = 3 5 10 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    44. 44. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) To maintain FRONT at zero index position, every delete operation would require you to shift all the succeeding elements in the array one position left. Let us implement a delete operation on the following queue. FRONT = 0 REAR = 3 5 10 7 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    45. 45. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) To maintain FRONT at zero index position, every delete operation would require you to shift all the succeeding elements in the array one position left. Let us implement a delete operation on the following queue. FRONT = 0 REAR = 3 5 10 7 10 10 0 1 2 3 4 Ver. 1.0 Session 11
    46. 46. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) To maintain FRONT at zero index position, every delete operation would require you to shift all the succeeding elements in the array one position left. Let us implement a delete operation on the following queue. FRONT = 0 REAR = 2REAR = 3 5 10 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    47. 47. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Disadvantage of this approach: Advantage of this approach: Every delete operation all the empty positions in an array. It enables you to utilize requires you to shift all the succeeding elements in the queue one position there Therefore, unlike the previous case,left. is no wastage of space. is lengthy, this can be very time consuming. If the list FRONT = 0 REAR = 2 5 10 7 10 0 1 2 3 4 Ver. 1.0 Session 11
    48. 48. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) An effective way to solve this problem is to implement the queue in the form of a circular array. In this approach, if REAR is at the last index position and if there is space in the beginning of an array, then you can set the value of REAR to zero and start inserting elements from the beginning. REAR = 0 FRONT = 3 REAR = 4 Insert 5 510 10 15 0 1 2 3 4 Ver. 1.0 Session 11
    49. 49. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) The cells in the array are treated as if they are arranged in a ring. [0] [4] [1] REAR = 3 10 7 FRONT = 2 [3] [2] Ver. 1.0 Session 11
    50. 50. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Let usan algorithm to insert Write now implement a few 1. If the queue is empty (If FRONT= –1): Requestinsert operations on the following values in a queue implemented number generated 15 a. Set FRONT = 0 b. Set REAR = 0 circular queue: as a circular array. c. Go to step 4 FRONT = 1 REAR = 3 • If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    51. 51. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 15 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 3 • If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    52. 52. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 15 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 3 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    53. 53. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 15 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 3 = 4 REAR 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    54. 54. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 15 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 4 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
    55. 55. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 4 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    56. 56. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 4 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    57. 57. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 FRONT = 1 REAR = 4 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    58. 58. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 REAR = 4 3. If REAR is at the last index position: • Set REAR = 0 10 20 23 10 15 • Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    59. 59. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: • Set REAR = 0 10 20 23 10 15 • Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    60. 60. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 17 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
    61. 61. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 25 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    62. 62. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 25 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    63. 63. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 25 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
    64. 64. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) 1. If the queue is empty (If FRONT= –1): Request number generated 25 a. Set FRONT = 0 b. Set REAR = 0 c. Go to step 4 REAR = 0 FRONT = 1 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Before cannot be incremented operation, you should always check for the Inserting an element in ainsert because the queue is full. REAR implementing an full queue leads to queue overflow. queue full condition. Ver. 1.0 Session 11
    65. 65. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) The conditions for queue full are as follows: If FRONT = 0 and REAR If FRONT = REAR + 1 is at the last index position OR FRONT = 0 REAR = 4 REAR = 2 FRONT = 3 310 5 7 10 15 17 10 20 23 10 15 0 1 2 3 4 0 1 2 3 4 Ver. 1.0 Session 11
    66. 66. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Modified algorithm for inserting an 1. If the queue is full: element in a circular queue. a. Display “Queue overflow” b. Exit 3. If queue is empty (If FRONT = –1): a. Set FRONT = 0 b. Set REAR = 0 c. Go to Step 5 • If REAR is at the last index position: a. Set REAR = 0 b. Go to step 5 • Increment REAR by 1 • Queue[REAR] = element Ver. 1.0 Session 11
    67. 67. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Writelet us implementimplement delete operation in a queue Now an algorithm to a delete operation on a queue implemented asthe form ofarray. in a circular a circular array. To delete an element, you need to increment the value of FRONT by one. This is same as that of a linear queue. However, if the element to be deleted is present at the last index position, then the value FRONT is reset to zero. If there is only one element present in the queue, then the value of FRONT and REAR is set to –1. Ver. 1.0 Session 11
    68. 68. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) Algorithm to delete an element 1. If there is only one element in the queue: from a circular queue. a. Set FRONT = –1 b. Set REAR = –1 c. Exit REAR = 0 FRONT = 4 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    69. 69. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit REAR = 0 FRONT = 4 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    70. 70. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed • If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit REAR = 0 FRONT = 4 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    71. 71. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit REAR = 0 FRONT = 4 • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    72. 72. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 FRONT = 4 4. If FRONT is at the last index position: • Set FRONT = 0 • Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    73. 73. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: • Set FRONT = 0 • Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
    74. 74. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    75. 75. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed • If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    76. 76. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    77. 77. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
    78. 78. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    79. 79. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    80. 80. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
    81. 81. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
    82. 82. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
    83. 83. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) The queue stage, empty. try to At this is now if you 1. If there is only one element in the queue: implement a delete operation, it a. Set FRONT = –1 will result in queue underflow. b. Set REAR = –1 c. Exit 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Therefore, before implementing a delete operation, you first need to check whether the queue is empty or not. Ver. 1.0 Session 11
    84. 84. Data Structures and AlgorithmsImplementing a Queue Using an Array (Contd.) The condition for queue empty an Modified algorithm for deleting is: • If the queue is empty: // If // FRONT = –1 element from a circular queue: FRONT = –1 a. Display “Queue underflow” b. Exit • If there is only one element in the queue: // If FRONT = REAR a. Set FRONT = –1 b. Set REAR = –1 c. Exit 7. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit • Increment FRONT by 1 Ver. 1.0 Session 11
    85. 85. Data Structures and AlgorithmsJust a minute What is the advantage of implementing a queue in the form of a circular array, instead of a linear array structure? Answer: If you implement a queue in the form of a linear array, you can add elements only in the successive index positions. However, when you reach the end of the queue, you cannot start inserting elements from the beginning, even if there is space for them at the beginning. You can overcome this disadvantage by implementing a queue in the form of a circular array. In this case, you can keep inserting elements till all the index positions are filled. Hence, it solves the problem of unutilized space. Ver. 1.0 Session 11
    86. 86. Data Structures and AlgorithmsActivity: Implementing a Queue Using Circular Array Problem Statement: Write a program to implement insert and delete operations on a queue implemented in the form of a circular array. Ver. 1.0 Session 11
    87. 87. Data Structures and AlgorithmsImplementing a Queue Using a Linked List What is the disadvantage of implementing a queue as an array? To implement a queue using an array, you must know the maximum number of elements in the queue in advance. To solve this problem, you should implement the queue in the form of a linked list. Ver. 1.0 Session 11
    88. 88. Data Structures and AlgorithmsImplementing a Queue Using a Linked List (Contd.) To keep track of the rear and front positions, you need to declare two variables/pointers, REAR and FRONT, that will always point to the rear and front end of the queue respectively. If the queue is empty, REAR and FRONT point to NULL. FRONT REAR 310 5 7 15 Ver. 1.0 Session 11
    89. 89. Data Structures and AlgorithmsInserting an Element in a Linked Queue Write an algorithm to implement insert operation in a linked queue. Ver. 1.0 Session 11
    90. 90. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) Algorithminitially, the queue Suppose to insert an 1. Allocate memory for the new node. Requestelement generated queue. number is empty.in a linked 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    91. 91. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    92. 92. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    93. 93. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    94. 94. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    95. 95. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following FRONT = NULL steps: FRONT • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    96. 96. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following steps: FRONT REAR • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    97. 97. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
    98. 98. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    99. 99. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    100. 100. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    101. 101. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    102. 102. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
    103. 103. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit • Make the next field of REAR point to the new 10 3 10 5 node. • Make REAR point to the new node. Ver. 1.0 Session 11
    104. 104. Data Structures and AlgorithmsInserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit • Make the next field of REAR point to the new 10 3 10 5 node. • Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
    105. 105. Data Structures and AlgorithmsDeleting an Element from a Linked Queue Write an algorithm to implement the delete operation on a linked queue. Ver. 1.0 Session 11
    106. 106. Data Structures and AlgorithmsDeleting an Element from a Linked Queue (Contd.) One request processed delete Algorithm to implement • If the queue is empty: // FRONT = NULL operation on a linked queue. a. Display “Queue empty” b. Exit 3. Mark the node marked FRONT as current 5. Make FRONT point to the next node in its sequence FRONT REAR 7. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
    107. 107. Data Structures and AlgorithmsDeleting an Element from a Linked Queue (Contd.) One request processed • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 3. Mark the node marked FRONT as current 5. Make FRONT point to the next node in its sequence FRONT REAR 7. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
    108. 108. Data Structures and AlgorithmsDeleting an Element from a Linked Queue (Contd.) One request processed • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit • Mark the node marked FRONT as current • Make FRONT point to the next node in its sequence FRONT REAR • Release the memory for the node marked as current 310 5 7 10 current Ver. 1.0 Session 11

    ×