SlideShare a Scribd company logo
1 of 128
Data Structures and Algorithms
Objectives


                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
Data Structures and Algorithms
Defining 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
Data Structures and Algorithms
Defining 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
Data Structures and Algorithms
Just a minute


                A queue is also known as a __________ list.




                Answer:
                   FIFO




     Ver. 1.0                                                 Session 11
Data Structures and Algorithms
Identifying 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
Data Structures and Algorithms
Identifying 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
Data Structures and Algorithms
Identifying 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
Data Structures and Algorithms
Just 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                    How can you of thethis problem? positions, you need
                    To keep tracksolve number, you need to perform the to
                         insert a request 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
Data Structures and Algorithms
Implementing 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.

                2. Increment REAR by 1.

                3. Store the element at index position REAR in the array.




     Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Implementing 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.

                                                   2.   Increment REAR by 1.

                                                   3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                           1.   If the queue is empty:

        Request number generated   3             a.   Set FRONT = 0.

                                           2.   Increment REAR by 1.

                                           3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                 1.   If the queue is empty:

        Request number generated         3             a.   Set FRONT = 0.

                                                 2.   Increment REAR by 1.

                                                 3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                   1.   If the queue is empty:

        Request number generated           3             a.   Set FRONT = 0.

                                                   2.   Increment REAR by 1.

                                                   3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                           1.   If the queue is empty:

        Request number generated               3                 a.   Set FRONT = 0.

                                                           2.   Increment REAR by 1.

                                                           3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                    1.   If the queue is empty:

        Request number generated            5             a.   Set FRONT = 0.

                                                    2.   Increment REAR by 1.

                                                    3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                    1.   If the queue is empty:

        Request number generated            5             a.   Set FRONT = 0.

                                                    2.   Increment REAR by 1.

                                                    3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                    1.   If the queue is empty:

        Request number generated            5             a.   Set FRONT = 0.

                                                    2.   Increment REAR by 1.

                                                    3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                         1.   If the queue is empty:

        Request number generated              5                a.   Set FRONT = 0.

                                                         2.   Increment REAR by 1.

                                                         3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                     1.   If the queue is empty:

        Request number generated             7             a.   Set FRONT = 0.

                                                     2.   Increment REAR by 1.

                                                     3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                     1.   If the queue is empty:

        Request number generated             7             a.   Set FRONT = 0.

                                                     2.   Increment REAR by 1.

                                                     3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                            1.   If the queue is empty:

        Request number generated                    7             a.   Set FRONT = 0.

                                                            2.   Increment REAR by 1.

                                                            3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                          1.   If the queue is empty:

        Request number generated                  7             a.   Set FRONT = 0.

                                                          2.   Increment REAR by 1.

                                                          3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                          1.   If the queue is empty:

        Request number generated                 10             a.   Set FRONT = 0.

                                                          2.   Increment REAR by 1.

                                                          3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                          1.   If the queue is empty:

        Request number generated                 10             a.   Set FRONT = 0.

                                                          2.   Increment REAR by 1.

                                                          3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                            1.   If the queue is empty:

        Request number generated               10                 a.   Set FRONT = 0.

                                                            2.   Increment REAR by 1.

                                                            3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                            1.   If the queue is empty:

        Request number generated               10                 a.   Set FRONT = 0.

                                                            2.   Increment REAR by 1.

                                                            3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                         1.   If the queue is empty:

        Request number generated            15                 a.   Set FRONT = 0.

                                                         2.   Increment REAR by 1.

                                                         3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                         1.   If the queue is empty:

        Request number generated            15                 a.   Set FRONT = 0.

                                                         2.   Increment REAR by 1.

                                                         3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                           1.   If the queue is empty:

        Request number generated            15                   a.   Set FRONT = 0.

                                                           2.   Increment REAR by 1.

                                                           3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                               1.   If the queue is empty:

        Request number generated               15                    a.   Set FRONT = 0.

                                                               2.   Increment REAR by 1.

                                                               3.   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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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.

                 2.   Increment FRONT by 1.




     Ver. 1.0                                                          Session 11
Data Structures and Algorithms
Implementing 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
                                                        2.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                One request processed                     1.   Retrieve the element at index
                                                               FRONT.

                                                          2.   Increment FRONT by 1.




                FRONT = 0                      REAR = 4



                            310   5   7   10   15

                            0     1   2   3     4




     Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                                1.   Retrieve the element at index
                                                                     FRONT.

                                                                2.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                One request processed                   1.   Retrieve the element at index
                                                             FRONT.

                                                        2.   Increment FRONT by 1.




                      FRONT = 1              REAR = 4



                        10    5   7     10   15

                        0     1   2     3     4




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                One request processed                   1.   Retrieve the element at index
                                                             FRONT.

                                                        2.   Increment FRONT by 1.




                      FRONT = 1              REAR = 4



                        10    5   7     10   15

                        0     1   2     3     4




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                                                     1.   Retrieve the element at index
                                                          FRONT.

                                                     2.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                To implement an insert or delete queue, theyou need to
                As you delete elements from the operation, 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 decremented.
                in the beginning is discarded and never used again.
                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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
                                                     2.   If REAR is at the last index position:

                                                           a.    Set REAR = 0
                10     20        23   10                   b.    Go to step 4

                                                     3.   Increment REAR by 1
                0      1         2    3       4
                                                     4.   Queue[REAR] = element




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Implementing 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
                                                     2.   If REAR is at the last index position:

                                                           a.    Set REAR = 0
                10     20        23   10                   b.    Go to step 4

                                                     3.   Increment REAR by 1
                0      1         2    3       4
                                                     4.   Queue[REAR] = element




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Implementing 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
                                                     2.   If REAR is at the last index position:

                                                           a.    Set REAR = 0
                10     20        23   10                   b.    Go to step 4

                                                     3.   Increment REAR by 1
                0      1         2    3       4
                                                     4.   Queue[REAR] = element




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Implementing 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
                                                         2.   If REAR is at the last index position:

                                                               a.    Set REAR = 0
                10     20        23   10                       b.    Go to step 4

                                                         3.   Increment REAR by 1
                0      1         2    3       4
                                                         4.   Queue[REAR] = element




     Ver. 1.0                                                                         Session 11
Data Structures and Algorithms
Implementing 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
                                                      2.   If REAR is at the last index position:

                                                            a.    Set REAR = 0
                10     20        23   10   15               b.    Go to step 4

                                                      3.   Increment REAR by 1
                0      1         2    3    4
                                                      4.   Queue[REAR] = element

                     Insertion complete




     Ver. 1.0                                                                      Session 11
Data Structures and Algorithms
Implementing 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
                                                      2.   If REAR is at the last index position:

                                                            a.    Set REAR = 0
                10     20        23   10   15               b.    Go to step 4

                                                      3.   Increment REAR by 1
                0      1         2    3    4
                                                      4.   Queue[REAR] = element




     Ver. 1.0                                                                      Session 11
Data Structures and Algorithms
Implementing 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
                                                      2.   If REAR is at the last index position:

                                                            a.    Set REAR = 0
                10     20        23   10   15               b.    Go to step 4

                                                      3.   Increment REAR by 1
                0      1         2    3    4
                                                      4.   Queue[REAR] = element




     Ver. 1.0                                                                      Session 11
Data Structures and Algorithms
Implementing 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
                                                      2.   If REAR is at the last index position:

                                                            a.    Set REAR = 0
                10     20        23   10   15               b.    Go to step 4

                                                      3.   Increment REAR by 1
                0      1         2    3    4
                                                      4.   Queue[REAR] = element




     Ver. 1.0                                                                      Session 11
Data Structures and Algorithms
Implementing 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
                                                          2.   If REAR is at the last index position:

                                                                a.    Set REAR = 0
                      10   20        23   10   15               b.    Go to step 4

                                                          3.   Increment REAR by 1
                     0     1         2    3    4
                                                          4.   Queue[REAR] = element




     Ver. 1.0                                                                          Session 11
Data Structures and Algorithms
Implementing 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
                                                    2.   If REAR is at the last index position:

                                                          a.    Set REAR = 0
                      10   20        23   10   15         b.    Go to step 4

                                                    3.   Increment REAR by 1
                     0     1         2    3    4
                                                    4.   Queue[REAR] = element




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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
                                                    2.   If REAR is at the last index position:

                                                          a.    Set REAR = 0
                     17
                      10   20        23   10   15         b.    Go to step 4

                                                    3.   Increment REAR by 1
                     0     1         2    3    4
                                                    4.   Queue[REAR] = element

                    Insertion complete




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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
                                                    2.   If REAR is at the last index position:

                                                          a.    Set REAR = 0
                     17
                      10   20        23   10   15         b.    Go to step 4

                                                    3.   Increment REAR by 1
                     0     1         2    3    4
                                                    4.   Queue[REAR] = element




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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
                                                    2.   If REAR is at the last index position:

                                                          a.    Set REAR = 0
                     17
                      10   20        23   10   15         b.    Go to step 4

                                                    3.   Increment REAR by 1
                     0     1         2    3    4
                                                    4.   Queue[REAR] = element




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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
                                                    2.   If REAR is at the last index position:

                                                          a.    Set REAR = 0
                     17
                      10   20        23   10   15         b.    Go to step 4

                                                    3.   Increment REAR by 1
                     0     1         2    3    4
                                                    4.   Queue[REAR] = element




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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
                                                            2.   If REAR is at the last index position:

                                                                  a.    Set REAR = 0
                     17
                      10   20        23   10   15                 b.    Go to step 4

                                                            3.   Increment REAR by 1
                     0     1         2    3    4
                                                            4.   Queue[REAR] = element


        Before cannot be incremented operation, youqueueis always check for the
        Inserting an element in ainsert because the queue overflow.
        REAR implementing an full queue leads to should full.
        queue full condition.




     Ver. 1.0                                                                            Session 11
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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

                                                      2.   If queue is empty (If FRONT = –1):

                                                            a.    Set FRONT = 0
                                                            b.    Set REAR = 0
                                                            c.    Go to Step 5

                                                      1.   If REAR is at the last index position:

                                                            a.    Set REAR = 0
                                                            b.    Go to step 5

                                                      2.   Increment REAR by 1

                                                      3.   Queue[REAR] = element




     Ver. 1.0                                                                      Session 11
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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   2.   If FRONT is at the last index
                                                         position:

                                                          a.    Set FRONT = 0
                                                          b.    Exit
                  17
                   10   20                 15
                                                    3.   Increment FRONT by 1
                   0    1      2    3       4




     Ver. 1.0                                                                   Session 11
Data Structures and Algorithms
Implementing 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   2.   If FRONT is at the last index
                                                             position:

                                                              a.    Set FRONT = 0
                                                              b.    Exit
                      17
                       10   20                 15
                                                        3.   Increment FRONT by 1
                      0     1     2     3       4




     Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Implementing 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   2.   If FRONT is at the last index
                                                             position:

                                                              a.    Set FRONT = 0
                                                              b.    Exit
                      17
                       10   20                 15
                                                        3.   Increment FRONT by 1
                      0     1     2     3       4




     Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Implementing 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   2.   If FRONT is at the last index
                                                             position:

                                                              a.    Set FRONT = 0
                                                              b.    Exit
                      17
                       10   20                 15
                                                        3.   Increment FRONT by 1
                      0     1     2     3       4




     Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Implementing 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   2.   If FRONT is at the last index
                                                              position:

                                                               a.    Set FRONT = 0
                                                               b.    Exit
                       17
                        10    20                15
                                                         3.   Increment FRONT by 1
                       0      1     2    3       4




     Ver. 1.0                                                                        Session 11
Data Structures and Algorithms
Implementing 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             2.   If FRONT is at the last index
                                                        position:

                                                         a.    Set FRONT = 0
                                                         b.    Exit
                       17
                        10    20
                                                   3.   Increment FRONT by 1
                       0      1     2    3     4

                           Deletion complete




     Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                      position:

                                                       a.    Set FRONT = 0
                                                       b.    Exit
                       17
                        10    20
                                                 3.   Increment FRONT by 1
                       0      1     2    3   4




     Ver. 1.0                                                                Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                      position:

                                                       a.    Set FRONT = 0
                                                       b.    Exit
                       17
                        10    20
                                                 3.   Increment FRONT by 1
                       0      1     2    3   4




     Ver. 1.0                                                                Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                      position:

                                                       a.    Set FRONT = 0
                                                       b.    Exit
                       17
                        10    20
                                                 3.   Increment FRONT by 1
                       0      1     2    3   4




     Ver. 1.0                                                                Session 11
Data Structures and Algorithms
Implementing 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              2.   If FRONT is at the last index
                                                          position:

                                                           a.    Set FRONT = 0
                                                           b.    Exit
                       17
                        10    20
                                                     3.   Increment FRONT by 1
                       0      1      2    3      4

                             Deletion complete




     Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                        position:

                                                         a.    Set FRONT = 0
                                                         b.    Exit
                      10      20
                                                   3.   Increment FRONT by 1
                      0       1       2    3   4




     Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                        position:

                                                         a.    Set FRONT = 0
                                                         b.    Exit
                      10      20
                                                   3.   Increment FRONT by 1
                      0       1       2    3   4




     Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Implementing 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           2.   If FRONT is at the last index
                                                        position:

                                                         a.    Set FRONT = 0
                                                         b.    Exit
   FRONT = –1         10      20
                                                   3.   Increment FRONT by 1
                      0       1       2    3   4




     Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Implementing 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            2.   If FRONT is at the last index
                                                     position:

                                                      a.    Set FRONT = 0
                                                      b.    Exit
   FRONT = –1         10
                                                3.   Increment FRONT by 1
   REAR = –1          0    1      2     3   4




     Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Implementing 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

                                                2.   If FRONT is at the last index
                                                     position:

                                                      a.    Set FRONT = 0
                                                      b.    Exit
   FRONT = –1         10
                                                3.   Increment FRONT by 1
   REAR = –1          0     1    2      3   4


                           Deletion complete




     Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Implementing 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

                                                         2.   If FRONT is at the last index
                                                              position:

                                                               a.    Set FRONT = 0
                                                               b.    Exit
   FRONT = –1          10
                                                         3.   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
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)


                The condition for queue empty an
                Modified algorithm for deleting is:   1.   If the queue is empty: // If
                                                                                 // FRONT = –1
                element from a circular queue:
                     FRONT = –1                              a.    Display “Queue underflow”
                                                             b.    Exit

                                                      2.   If there is only one element in the
                                                           queue: // If FRONT = REAR

                                                            a.    Set FRONT = –1
                                                            b.    Set REAR = –1
                                                            c.    Exit

                                                      3.   If FRONT is at the last index
                                                           position:

                                                            a.    Set FRONT = 0
                                                            b.    Exit

                                                      3.   Increment FRONT by 1




     Ver. 1.0                                                                     Session 11
Data Structures and Algorithms
Just 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
Data Structures and Algorithms
Activity: 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Implementing 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
Data Structures and Algorithms
Inserting an Element in a Linked Queue


                Write an algorithm to implement insert operation in a linked
                queue.




     Ver. 1.0                                                         Session 11
Data Structures and Algorithms
Inserting 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          2.   Assign value to the data field of the new
                                                     node.

                                                3.   Make the next field of the new node point to
                                                     NULL.
        REAR = NULL
                                                4.   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

                                                5.   Make the next field of REAR point to the new
                                                     node.

                                                6.   Make REAR point to the new node.




      Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
        REAR = NULL
                                        4.   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

                                        5.   Make the next field of REAR point to the new
                                             node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
        REAR = NULL
                                        4.   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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
        REAR = NULL
                                        4.   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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
        REAR = NULL
                                        4.   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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                        4.   If the queue is empty, execute the following
       FRONT = NULL
                                             steps:
    FRONT
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   3   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.
        REAR = NULL
                                        4.   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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                              1.     Allocate memory for the new node.
         Request number generated   3         2.     Assign value to the data field of the new
                                                     node.

                                              3.     Make the next field of the new node point to
                                                     NULL.

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

                                              5.     Make the next field of REAR point to the new
                 10
                 3                                   node.

                                              6.     Make REAR point to the new node.

                         Insert operation complete




      Ver. 1.0                                                                       Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3                           node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3    10
                      5                      node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3    10
                      5                      node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3    10
                      5                      node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                        1.   Allocate memory for the new node.
         Request number generated   5   2.   Assign value to the data field of the new
                                             node.

                                        3.   Make the next field of the new node point to
                                             NULL.

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

                                        5.   Make the next field of REAR point to the new
                 10
                 3    10
                      5                      node.

                                        6.   Make REAR point to the new node.




      Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Inserting an Element in a Linked Queue (Contd.)


                                                1.     Allocate memory for the new node.
         Request number generated     5         2.     Assign value to the data field of the new
                                                       node.

                                                3.     Make the next field of the new node point to
                                                       NULL.

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

                                                5.     Make the next field of REAR point to the new
                 10
                 3    10
                      5                                node.

                                                6.     Make REAR point to the new node.

                           Insert operation complete




      Ver. 1.0                                                                         Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue


                Write an algorithm to implement the delete operation on a
                linked queue.




     Ver. 1.0                                                        Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue (Contd.)


                 One request processed delete
                   Algorithm to implement         1.   If the queue is empty: // FRONT = NULL

                   operation on a linked queue.         a.   Display “Queue empty”
                                                        b.   Exit

                                                  2.   Mark the node marked FRONT as current

                                                  3.   Make FRONT point to the next node in its
                                                       sequence
      FRONT                             REAR
                                                  4.   Release the memory for the node marked
                                                       as current

           310          5        7        10




      Ver. 1.0                                                                    Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue (Contd.)


                 One request processed          1.   If the queue is empty: // FRONT = NULL

                                                      a.   Display “Queue empty”
                                                      b.   Exit

                                                2.   Mark the node marked FRONT as current

                                                3.   Make FRONT point to the next node in its
                                                     sequence
      FRONT                              REAR
                                                4.   Release the memory for the node marked
                                                     as current

           310          5       7          10




      Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue (Contd.)


                 One request processed          1.   If the queue is empty: // FRONT = NULL

                                                      a.   Display “Queue empty”
                                                      b.   Exit

                                                2.   Mark the node marked FRONT as current

                                                3.   Make FRONT point to the next node in its
                                                     sequence
      FRONT                              REAR
                                                4.   Release the memory for the node marked
                                                     as current

           310          5       7          10


        current




      Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue (Contd.)


                 One request processed          1.   If the queue is empty: // FRONT = NULL

                                                      a.   Display “Queue empty”
                                                      b.   Exit

                                                2.   Mark the node marked FRONT as current

                                                3.   Make FRONT point to the next node in its
                                                     sequence
      FRONT          FRONT               REAR
                                                4.   Release the memory for the node marked
                                                     as current

           310          5       7          10


        current




      Ver. 1.0                                                                  Session 11
Data Structures and Algorithms
Deleting an Element from a Linked Queue (Contd.)


                 One request processed           1.   If the queue is empty: // FRONT = NULL

                                                       a.   Display “Queue empty”
                                                       b.   Exit

                                                 2.   Mark the node marked FRONT as current

                                                 3.   Make FRONT point to the next node in its
                                                      sequence
                     FRONT               REAR
                                                 4.   Release the memory for the node marked
                                                      as current

           3            5       7          10


        current
                                    Delete operation complete

    Memory released



      Ver. 1.0                                                                   Session 11
Data Structures and Algorithms
Just a minute


                How does the implementation of a linked list differ from that
                of a linked queue?




                Answer:
                    In a linked list, you can insert and delete elements anywhere in
                    the list. However, in a linked queue, insertion and deletion
                    takes place only from the ends. More specifically, insertion
                    takes place at the rear end and deletion takes place at the
                    front end of the queue.


     Ver. 1.0                                                               Session 11
Data Structures and Algorithms
Applications of Queues


                Queues offer a lot of practical applications, such as:
                   Printer Spooling
                   CPU Scheduling
                   Mail Service
                   Keyboard Buffering
                   Elevator




     Ver. 1.0                                                            Session 11
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session
ds and algorithm session

More Related Content

What's hot

04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05Niit Care
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listAndres Mendez-Vazquez
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queuesAndres Mendez-Vazquez
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays ConceptsVicter Paul
 
Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)Janki Shah
 
AIN102S Access string function sample queries
AIN102S Access string function sample queriesAIN102S Access string function sample queries
AIN102S Access string function sample queriesDan D'Urso
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
C Programming : Arrays and Functions
C Programming : Arrays and FunctionsC Programming : Arrays and Functions
C Programming : Arrays and FunctionsSelvaraj Seerangan
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSM.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSSupriya Radhakrishna
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Gouda Mando
 

What's hot (20)

04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
 
Stack and Queue
Stack and QueueStack and Queue
Stack and Queue
 
Lesson 3 simple sorting
Lesson 3   simple sortingLesson 3   simple sorting
Lesson 3 simple sorting
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)
 
Chapter03
Chapter03Chapter03
Chapter03
 
Chapter11
Chapter11Chapter11
Chapter11
 
AIN102S Access string function sample queries
AIN102S Access string function sample queriesAIN102S Access string function sample queries
AIN102S Access string function sample queries
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
C Programming : Arrays and Functions
C Programming : Arrays and FunctionsC Programming : Arrays and Functions
C Programming : Arrays and Functions
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSM.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 

Similar to ds and algorithm session

QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)Mehedi Hasan
 
computer notes - Memory organization
computer notes - Memory organizationcomputer notes - Memory organization
computer notes - Memory organizationecomputernotes
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2AzharIqbal710687
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structureMuhammad Ismail
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptxDwijBaxi
 
Queue data structure
Queue data structureQueue data structure
Queue data structureMekk Mhmd
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...trangiaphuc362003181
 
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docx
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docxCMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docx
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docxmary772
 

Similar to ds and algorithm session (20)

Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
computer notes - Memory organization
computer notes - Memory organizationcomputer notes - Memory organization
computer notes - Memory organization
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Datastructure
DatastructureDatastructure
Datastructure
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Lec3
Lec3Lec3
Lec3
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structure
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docx
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docxCMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docx
CMPE2300 – Lab01 - Defrag-o-maticIn this lab you will crea.docx
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

ds and algorithm session

  • 1. Data Structures and Algorithms Objectives 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. Data Structures and Algorithms Defining 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. Data Structures and Algorithms Defining 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. Data Structures and Algorithms Just a minute A queue is also known as a __________ list. Answer: FIFO Ver. 1.0 Session 11
  • 5. Data Structures and Algorithms Identifying 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. Data Structures and Algorithms Identifying 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. Data Structures and Algorithms Identifying 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. Data Structures and Algorithms Just 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) How can you of thethis problem? positions, you need To keep tracksolve number, you need to perform the to insert a request 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. Data Structures and Algorithms Implementing 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. 2. Increment REAR by 1. 3. Store the element at index position REAR in the array. Ver. 1.0 Session 11
  • 12. Data Structures and Algorithms Implementing 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. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 3 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 5 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 7 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 10 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. If the queue is empty: Request number generated 15 a. Set FRONT = 0. 2. Increment REAR by 1. 3. 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. 2. Increment FRONT by 1. Ver. 1.0 Session 11
  • 35. Data Structures and Algorithms Implementing 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 2. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. Retrieve the element at index FRONT. 2. Increment FRONT by 1. FRONT = 0 REAR = 4 310 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 37. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. Retrieve the element at index FRONT. 2. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. Retrieve the element at index FRONT. 2. Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 39. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. Retrieve the element at index FRONT. 2. Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 40. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) 1. Retrieve the element at index FRONT. 2. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) To implement an insert or delete queue, theyou need to As you delete elements from the operation, 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 decremented. in the beginning is discarded and never used again. 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 51. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 52. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 53. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 54. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
  • 55. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 56. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 57. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 58. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 59. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 60. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
  • 61. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 62. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 63. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Ver. 1.0 Session 11
  • 64. Data Structures and Algorithms Implementing 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 2. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 3. Increment REAR by 1 0 1 2 3 4 4. Queue[REAR] = element Before cannot be incremented operation, youqueueis always check for the Inserting an element in ainsert because the queue overflow. REAR implementing an full queue leads to should full. queue full condition. Ver. 1.0 Session 11
  • 65. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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 2. If queue is empty (If FRONT = –1): a. Set FRONT = 0 b. Set REAR = 0 c. Go to Step 5 1. If REAR is at the last index position: a. Set REAR = 0 b. Go to step 5 2. Increment REAR by 1 3. Queue[REAR] = element Ver. 1.0 Session 11
  • 67. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 69. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 70. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 71. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 72. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 73. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 3. Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 74. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 75. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 76. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 77. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 3. Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 78. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 79. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 80. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 20 3. Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 81. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 3. Increment FRONT by 1 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 82. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 3. Increment FRONT by 1 REAR = –1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 83. Data Structures and Algorithms Implementing 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 2. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 3. 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. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) The condition for queue empty an Modified algorithm for deleting is: 1. If the queue is empty: // If // FRONT = –1 element from a circular queue: FRONT = –1 a. Display “Queue underflow” b. Exit 2. If there is only one element in the queue: // If FRONT = REAR a. Set FRONT = –1 b. Set REAR = –1 c. Exit 3. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 3. Increment FRONT by 1 Ver. 1.0 Session 11
  • 85. Data Structures and Algorithms Just 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. Data Structures and Algorithms Activity: 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Implementing 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. Data Structures and Algorithms Inserting an Element in a Linked Queue Write an algorithm to implement insert operation in a linked queue. Ver. 1.0 Session 11
  • 90. Data Structures and Algorithms Inserting 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 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 91. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 92. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 93. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 94. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 95. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. If the queue is empty, execute the following FRONT = NULL steps: FRONT a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 96. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. REAR = NULL 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 97. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
  • 98. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 99. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 100. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 10 5 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 101. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 10 5 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 102. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 10 5 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 103. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 10 5 node. 6. Make REAR point to the new node. Ver. 1.0 Session 11
  • 104. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 2. Assign value to the data field of the new node. 3. Make the next field of the new node point to NULL. 4. 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 5. Make the next field of REAR point to the new 10 3 10 5 node. 6. Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
  • 105. Data Structures and Algorithms Deleting an Element from a Linked Queue Write an algorithm to implement the delete operation on a linked queue. Ver. 1.0 Session 11
  • 106. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed delete Algorithm to implement 1. If the queue is empty: // FRONT = NULL operation on a linked queue. a. Display “Queue empty” b. Exit 2. Mark the node marked FRONT as current 3. Make FRONT point to the next node in its sequence FRONT REAR 4. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
  • 107. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed 1. If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 2. Mark the node marked FRONT as current 3. Make FRONT point to the next node in its sequence FRONT REAR 4. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
  • 108. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed 1. If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 2. Mark the node marked FRONT as current 3. Make FRONT point to the next node in its sequence FRONT REAR 4. Release the memory for the node marked as current 310 5 7 10 current Ver. 1.0 Session 11
  • 109. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed 1. If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 2. Mark the node marked FRONT as current 3. Make FRONT point to the next node in its sequence FRONT FRONT REAR 4. Release the memory for the node marked as current 310 5 7 10 current Ver. 1.0 Session 11
  • 110. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed 1. If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 2. Mark the node marked FRONT as current 3. Make FRONT point to the next node in its sequence FRONT REAR 4. Release the memory for the node marked as current 3 5 7 10 current Delete operation complete Memory released Ver. 1.0 Session 11
  • 111. Data Structures and Algorithms Just a minute How does the implementation of a linked list differ from that of a linked queue? Answer: In a linked list, you can insert and delete elements anywhere in the list. However, in a linked queue, insertion and deletion takes place only from the ends. More specifically, insertion takes place at the rear end and deletion takes place at the front end of the queue. Ver. 1.0 Session 11
  • 112. Data Structures and Algorithms Applications of Queues Queues offer a lot of practical applications, such as: Printer Spooling CPU Scheduling Mail Service Keyboard Buffering Elevator Ver. 1.0 Session 11

Editor's Notes

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Ask the student to modify the algorithm to incorporate the queue overflow condition.
  6. Ask the student to modify the above algorithm to check for queue empty condition.
  7. 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.
  8. 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.
  9. Faculty should ask the students “Will this algorithm work if there is only one node in the list?”
  10. 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.
  11. 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.
  12. 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.