SlideShare ist ein Scribd-Unternehmen logo
1 von 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 a request this problem? positions, you need
                    To keep tracksolve number, you need to perform the to
                         insert you of the rear and front
                    following steps:
                    declare two solve this problem by implementing a queue.
                          You can integer variables, REAR and FRONT.
                    Let us implementvalue of REAR by FRONT are stores these
                          Increment the a queue using 1.
                    If the queue is empty, REAR andan array that set to –1.
                    request numbers in theindex position REAR in the array.
                          Insert the element at order of their arrival.



        FRONT = –1
        REAR = –1
                        0    1    2    3    4




     Ver. 1.0                                                         Session 11
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.

                3. Increment REAR by 1.

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

                                                   3.   Increment REAR by 1.

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



        FRONT = –1
                     10
        REAR = –1
                     0    1   2   3    4




     Ver. 1.0                                                                    Session 11
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.

                                           3.   Increment REAR by 1.

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



        FRONT = –1
                     10
        REAR = –1
                     0    1   2    3   4




     Ver. 1.0                                                            Session 11
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.

                                                 3.   Increment REAR by 1.

                                                 5.   Store the element at index position
                                                      REAR in the array.
                FRONT = 0



        FRONT = –1
                            10
        REAR = –1
                            0    1   2   3   4




     Ver. 1.0                                                                  Session 11
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.

                                                   •    Increment REAR by 1.

                                                   •    Store the element at index position
                                                        REAR in the array.
                FRONT = 0   REAR = 0



                            10
        REAR = –1
                            0    1     2   3   4




     Ver. 1.0                                                                    Session 11
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.

                                                           •    Increment REAR by 1.

                                                           •    Store the element at index position
                                                                REAR in the array.
                FRONT = 0    REAR = 0



                            310

                            0       1    2    3        4


                                  Insertion complete




     Ver. 1.0                                                                            Session 11
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.

                                                    3.   Increment REAR by 1.

                                                    5.   Store the element at index position
                                                         REAR in the array.
                FRONT = 0    REAR = 0



                            310

                            0     1     2   3   4




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


                                                    •    If the queue is empty:

        Request number generated            5             a.   Set FRONT = 0.

                                                    3.   Increment REAR by 1.

                                                    5.   Store the element at index position
                                                         REAR in the array.
                FRONT = 0    REAR = 0



                            310

                            0     1     2   3   4




     Ver. 1.0                                                                     Session 11
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.

                                                    •    Increment REAR by 1.

                                                    •    Store the element at index position
                                                         REAR in the array.
                FRONT = 0    REARREAR = 1
                                 =0



                            310

                            0     1     2   3   4




     Ver. 1.0                                                                     Session 11
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.

                                                         •    Increment REAR by 1.

                                                         •    Store the element at index position
                                                              REAR in the array.
                FRONT = 0         REAR = 1



                            310   5

                            0     1      2   3       4

                                Insertion complete




     Ver. 1.0                                                                          Session 11
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.

                                                     3.   Increment REAR by 1.

                                                     5.   Store the element at index position
                                                          REAR in the array.
                FRONT = 0         REAR = 1



                            310   5

                            0     1      2   3   4




     Ver. 1.0                                                                      Session 11
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.

                                                     3.   Increment REAR by 1.

                                                     5.   Store the element at index position
                                                          REAR in the array.
                FRONT = 0         REAR = 1



                            310   5

                            0     1      2   3   4




     Ver. 1.0                                                                      Session 11
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.

                                                            •    Increment REAR by 1.

                                                            •    Store the element at index position
                                                                 REAR in the array.
                FRONT = 0         REAR = REAR = 2
                                         1



                            310   5

                            0     1       2     3       4




     Ver. 1.0                                                                             Session 11
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.

                                                          •    Increment REAR by 1.

                                                          •    Store the element at index position
                                                               REAR in the array.
                FRONT = 0              REAR = 2



                            310   5     7

                            0     1     2     3       4

                                  Insertion complete




     Ver. 1.0                                                                           Session 11
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.

                                                          3.   Increment REAR by 1.

                                                          5.   Store the element at index position
                                                               REAR in the array.
                FRONT = 0             REAR = 2



                            310   5   7

                            0     1    2     3        4




     Ver. 1.0                                                                           Session 11
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.

                                                          3.   Increment REAR by 1.

                                                          5.   Store the element at index position
                                                               REAR in the array.
                FRONT = 0             REAR = 2



                            310   5   7

                            0     1    2     3        4




     Ver. 1.0                                                                           Session 11
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.

                                                            •    Increment REAR by 1.

                                                            •    Store the element at index position
                                                                 REAR in the array.
                FRONT = 0             REAR = REAR = 3
                                             2



                            310   5   7

                            0     1    2      3         4




     Ver. 1.0                                                                             Session 11
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.

                                                            •    Increment REAR by 1.

                                                            •    Store the element at index position
                                                                 REAR in the array.
                FRONT = 0                    REAR = 3



                            310     5    7    10

                            0       1    2    3         4

                                  Insertion complete




     Ver. 1.0                                                                             Session 11
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.

                                                         3.   Increment REAR by 1.

                                                         5.   Store the element at index position
                                                              REAR in the array.
                FRONT = 0                 REAR = 3



                            310   5   7    10

                            0     1   2    3         4




     Ver. 1.0                                                                          Session 11
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.

                                                         3.   Increment REAR by 1.

                                                         5.   Store the element at index position
                                                              REAR in the array.
                FRONT = 0                 REAR = 3



                            310   5   7    10

                            0     1   2    3         4




     Ver. 1.0                                                                          Session 11
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.

                                                           •    Increment REAR by 1.

                                                           •    Store the element at index position
                                                                REAR in the array.
                FRONT = 0                 REAR =REAR = 4
                                                 3



                            310   5   7    10

                            0     1   2    3      4




     Ver. 1.0                                                                            Session 11
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.

                                                               •    Increment REAR by 1.

                                                               •    Store the element at index position
                                                                    REAR in the array.
                FRONT = 0                           REAR = 4



                            310     5    7    10     15

                            0       1    2    3        4

                                  Insertion complete




     Ver. 1.0                                                                                Session 11
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.

                 3.   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
                                                        3.   Increment FRONT by 1.
                    get processed.

                FRONT = 0                    REAR = 4



                            3   5   7   10   15

                            0   1   2   3     4




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


                One request processed                     •   Retrieve the element at index
                                                              FRONT.

                                                          •   Increment FRONT by 1.




                FRONT = 0                      REAR = 4



                            310   5   7   10   15

                            0     1   2   3     4




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


                                                                •   Retrieve the element at index
                                                                    FRONT.

                                                                •   Increment FRONT by 1.




                FRONT = 0
                        FRONT = 1                 REAR = 4



                          10    5       7    10   15

                          0     1       2     3    4

                                    Delete operation complete




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


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

                                                        3.   Increment FRONT by 1.




                      FRONT = 1              REAR = 4



                        10    5   7     10   15

                        0     1   2     3     4




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


                One request processed                   •   Retrieve the element at index
                                                            FRONT.

                                                        •   Increment FRONT by 1.




                      FRONT = 1              REAR = 4



                        10    5   7     10   15

                        0     1   2     3     4




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


                                                     •   Retrieve the element at index
                                                         FRONT.

                                                     •   Increment FRONT by 1.




                FRONT = FRONT = 2
                        1                REAR = 4



                  10           7    10   15

                  0     1      2    3     4

                         Delete operation complete




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


                To you delete an insert or delete operation, you need to
                As implement elements from the queue, the queue moves
                incrementarray.
                down the the values of REAR or FRONT by one,
                respectively.
                The disadvantage of this approach is that the storage space
                However, these values are never never used again.
                in the beginning is discarded and decremented.
                Consider the following queue.
                           FRONT = 3   REAR = 4



                 10              10     15
                 0    1     2    3       4
                REAR is at the last index position.
                Therefore, you cannot insert elements in this queue, even
                though there is space for them.
                This means that all the initial vacant positions go waste.

     Ver. 1.0                                                        Session 11
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
                                                     •    If REAR is at the last index position:

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

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




     Ver. 1.0                                                                     Session 11
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
                                                     •    If REAR is at the last index position:

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

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




     Ver. 1.0                                                                     Session 11
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
                                                     3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                     Session 11
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
                                                         3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                         Session 11
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
                                                      3.   If REAR is at the last index position:

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

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

                     Insertion complete




     Ver. 1.0                                                                      Session 11
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
                                                      3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                      Session 11
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
                                                      3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                      Session 11
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
                                                      3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                      Session 11
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
                                                          3.   If REAR is at the last index position:

                                                                •     Set REAR = 0
                      10   20        23   10   15               •     Go to step 4

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




     Ver. 1.0                                                                          Session 11
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
                                                    3.   If REAR is at the last index position:

                                                          •     Set REAR = 0
                      10   20        23   10   15         •     Go to step 4

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




     Ver. 1.0                                                                    Session 11
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
                                                    3.   If REAR is at the last index position:

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

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

                    Insertion complete




     Ver. 1.0                                                                    Session 11
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
                                                    3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                    Session 11
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
                                                    3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                    Session 11
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
                                                    3.   If REAR is at the last index position:

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

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




     Ver. 1.0                                                                    Session 11
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
                                                            3.   If REAR is at the last index position:

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

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


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




     Ver. 1.0                                                                            Session 11
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

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

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

                                                      •    If REAR is at the last index position:

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

                                                      •    Increment REAR by 1

                                                      •    Queue[REAR] = element




     Ver. 1.0                                                                      Session 11
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   4.   If FRONT is at the last index
                                                         position:

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




     Ver. 1.0                                                                   Session 11
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   4.   If FRONT is at the last index
                                                             position:

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




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


                One request processed                   •    If there is only one element in the
                                                             queue:

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

                            REAR = 0        FRONT = 4   4.   If FRONT is at the last index
                                                             position:

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




     Ver. 1.0                                                                       Session 11
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   •    If FRONT is at the last index
                                                             position:

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




     Ver. 1.0                                                                       Session 11
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   4.   If FRONT is at the last index
                                                              position:

                                                               •     Set FRONT = 0
                                                               •     Exit
                       17
                        10    20                15
                                                         •    Increment FRONT by 1
                       0      1     2    3       4




     Ver. 1.0                                                                        Session 11
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             4.   If FRONT is at the last index
                                                        position:

                                                         •     Set FRONT = 0
                                                         •     Exit
                       17
                        10    20
                                                   •    Increment FRONT by 1
                       0      1     2    3     4

                           Deletion complete




     Ver. 1.0                                                                  Session 11
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           4.   If FRONT is at the last index
                                                      position:

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




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


                One request processed            •    If there is only one element in the
                                                      queue:

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

                  FRONT = 0   REAR = 0           4.   If FRONT is at the last index
                                                      position:

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




     Ver. 1.0                                                                Session 11
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           •    If FRONT is at the last index
                                                      position:

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




     Ver. 1.0                                                                Session 11
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              4.   If FRONT is at the last index
                                                          position:

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

                             Deletion complete




     Ver. 1.0                                                                    Session 11
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           4.   If FRONT is at the last index
                                                        position:

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




     Ver. 1.0                                                                  Session 11
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           4.   If FRONT is at the last index
                                                        position:

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




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


                One request processed              1.   If there is only one element in the
                                                        queue:

                                                         •     Set FRONT = –1
                                                         •     Set REAR = –1
                                                         •     Exit
                          FRONT = 0
                                REAR = 0           4.   If FRONT is at the last index
                                                        position:

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




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


                One request processed           1.   If there is only one element in the
                                                     queue:

                                                      •     Set FRONT = –1
                                                      •     Set REAR = –1
                                                      •     Exit

                            REAR = 0            4.   If FRONT is at the last index
                                                     position:

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




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


                One request processed           1.   If there is only one element in the
                                                     queue:

                                                      •     Set FRONT = –1
                                                      •     Set REAR = –1
                                                      •     Exit

                                                •    If FRONT is at the last index
                                                     position:

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


                           Deletion complete




     Ver. 1.0                                                               Session 11
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

                                                         4.   If FRONT is at the last index
                                                              position:

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


                   Therefore, before implementing a
                   delete operation, you first need to
                   check whether the queue is
                   empty or not.

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


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

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

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

                                                      7.   If FRONT is at the last index
                                                           position:

                                                            a.    Set FRONT = 0
                                                            b.    Exit

                                                      •    Increment FRONT by 1




     Ver. 1.0                                                                     Session 11
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          3.    Assign value to the data field of the new
                                                     node.

                                               5.    Make the next field of the new node point to
                                                     NULL.
       REAR = NULL
                                               7.    If the queue is empty, execute the following
       FRONT = NULL
                                                     steps:

                                                      a.   Make FRONT point to the new node
                                                      b.   Make REAR point to the new node
                                                      c.   Exit

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

                                               11.   Make REAR point to the new node.




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


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

                                       •     Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       •     If the queue is empty, execute the following
       FRONT = NULL
                                             steps:

                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

                                       •     Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       •     If the queue is empty, execute the following
       FRONT = NULL
                                             steps:

                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

                                       •     Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       •     If the queue is empty, execute the following
       FRONT = NULL
                                             steps:

                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

                                       •     Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       •     If the queue is empty, execute the following
       FRONT = NULL
                                             steps:

                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

                                       5.    Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       7.    If the queue is empty, execute the following
       FRONT = NULL
                                             steps:
   FRONT
                                              •    Make FRONT point to the new node
                                              •    Make REAR point to the new node
                                              •    Exit

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

                                       11.   Make REAR point to the new node.




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


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

                                       5.    Make the next field of the new node point to
                                             NULL.
       REAR = NULL
                                       7.    If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              •    Make FRONT point to the new node
                                              •    Make REAR point to the new node
                                              •    Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                             7.     If the queue is empty, execute the following
                                                    steps:
   FRONT REAR
                                                     •    Make FRONT point to the new node
                                                     •    Make REAR point to the new node
                                                     •    Exit

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

                                             11.    Make REAR point to the new node.

                        Insert operation complete




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


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

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

                                       7.    If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                       •     If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                       •     If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                       •     If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                       •     If the queue is empty, execute the following
                                             steps:
   FRONT REAR
                                              a.   Make FRONT point to the new node
                                              b.   Make REAR point to the new node
                                              c.   Exit

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

                                       11.   Make REAR point to the new node.




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


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

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

                                       7.   If the queue is empty, execute the following
                                            steps:
   FRONT REAR
                                             a.   Make FRONT point to the new node
                                             b.   Make REAR point to the new node
                                             c.   Exit

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

                                       •    Make REAR point to the new node.




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


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

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

                                               7.     If the queue is empty, execute the following
                                                      steps:
   FRONT REAR        REAR
                                                       a.   Make FRONT point to the new node
                                                       b.   Make REAR point to the new node
                                                       c.   Exit

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

                                               •      Make REAR point to the new node.

                          Insert operation complete




     Ver. 1.0                                                                        Session 11
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         •    If the queue is empty: // FRONT = NULL

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

                                                 3.   Mark the node marked FRONT as current

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

          310          5        7        10




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


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

                                                     a.   Display “Queue empty”
                                                     b.   Exit

                                               3.   Mark the node marked FRONT as current

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

          310          5       7          10




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


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

                                                    a.   Display “Queue empty”
                                                    b.   Exit

                                               •   Mark the node marked FRONT as current

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

          310          5       7          10


       current




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


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

                                                    a.   Display “Queue empty”
                                                    b.   Exit

                                               •   Mark the node marked FRONT as current

                                               •   Make FRONT point to the next node in its
                                                   sequence
     FRONT          FRONT               REAR
                                               •   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           •   If the queue is empty: // FRONT = NULL

                                                     a.   Display “Queue empty”
                                                     b.   Exit

                                                •   Mark the node marked FRONT as current

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

          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
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11
08 ds and algorithm session_11

Weitere ähnliche Inhalte

Was ist angesagt?

Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structuresKuber Chandra
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sortingheshekik
 
Week09
Week09Week09
Week09hccit
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Ameer B. Alaasam
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays ConceptsVicter Paul
 

Was ist angesagt? (19)

Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
Week09
Week09Week09
Week09
 
2 b queues
2 b queues2 b queues
2 b queues
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Binary tree
Binary treeBinary tree
Binary tree
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Tree
TreeTree
Tree
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
07slide
07slide07slide
07slide
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 

Andere mochten auch

01 ds and algorithm session_01
01 ds and algorithm session_0101 ds and algorithm session_01
01 ds and algorithm session_01Niit Care
 
Its agung-fstpt 11- safe riding campaign-revisi
Its agung-fstpt 11- safe riding campaign-revisiIts agung-fstpt 11- safe riding campaign-revisi
Its agung-fstpt 11- safe riding campaign-revisia_agung_kartika
 
Economics Honors Paper - Tu Nguyen - 2015
Economics Honors Paper - Tu Nguyen - 2015Economics Honors Paper - Tu Nguyen - 2015
Economics Honors Paper - Tu Nguyen - 2015Tu Nguyen
 
Emerging kerala 14-night-life zone-at-uec-veli-tvm
Emerging kerala   14-night-life zone-at-uec-veli-tvmEmerging kerala   14-night-life zone-at-uec-veli-tvm
Emerging kerala 14-night-life zone-at-uec-veli-tvmkeralawatchnews
 
Jurnal rekayasa 2_ft_3
Jurnal rekayasa 2_ft_3Jurnal rekayasa 2_ft_3
Jurnal rekayasa 2_ft_3Eddy Ibrahim
 
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"Pawan Kumar Dhoot support "Pradhan mantri awas yojana"
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"PawanKumarDhoot
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
WATER SPORTS
WATER SPORTSWATER SPORTS
WATER SPORTSNaaz Khan
 
penggunaan geosintetik untuk konstruksi jalan
penggunaan geosintetik untuk konstruksi jalanpenggunaan geosintetik untuk konstruksi jalan
penggunaan geosintetik untuk konstruksi jalanrobert tuba
 
UMPTN Fisika 2001 Rayon B Kode 150
UMPTN Fisika 2001 Rayon B Kode 150UMPTN Fisika 2001 Rayon B Kode 150
UMPTN Fisika 2001 Rayon B Kode 150SMA Negeri 9 KERINCI
 
4 Menggambar Grafik Fungsi Dengan Matlab
4 Menggambar Grafik Fungsi Dengan Matlab4 Menggambar Grafik Fungsi Dengan Matlab
4 Menggambar Grafik Fungsi Dengan MatlabSimon Patabang
 

Andere mochten auch (20)

01 ds and algorithm session_01
01 ds and algorithm session_0101 ds and algorithm session_01
01 ds and algorithm session_01
 
Ds 1
Ds 1Ds 1
Ds 1
 
Post 2015 paris c limate conference politics on the internet manuela hartwig
Post 2015 paris c limate conference politics on the internet  manuela hartwigPost 2015 paris c limate conference politics on the internet  manuela hartwig
Post 2015 paris c limate conference politics on the internet manuela hartwig
 
TrainingShowF
TrainingShowFTrainingShowF
TrainingShowF
 
Its agung-fstpt 11- safe riding campaign-revisi
Its agung-fstpt 11- safe riding campaign-revisiIts agung-fstpt 11- safe riding campaign-revisi
Its agung-fstpt 11- safe riding campaign-revisi
 
Rpp fluida statis
Rpp fluida statisRpp fluida statis
Rpp fluida statis
 
Economics Honors Paper - Tu Nguyen - 2015
Economics Honors Paper - Tu Nguyen - 2015Economics Honors Paper - Tu Nguyen - 2015
Economics Honors Paper - Tu Nguyen - 2015
 
Emerging kerala 14-night-life zone-at-uec-veli-tvm
Emerging kerala   14-night-life zone-at-uec-veli-tvmEmerging kerala   14-night-life zone-at-uec-veli-tvm
Emerging kerala 14-night-life zone-at-uec-veli-tvm
 
Jurnal rekayasa 2_ft_3
Jurnal rekayasa 2_ft_3Jurnal rekayasa 2_ft_3
Jurnal rekayasa 2_ft_3
 
8 клас
8 клас8 клас
8 клас
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"Pawan Kumar Dhoot support "Pradhan mantri awas yojana"
Pawan Kumar Dhoot support "Pradhan mantri awas yojana"
 
Details
DetailsDetails
Details
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Portfolio
PortfolioPortfolio
Portfolio
 
WATER SPORTS
WATER SPORTSWATER SPORTS
WATER SPORTS
 
penggunaan geosintetik untuk konstruksi jalan
penggunaan geosintetik untuk konstruksi jalanpenggunaan geosintetik untuk konstruksi jalan
penggunaan geosintetik untuk konstruksi jalan
 
UMPTN Fisika 1999 Rayon A Kode 53
UMPTN Fisika 1999 Rayon A Kode 53UMPTN Fisika 1999 Rayon A Kode 53
UMPTN Fisika 1999 Rayon A Kode 53
 
UMPTN Fisika 2001 Rayon B Kode 150
UMPTN Fisika 2001 Rayon B Kode 150UMPTN Fisika 2001 Rayon B Kode 150
UMPTN Fisika 2001 Rayon B Kode 150
 
4 Menggambar Grafik Fungsi Dengan Matlab
4 Menggambar Grafik Fungsi Dengan Matlab4 Menggambar Grafik Fungsi Dengan Matlab
4 Menggambar Grafik Fungsi Dengan Matlab
 

Ähnlich wie 08 ds and algorithm session_11

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
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structureMuhammad Ismail
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.pptSajalFayyaz
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Data-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docxData-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docxKedarkamal
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...trangiaphuc362003181
 
Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Umar Ali
 
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
 

Ähnlich wie 08 ds and algorithm session_11 (20)

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
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Datastructure
DatastructureDatastructure
Datastructure
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structure
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Lec3
Lec3Lec3
Lec3
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
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
 
Queue
QueueQueue
Queue
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Data-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docxData-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docx
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
 
Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Chapter11
Chapter11Chapter11
Chapter11
 

Mehr von Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Kürzlich hochgeladen

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Kürzlich hochgeladen (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

08 ds and algorithm session_11

  • 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 a request this problem? positions, you need To keep tracksolve number, you need to perform the to insert you of the rear and front following steps: declare two solve this problem by implementing a queue. You can integer variables, REAR and FRONT. Let us implementvalue of REAR by FRONT are stores these Increment the a queue using 1. If the queue is empty, REAR andan array that set to –1. request numbers in theindex position REAR in the array. Insert the element at order of their arrival. FRONT = –1 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 11. 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. 3. Increment REAR by 1. 5. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 13. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 14. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 FRONT = –1 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 15. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 0 10 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 16. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
  • 17. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Ver. 1.0 Session 11
  • 18. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) • If the queue is empty: Request number generated 5 a. Set FRONT = 0. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 0 310 0 1 2 3 4 Ver. 1.0 Session 11
  • 19. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REARREAR = 1 =0 310 0 1 2 3 4 Ver. 1.0 Session 11
  • 20. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
  • 21. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
  • 22. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
  • 23. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = REAR = 2 1 310 5 0 1 2 3 4 Ver. 1.0 Session 11
  • 24. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
  • 25. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
  • 26. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
  • 27. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = REAR = 3 2 310 5 7 0 1 2 3 4 Ver. 1.0 Session 11
  • 28. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
  • 29. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
  • 30. 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. 3. Increment REAR by 1. 5. Store the element at index position REAR in the array. FRONT = 0 REAR = 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
  • 31. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR =REAR = 4 3 310 5 7 10 0 1 2 3 4 Ver. 1.0 Session 11
  • 32. 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. • Increment REAR by 1. • Store the element at index position REAR in the array. FRONT = 0 REAR = 4 310 5 7 10 15 0 1 2 3 4 Insertion complete Ver. 1.0 Session 11
  • 33. 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. 3. 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 3. Increment FRONT by 1. get processed. FRONT = 0 REAR = 4 3 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 36. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 0 REAR = 4 310 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 37. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 0 FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Delete operation complete Ver. 1.0 Session 11
  • 38. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. Retrieve the element at index FRONT. 3. Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 39. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = 1 REAR = 4 10 5 7 10 15 0 1 2 3 4 Ver. 1.0 Session 11
  • 40. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) • Retrieve the element at index FRONT. • Increment FRONT by 1. FRONT = FRONT = 2 1 REAR = 4 10 7 10 15 0 1 2 3 4 Delete operation complete Ver. 1.0 Session 11
  • 41. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) To you delete an insert or delete operation, you need to As implement elements from the queue, the queue moves incrementarray. down the the values of REAR or FRONT by one, respectively. The disadvantage of this approach is that the storage space However, these values are never never used again. in the beginning is discarded and decremented. Consider the following queue. FRONT = 3 REAR = 4 10 10 15 0 1 2 3 4 REAR is at the last index position. Therefore, you cannot insert elements in this queue, even though there is space for them. This means that all the initial vacant positions go waste. Ver. 1.0 Session 11
  • 42. 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 • If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 51. 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 • If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 52. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 53. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 54. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
  • 55. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 56. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 57. 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 3. If REAR is at the last index position: a. Set REAR = 0 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 58. 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 3. If REAR is at the last index position: • Set REAR = 0 10 20 23 10 15 • Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 59. 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 3. If REAR is at the last index position: • Set REAR = 0 10 20 23 10 15 • Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 60. 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 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Insertion complete Ver. 1.0 Session 11
  • 61. 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 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 62. 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 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 63. 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 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Ver. 1.0 Session 11
  • 64. 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 3. If REAR is at the last index position: a. Set REAR = 0 17 10 20 23 10 15 b. Go to step 4 • Increment REAR by 1 0 1 2 3 4 • Queue[REAR] = element Before cannot be incremented operation, you should always check for the Inserting an element in ainsert because the queue is full. REAR implementing an full queue leads to queue overflow. queue full condition. Ver. 1.0 Session 11
  • 65. 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 3. If queue is empty (If FRONT = –1): a. Set FRONT = 0 b. Set REAR = 0 c. Go to Step 5 • If REAR is at the last index position: a. Set REAR = 0 b. Go to step 5 • Increment REAR by 1 • Queue[REAR] = element Ver. 1.0 Session 11
  • 67. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 69. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 70. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed • If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit REAR = 0 FRONT = 4 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 71. 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 • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 72. 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 4. If FRONT is at the last index position: • Set FRONT = 0 • Exit 17 10 20 15 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 73. 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 4. If FRONT is at the last index position: • Set FRONT = 0 • Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 74. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 75. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed • If there is only one element in the queue: a. Set FRONT = –1 b. Set REAR = –1 c. Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 76. 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 • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 77. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 17 10 20 • Increment FRONT by 1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 78. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 79. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 80. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit FRONT = 0 REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 20 • Increment FRONT by 1 0 1 2 3 4 Ver. 1.0 Session 11
  • 81. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit REAR = 0 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Ver. 1.0 Session 11
  • 82. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) One request processed 1. If there is only one element in the queue: • Set FRONT = –1 • Set REAR = –1 • Exit • If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Deletion complete Ver. 1.0 Session 11
  • 83. 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 4. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit FRONT = –1 10 • Increment FRONT by 1 REAR = –1 0 1 2 3 4 Therefore, before implementing a delete operation, you first need to check whether the queue is empty or not. Ver. 1.0 Session 11
  • 84. Data Structures and Algorithms Implementing a Queue Using an Array (Contd.) The condition for queue empty an Modified algorithm for deleting is: • If the queue is empty: // If // FRONT = –1 element from a circular queue: FRONT = –1 a. Display “Queue underflow” b. Exit • If there is only one element in the queue: // If FRONT = REAR a. Set FRONT = –1 b. Set REAR = –1 c. Exit 7. If FRONT is at the last index position: a. Set FRONT = 0 b. Exit • Increment FRONT by 1 Ver. 1.0 Session 11
  • 85. 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 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 91. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 92. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 93. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 94. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 3 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. REAR = NULL • If the queue is empty, execute the following FRONT = NULL steps: a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 95. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following FRONT = NULL steps: FRONT • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 96. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. REAR = NULL 7. If the queue is empty, execute the following steps: FRONT REAR • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 97. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 3 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR • Make FRONT point to the new node • Make REAR point to the new node • Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
  • 98. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 99. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 100. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 101. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 102. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) • Allocate memory for the new node. Request number generated 5 • Assign value to the data field of the new node. • Make the next field of the new node point to NULL. • If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit 9. Make the next field of REAR point to the new 10 3 10 5 node. 11. Make REAR point to the new node. Ver. 1.0 Session 11
  • 103. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit • Make the next field of REAR point to the new 10 3 10 5 node. • Make REAR point to the new node. Ver. 1.0 Session 11
  • 104. Data Structures and Algorithms Inserting an Element in a Linked Queue (Contd.) 1. Allocate memory for the new node. Request number generated 5 3. Assign value to the data field of the new node. 5. Make the next field of the new node point to NULL. 7. If the queue is empty, execute the following steps: FRONT REAR REAR a. Make FRONT point to the new node b. Make REAR point to the new node c. Exit • Make the next field of REAR point to the new 10 3 10 5 node. • Make REAR point to the new node. Insert operation complete Ver. 1.0 Session 11
  • 105. 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 • If the queue is empty: // FRONT = NULL operation on a linked queue. a. Display “Queue empty” b. Exit 3. Mark the node marked FRONT as current 5. Make FRONT point to the next node in its sequence FRONT REAR 7. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
  • 107. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit 3. Mark the node marked FRONT as current 5. Make FRONT point to the next node in its sequence FRONT REAR 7. Release the memory for the node marked as current 310 5 7 10 Ver. 1.0 Session 11
  • 108. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit • Mark the node marked FRONT as current • Make FRONT point to the next node in its sequence FRONT REAR • Release the memory for the node marked as current 310 5 7 10 current Ver. 1.0 Session 11
  • 109. Data Structures and Algorithms Deleting an Element from a Linked Queue (Contd.) One request processed • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit • Mark the node marked FRONT as current • Make FRONT point to the next node in its sequence FRONT FRONT REAR • 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 • If the queue is empty: // FRONT = NULL a. Display “Queue empty” b. Exit • Mark the node marked FRONT as current • Make FRONT point to the next node in its sequence FRONT REAR • Release the memory for the node marked as current 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

Hinweis der Redaktion

  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.