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


                In this session, you will learn to:
                    Identify the features of a stack
                    Implement stacks
                    Apply stacks to solve programming problems




     Ver. 1.0                                                    Session 10
Data Structures and Algorithms
Stacks


                • Let us play the game of Rummy.


                      7               7
                          7               7
                              6               6
                                  6               6
                                      6               6
                                          6               6

                      7               7
                          7               7
                              6               6
                                  6               6
                                      6               6
                                          6               6




     Ver. 1.0                                                 Session 10
Data Structures and Algorithms
Defining a Stack


                A stack is Stack ?
                What is a a collection of data items that can be accessed at
                only one end, called top.
                Items can be inserted and deleted in a stack only at the top.
                The last item inserted in a stack is the first one to be
                deleted.
                Therefore, a stack is called a Last-In-First-Out (LIFO) data
                structure.




     Ver. 1.0                                                         Session 10
Data Structures and Algorithms
Identifying the Operations on Stacks


                •   PUSH: It two process of inserting are performed
                    There areis thebasic operations that a new element on the
                    top of a
                    stacks: stack.
                       PUSH
                       POP
                                                Push an
                                                Element 1
                                             Empty Stack




                                                    1




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Identifying the Operations on Stacks (Contd.)


                •   PUSH: It is the process of inserting a new element on the
                    top of a stack.


                                               Push an
                                               Push an
                                               Element 2
                                               Element 3




                                                   3
                                                   2
                                                   1




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Identifying the Operations on Stacks (Contd.)


                •   POP: It is the process of deleting an element from the top of
                    a stack.


                                                POP an
                                                Element


                                                              3
                                                    3         2
                                                    2
                                                    1




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


                Elements in stacks are inserted and deleted on a
                ___________ basis.




                Answer:
                   LIFO




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


                List down some real life examples that work on the LIFO
                principle.
                Answer:
                   Pile of books: Suppose a set of books are placed one over
                   the other in a pile. When you remove books from the pile, the
                   topmost book will be removed first. Similarly, when you have
                   to add a book to the pile, the book will be placed at the top of
                   the pile.
                   Pile of plates: The first plate begins the pile. The second plate
                   is placed on the top of the first plate and the third plate is
                   placed on the top of the second plate, and so on. In general, if
                   you want to add a plate to the pile, you can keep it on the top
                   of the pile. Similarly, if you want to remove a plate, you can
                   remove the plate from the top of the pile.
                   Bangles in a hand: When a person wears bangles, the last
                   bangle worn is the first one to be removed.
     Ver. 1.0                                                               Session 10
Data Structures and Algorithms
Implementing Stacks


               You need to develop a method to check if the parentheses
               in an arithmetic expression are correctly nested.
               How will you solve this problem?
               You can solve this problem easily by using a stack.




    Ver. 1.0                                                      Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                                  {(a + b) × (c + d) + (c × d)]}
                Consider an example.
                Suppose the expression is:
                {(a + b) × (c + d) + (c × d)]}
                Scan the expression from
                left to right.
                The first entry to be
                scanned is ‘{’, which is a left
                parenthesis.
                Push it into the stack.
                                                          {




     Ver. 1.0                                                                 Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                                  {(a + b) × (c + d) + (c × d)]}
                The next entry to be
                scanned is ‘(’, which is a left
                parenthesis.
                Push it into the stack.
                The next entry is ‘a’, which
                is an operand. Therefore, it
                is discarded.
                The next entry is ‘+’, which
                is an operator. Therefore, it
                is discarded.                             (
                                                          {
                The next entry is ‘b’, which
                is an operand. Therefore, it
                is discarded.


     Ver. 1.0                                                                 Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                             {(a + b) × (c + d) + (c × d)]}
                The next entry to be
                scanned is ‘)’, which is a
                right parenthesis
                POP the topmost entry from
                the stack.
                Match the two brackets.
                                                                 (        )


                                                              Brackets Matched

                                                     (
                                                     {




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                The next entry to be scanned
                                              {(a + b) × (c + d) + (c × d)]}
                is ‘×’, which is an operator.
                Therefore, it is discarded.
                The next entry to be scanned
                is ‘(’, which is a left
                parenthesis
                Push it into the stack
                The next entry to be scanned
                is ‘c’, which is an operand.
                Therefore it is discarded
                                                      (
                The next entry to be scanned
                                                      {
                is ‘+’, which is an operator.
                Therefore it is discarded
                The next entry to be scanned
                is ‘d’, which is an operand.
                Therefore it is discarded
     Ver. 1.0                                                             Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                             {(a + b) × (c + d) + (c × d)]}
                The next entry to be
                scanned is ‘)’, which is a
                right parenthesis.
                POP the topmost element
                from the stack.
                Match the two brackets.
                                                                 (        )


                                                              Brackets Matched

                                                     (
                                                     {




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                The next entry to be scanned
                                              {(a + b) × (c + d) + (c × d)]}
                is ‘+’, which is an operator.
                Therefore, it is discarded.
                The next entry to be scanned
                is ‘(’, which is a left
                parenthesis.
                Push it into the stack.
                The next entry to be scanned
                is ‘c’, which is an operand.
                Therefore, it is discarded.
                                                      (
                The next entry to be scanned
                                                      {
                is ‘×’, which is an operator.
                Therefore, it is discarded.
                The next entry to be scanned
                is ‘d’, which is an operand.
                Therefore, it is discarded.
     Ver. 1.0                                                             Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                             {(a + b) × (c + d) + (c × d)]}
                The next entry to be
                scanned is ‘)’, which is a
                right parenthesis.
                POP the topmost element
                from the stack.
                Match the two brackets.
                                                                 (        )


                                                              Brackets Matched

                                                     (
                                                     {




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)

                                             {(a + b) × (c + d) + (c × d)]}
                The next entry to be
                scanned is ‘]’, which is a
                right parenthesis.
                POP the topmost element
                from the stack.
                Match the two brackets.
                                                                 {        ]


                                                             Brackets Do Not Match
                The Expression is INVALID

                                                     {




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Implementing a Stack Using an Array


                A stack is similarstack list in which insertion and deletion is
                To implement a to a using an array:
                allowed only atarray: end.
                 – Declare an one
                Therefore,Stack[5];a // Maximum size needs to be specified in
                     int similar to list, stack can be implemented using
                both arrays and linked lists. // advance
                 –   Declare a variable, top to hold the index of the topmost
                     element in the stacks:
                       int top;
                 –   Initially, when the stack is empty, set:
                     top = –1




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


                Let us now write an algorithm for   1.   Increment top by 1.
                the PUSH operation.                 3.   Store the value to be
                                                         pushed at index top in
                    Initially:
                                                         the array. Top now
                    top = – 1                            contains the index of the
                                                         topmost element.

                    PUSH an element 3




                0      1         2   3   4
     Stack




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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                    top = – 1                      contains the index of the
                                                   topmost element.

                    PUSH an element 3




                0        1      2   3   4
     Stack          10




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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                      top = 0                      contains the index of the
                                                   topmost element.

                      PUSH an element 3




                  0        1    2   3     4
     Stack            10

                top = 0



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


                                                 •   Increment top by 1.

                                                 •   Store the value to be
                                                     pushed at index top in
                                                     the array. Top now
                                                     contains the index of the
                                                     topmost element.

                      PUSH an element 3




                  0       1   2   3       4
     Stack        3 10                        Item pushed


                top = 0



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


                                               1.   Increment top by 1.

                                               3.   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                      PUSH an element 8




                  0       1   2   3       4
     Stack        3 10


                top = 0



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                      PUSH an element 8




                  0      1    2   3       4
     Stack        3 10


                top = 0 = 1
                      top



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                    PUSH an element 8




                0     1       2   3     4
     Stack      3 10 8                      Item pushed


                    top = 1



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


                                               1.   Increment top by 1.

                                               3.   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                    PUSH an element 5




                0     1       2   3     4
     Stack      3 10 8


                    top = 1



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                    PUSH an element 5




                0     1     2       3   4
     Stack      3 10 8


                    top = top = 2
                           1



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                    PUSH an element 5




                0     1     2       3   4
     Stack      3 10 8      5               Item pushed


                          top = 2



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


                                               1.   Increment top by 1.

                                               3.   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                    PUSH an element 1




                0     1     2       3   4
     Stack      3 10 8      5


                          top = 2



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                    PUSH an element 1




                0     1     2   3       4
     Stack      3 10 8      5


                          top = 2 = 3
                                top



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


                                                •   Increment top by 1.

                                                •   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                    PUSH an element 1




                0     1    2     3       4
     Stack      3 10 8     5     1           Item pushed


                               top = 3



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


                                               1.   Increment top by 1.

                                               3.   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                    PUSH an element 9




                0     1    2     3       4
     Stack      3 10 8     5     1


                               top = 3



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


                                               •   Increment top by 1.

                                               •   Store the value to be
                                                   pushed at index top in
                                                   the array. Top now
                                                   contains the index of the
                                                   topmost element.

                    PUSH an element 9




                0     1    2    3       4
     Stack      3 10 8     5     1


                               top = 3 = 4
                                     top



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


                                                 •   Increment top by 1.

                                                 •   Store the value to be
                                                     pushed at index top in
                                                     the array. Top now
                                                     contains the index of the
                                                     topmost element.

                    PUSH an element 9




                0     1    2    3       4
     Stack      3 10 8     5    1       9     Item pushed


                                    top = 4



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


                                               1.   Increment top by 1.

                                               3.   Store the value to be
                                                    pushed at index top in
                                                    the array. Top now
                                                    contains the index of the
                                                    topmost element.

                    PUSH an element 2




                0     1    2    3       4
     Stack      3 10 8     5    1       9


                                    top = 4



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


                                                     •   Increment top by 1.

                                                     •   Store the value to be
                                                         pushed at index top in
                                                         the array. Top now
                                                         contains the index of the
                                                         topmost element.

                    PUSH an element 2




                0     1    2    3       4
     Stack      3 10 8     5    1       9


                                    top = 4top = 5




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


                                                         •   Increment top by 1.

                                                         •   Store the value to be
                                                             pushed at index top in
                                                             the array. Top now
                                                             contains the index of the
                                                             topmost element.

                    PUSH an element 2




                0     1    2    3       4
     Stack      3 10 8     5    1       9             Stack overflow


                                            top = 5




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


                The stack has been implemented
                To avoid the stack overflow, you       1.   Increment top–by 1.
                                                             If top = MAX 1:
                                                               a. Display “Stack
                need to checksizethe stack full
                in an array of for 5.                  3.   Store the value to be
                                                                    Full”
                condition before pushing an element
                Therefore, you cannot store more            pushed at index top in
                                                               b. Exit
                                                            the array. Top now
                into theelements in the stack.
                than 5 stack.                          3.   contains the index1of the
                                                             Increment top by
                Let us modify the algorithm to check        topmost element.
                                                       5.    Store the value to be
                for this condition.                          pushed at index top in
                                                             the array




                0   1    2   3    4
     Stack      3 10 8   5   1    9




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


                Write an algorithm to implement the POP operation on a
                stack.
                Algorithm for POP operation:
                1. If top = – 1:
                    a. Display “Stack Empty”
                    b. Exit
                4. Retrieve the value stored at index top
                5. Decrement top by 1




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


                In a stack, data can be stored and removed only from one
                end of the stack called the ______ of the stack.




                Answer:
                   top




     Ver. 1.0                                                      Session 10
Data Structures and Algorithms
Implementing a Stack Using a Linked List


                Write an algorithm to implement theas follows:
                The algorithm for PUSH operation isPUSH and POP
                                   POP operation is as follows:
                operations using a Linkedtmp point to the topmost node.
                    Make a memory for the List.
                 1. Allocatevariable/pointer new node.
                 2. Assign valuevalue contained in the topmost node.
                    Retrieve the to the data field of the new node.
                 3. Make the next field of nextnew node point to top.
                          top point to the the node in sequence.
                 4. Make topmemory allocatednode. node marked by tmp.
                    Release point to the new to the




     Ver. 1.0                                                             Session 10
Data Structures and Algorithms
Activity: Implementing a Stack Using an Array


                Problem Statement:
                   Write a program to implement a stack by using an array that
                   can store five elements.




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Activity: Implementing a Stack Using a Linked List


                Problem Statement:
                   Write a program to implement a stack by using a linked list.




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


                What will be the condition for stack full in a stack
                implemented as a linked list?




                Answer:
                    When a stack is implemented as a linked list, there is no upper
                    bound limit on the size of the stack. Therefore, there will be no
                    stack full condition in this case.




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


                If a stack is represented in memory by using a linked list,
                then insertion and deletion of data will be done ________.
                1.   At the end of the list
                2.   At the beginning of the list
                3.   Anywhere in the list
                4.   At the beginning and at the end of the list respectively




                Answer:
                2. At the beginning of the list




     Ver. 1.0                                                                   Session 10
Data Structures and Algorithms
Applications of Stacks


                Some of the applications of stacks are:
                    Implementing function calls
                    Maintaining the UNDO list for an application
                    Checking the nesting of parentheses in an expression
                    Evaluating expressions




     Ver. 1.0                                                              Session 10
Data Structures and Algorithms
Implementing Function Calls


                Implementing function calls:
                    Consider an example. There are three functions, F1, F2, and
                    F3. Function F1 invokes F2 and function F2 invokes F3, as
                    shown.




     Ver. 1.0                                                            Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()
                                           Assuming these instructions at the
                     {
                                           given locations in the memory.
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                                        Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()
                     {                     The execution starts from
                1100         int x;        function F1
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                                 Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                              Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()            x=5
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                    Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                   x=5
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);                 1103, x = 5
                1122          print(x);
                     }                     Address and the local variable of F1
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                                        Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()            x = 10
                                               5
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);         1103, x = 5
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                              Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                        x = 10
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120                                      1122, x = 10
                              x = x + 5;
                1121          F3(x);                       1103, x = 5
                1122          print(x);
                     }                   Address and the local variable of F2
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                                               Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                        x = 20
                                                            10
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120                                      1122, x = 10
                              x = x + 5;
                1121          F3(x);                       1103, x = 5
                1122          print(x);
                     }                   Address and the local variable of F2
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;
                     }
     Ver. 1.0                                                               Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                 x = 20
                                                    10
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)                              1122, x = 10
                     {
                1120                              1122, x = 10
                              x = x + 5;
                1121          F3(x);              1103, x = 5
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;     20
                     }
     Ver. 1.0                                                        Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                      x=5
                                                       10
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)                                 1103, x = 5
                     {
                1120          x = x + 5;
                1121          F3(x);                  1103, x = 5
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;     20   10
                     }
     Ver. 1.0                                                          Session 10
Data Structures and Algorithms
Implementing Function Calls (Contd.)
                      void F1()                      x=5
                     {
                1100         int x;
                1101          x = 5;
                1102          F2();
                1103          print(x);
                     }
                     void F2(int x)
                     {
                1120          x = x + 5;
                1121          F3(x);
                1122          print(x);
                     }
                     void F3(int x)
                     {
                1140          x = x × 2;
                1141          print x;     20   10    5
                     }
     Ver. 1.0                                              Session 10
Data Structures and Algorithms
Maintaining the UNDO list for an Application


                Maintaining the UNDO list for an application:
                    Consider that you made some changes in a Word document.
                    Now, you want to revert back those changes. You can revert
                    those changes with the help of an UNDO feature.
                    The UNDO feature reverts the changes in a LIFO manner.
                    This means that the change that was made last is the first one
                    to be reverted.
                    You can implement the UNDO list by using a stack.




     Ver. 1.0                                                              Session 10
Data Structures and Algorithms
Checking the Nesting of Parentheses in an Expression


                Checking the nesting of parentheses in an expression:
                   You can do this by checking the following two conditions:
                    – The number of left parenthesis should be equal to the number of
                      right parenthesis.
                    – Each right parenthesis is preceded by a matching left
                      parenthesis.




     Ver. 1.0                                                                 Session 10
Data Structures and Algorithms
Evaluating Expressions


                Evaluating an expression by using stacks:
                   Stacks can be used to solve complex arithmetic expressions.
                   The evaluation of an expression is done in two steps:
                       Conversion of the infix expression into a postfix expression.
                       Evaluation of the postfix expression.




     Ver. 1.0                                                                      Session 10
Data Structures and Algorithms
Activity: Implementing a Stack using a Linked List


                Problem Statement:
                   Write a program that accepts an infix expression, and then
                   converts it into a postfix expression. You can assume that the
                   entered expression is a valid infix expression.




     Ver. 1.0                                                             Session 10
Data Structures and Algorithms
Summary


               In this session, you learned that:
                  A stack is a collection of data items that can be accessed at
                  only one end, called top. The last item inserted in a stack is the
                  first one to be deleted.
                  A stack is called a LIFO data structure.
                  There are two operations that can be performed on stacks.
                  They are:
                    – PUSH
                    – POP
                – Stacks can be implemented by using both arrays and linked
                  lists.




    Ver. 1.0                                                                Session 10
Data Structures and Algorithms
Summary (Contd.)


               Stacks are used in many applications. Some of the application
               domains of stacks are as follows:
                   Implementing function calls
                   Maintaining the UNDO list for an application
                   Checking the nesting of parentheses in an expression
                   Evaluating expressions




    Ver. 1.0                                                              Session 10

More Related Content

What's hot

Week09
Week09Week09
Week09hccit
 
Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Umar Ali
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listAndres Mendez-Vazquez
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueFarhanum Aziera
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select TopicsJay Coskey
 
Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introductionAndres Mendez-Vazquez
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays ConceptsVicter Paul
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listAndres Mendez-Vazquez
 
Optimizing array-based data structures to the limit
Optimizing array-based data structures to the limitOptimizing array-based data structures to the limit
Optimizing array-based data structures to the limitRoman Leventov
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2Prerna Sharma
 
Top 50 oracle interview questions and answers
Top 50 oracle interview questions and answersTop 50 oracle interview questions and answers
Top 50 oracle interview questions and answersNIKUL GOYAL
 

What's hot (20)

Week09
Week09Week09
Week09
 
Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Tree
TreeTree
Tree
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
Preparation Data Structures 01 introduction
Preparation Data Structures 01 introductionPreparation Data Structures 01 introduction
Preparation Data Structures 01 introduction
 
Binary tree
Binary treeBinary tree
Binary tree
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_list
 
Optimizing array-based data structures to the limit
Optimizing array-based data structures to the limitOptimizing array-based data structures to the limit
Optimizing array-based data structures to the limit
 
Tree & bst
Tree & bstTree & bst
Tree & bst
 
Class Diagram Uml
Class Diagram UmlClass Diagram Uml
Class Diagram Uml
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Top 50 oracle interview questions and answers
Top 50 oracle interview questions and answersTop 50 oracle interview questions and answers
Top 50 oracle interview questions and answers
 

Viewers also liked (19)

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Top10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing WorldTop10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing World
 
Data Structure
Data StructureData Structure
Data Structure
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar to 07 ds and algorithm session_10

My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdfhafsa40
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-dequesRishabh Jindal
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxHusnainNaqvi2
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.pptsoniya555961
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 

Similar to 07 ds and algorithm session_10 (20)

My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdf
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Queue
QueueQueue
Queue
 
Data structures
Data structuresData structures
Data structures
 
stack coding.pptx
stack coding.pptxstack coding.pptx
stack coding.pptx
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 

More from 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
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
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
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"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
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
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
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
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
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
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!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
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
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"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...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
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)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
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!
 

07 ds and algorithm session_10

  • 1. Data Structures and Algorithms Objectives In this session, you will learn to: Identify the features of a stack Implement stacks Apply stacks to solve programming problems Ver. 1.0 Session 10
  • 2. Data Structures and Algorithms Stacks • Let us play the game of Rummy. 7 7 7 7 6 6 6 6 6 6 6 6 7 7 7 7 6 6 6 6 6 6 6 6 Ver. 1.0 Session 10
  • 3. Data Structures and Algorithms Defining a Stack A stack is Stack ? What is a a collection of data items that can be accessed at only one end, called top. Items can be inserted and deleted in a stack only at the top. The last item inserted in a stack is the first one to be deleted. Therefore, a stack is called a Last-In-First-Out (LIFO) data structure. Ver. 1.0 Session 10
  • 4. Data Structures and Algorithms Identifying the Operations on Stacks • PUSH: It two process of inserting are performed There areis thebasic operations that a new element on the top of a stacks: stack. PUSH POP Push an Element 1 Empty Stack 1 Ver. 1.0 Session 10
  • 5. Data Structures and Algorithms Identifying the Operations on Stacks (Contd.) • PUSH: It is the process of inserting a new element on the top of a stack. Push an Push an Element 2 Element 3 3 2 1 Ver. 1.0 Session 10
  • 6. Data Structures and Algorithms Identifying the Operations on Stacks (Contd.) • POP: It is the process of deleting an element from the top of a stack. POP an Element 3 3 2 2 1 Ver. 1.0 Session 10
  • 7. Data Structures and Algorithms Just a minute Elements in stacks are inserted and deleted on a ___________ basis. Answer: LIFO Ver. 1.0 Session 10
  • 8. Data Structures and Algorithms Just a minute List down some real life examples that work on the LIFO principle. Answer: Pile of books: Suppose a set of books are placed one over the other in a pile. When you remove books from the pile, the topmost book will be removed first. Similarly, when you have to add a book to the pile, the book will be placed at the top of the pile. Pile of plates: The first plate begins the pile. The second plate is placed on the top of the first plate and the third plate is placed on the top of the second plate, and so on. In general, if you want to add a plate to the pile, you can keep it on the top of the pile. Similarly, if you want to remove a plate, you can remove the plate from the top of the pile. Bangles in a hand: When a person wears bangles, the last bangle worn is the first one to be removed. Ver. 1.0 Session 10
  • 9. Data Structures and Algorithms Implementing Stacks You need to develop a method to check if the parentheses in an arithmetic expression are correctly nested. How will you solve this problem? You can solve this problem easily by using a stack. Ver. 1.0 Session 10
  • 10. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} Consider an example. Suppose the expression is: {(a + b) × (c + d) + (c × d)]} Scan the expression from left to right. The first entry to be scanned is ‘{’, which is a left parenthesis. Push it into the stack. { Ver. 1.0 Session 10
  • 11. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} The next entry to be scanned is ‘(’, which is a left parenthesis. Push it into the stack. The next entry is ‘a’, which is an operand. Therefore, it is discarded. The next entry is ‘+’, which is an operator. Therefore, it is discarded. ( { The next entry is ‘b’, which is an operand. Therefore, it is discarded. Ver. 1.0 Session 10
  • 12. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} The next entry to be scanned is ‘)’, which is a right parenthesis POP the topmost entry from the stack. Match the two brackets. ( ) Brackets Matched ( { Ver. 1.0 Session 10
  • 13. Data Structures and Algorithms Implementing Stacks (Contd.) The next entry to be scanned {(a + b) × (c + d) + (c × d)]} is ‘×’, which is an operator. Therefore, it is discarded. The next entry to be scanned is ‘(’, which is a left parenthesis Push it into the stack The next entry to be scanned is ‘c’, which is an operand. Therefore it is discarded ( The next entry to be scanned { is ‘+’, which is an operator. Therefore it is discarded The next entry to be scanned is ‘d’, which is an operand. Therefore it is discarded Ver. 1.0 Session 10
  • 14. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} The next entry to be scanned is ‘)’, which is a right parenthesis. POP the topmost element from the stack. Match the two brackets. ( ) Brackets Matched ( { Ver. 1.0 Session 10
  • 15. Data Structures and Algorithms Implementing Stacks (Contd.) The next entry to be scanned {(a + b) × (c + d) + (c × d)]} is ‘+’, which is an operator. Therefore, it is discarded. The next entry to be scanned is ‘(’, which is a left parenthesis. Push it into the stack. The next entry to be scanned is ‘c’, which is an operand. Therefore, it is discarded. ( The next entry to be scanned { is ‘×’, which is an operator. Therefore, it is discarded. The next entry to be scanned is ‘d’, which is an operand. Therefore, it is discarded. Ver. 1.0 Session 10
  • 16. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} The next entry to be scanned is ‘)’, which is a right parenthesis. POP the topmost element from the stack. Match the two brackets. ( ) Brackets Matched ( { Ver. 1.0 Session 10
  • 17. Data Structures and Algorithms Implementing Stacks (Contd.) {(a + b) × (c + d) + (c × d)]} The next entry to be scanned is ‘]’, which is a right parenthesis. POP the topmost element from the stack. Match the two brackets. { ] Brackets Do Not Match The Expression is INVALID { Ver. 1.0 Session 10
  • 18. Data Structures and Algorithms Implementing a Stack Using an Array A stack is similarstack list in which insertion and deletion is To implement a to a using an array: allowed only atarray: end. – Declare an one Therefore,Stack[5];a // Maximum size needs to be specified in int similar to list, stack can be implemented using both arrays and linked lists. // advance – Declare a variable, top to hold the index of the topmost element in the stacks: int top; – Initially, when the stack is empty, set: top = –1 Ver. 1.0 Session 10
  • 19. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) Let us now write an algorithm for 1. Increment top by 1. the PUSH operation. 3. Store the value to be pushed at index top in Initially: the array. Top now top = – 1 contains the index of the topmost element. PUSH an element 3 0 1 2 3 4 Stack Ver. 1.0 Session 10
  • 20. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now top = – 1 contains the index of the topmost element. PUSH an element 3 0 1 2 3 4 Stack 10 Ver. 1.0 Session 10
  • 21. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now top = 0 contains the index of the topmost element. PUSH an element 3 0 1 2 3 4 Stack 10 top = 0 Ver. 1.0 Session 10
  • 22. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 3 0 1 2 3 4 Stack 3 10 Item pushed top = 0 Ver. 1.0 Session 10
  • 23. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) 1. Increment top by 1. 3. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 8 0 1 2 3 4 Stack 3 10 top = 0 Ver. 1.0 Session 10
  • 24. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 8 0 1 2 3 4 Stack 3 10 top = 0 = 1 top Ver. 1.0 Session 10
  • 25. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 8 0 1 2 3 4 Stack 3 10 8 Item pushed top = 1 Ver. 1.0 Session 10
  • 26. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) 1. Increment top by 1. 3. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 5 0 1 2 3 4 Stack 3 10 8 top = 1 Ver. 1.0 Session 10
  • 27. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 5 0 1 2 3 4 Stack 3 10 8 top = top = 2 1 Ver. 1.0 Session 10
  • 28. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 5 0 1 2 3 4 Stack 3 10 8 5 Item pushed top = 2 Ver. 1.0 Session 10
  • 29. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) 1. Increment top by 1. 3. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 1 0 1 2 3 4 Stack 3 10 8 5 top = 2 Ver. 1.0 Session 10
  • 30. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 1 0 1 2 3 4 Stack 3 10 8 5 top = 2 = 3 top Ver. 1.0 Session 10
  • 31. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 1 0 1 2 3 4 Stack 3 10 8 5 1 Item pushed top = 3 Ver. 1.0 Session 10
  • 32. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) 1. Increment top by 1. 3. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 9 0 1 2 3 4 Stack 3 10 8 5 1 top = 3 Ver. 1.0 Session 10
  • 33. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 9 0 1 2 3 4 Stack 3 10 8 5 1 top = 3 = 4 top Ver. 1.0 Session 10
  • 34. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 9 0 1 2 3 4 Stack 3 10 8 5 1 9 Item pushed top = 4 Ver. 1.0 Session 10
  • 35. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) 1. Increment top by 1. 3. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 2 0 1 2 3 4 Stack 3 10 8 5 1 9 top = 4 Ver. 1.0 Session 10
  • 36. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 2 0 1 2 3 4 Stack 3 10 8 5 1 9 top = 4top = 5 Ver. 1.0 Session 10
  • 37. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) • Increment top by 1. • Store the value to be pushed at index top in the array. Top now contains the index of the topmost element. PUSH an element 2 0 1 2 3 4 Stack 3 10 8 5 1 9 Stack overflow top = 5 Ver. 1.0 Session 10
  • 38. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) The stack has been implemented To avoid the stack overflow, you 1. Increment top–by 1. If top = MAX 1: a. Display “Stack need to checksizethe stack full in an array of for 5. 3. Store the value to be Full” condition before pushing an element Therefore, you cannot store more pushed at index top in b. Exit the array. Top now into theelements in the stack. than 5 stack. 3. contains the index1of the Increment top by Let us modify the algorithm to check topmost element. 5. Store the value to be for this condition. pushed at index top in the array 0 1 2 3 4 Stack 3 10 8 5 1 9 Ver. 1.0 Session 10
  • 39. Data Structures and Algorithms Implementing a Stack Using an Array (Contd.) Write an algorithm to implement the POP operation on a stack. Algorithm for POP operation: 1. If top = – 1: a. Display “Stack Empty” b. Exit 4. Retrieve the value stored at index top 5. Decrement top by 1 Ver. 1.0 Session 10
  • 40. Data Structures and Algorithms Just a minute In a stack, data can be stored and removed only from one end of the stack called the ______ of the stack. Answer: top Ver. 1.0 Session 10
  • 41. Data Structures and Algorithms Implementing a Stack Using a Linked List Write an algorithm to implement theas follows: The algorithm for PUSH operation isPUSH and POP POP operation is as follows: operations using a Linkedtmp point to the topmost node. Make a memory for the List. 1. Allocatevariable/pointer new node. 2. Assign valuevalue contained in the topmost node. Retrieve the to the data field of the new node. 3. Make the next field of nextnew node point to top. top point to the the node in sequence. 4. Make topmemory allocatednode. node marked by tmp. Release point to the new to the Ver. 1.0 Session 10
  • 42. Data Structures and Algorithms Activity: Implementing a Stack Using an Array Problem Statement: Write a program to implement a stack by using an array that can store five elements. Ver. 1.0 Session 10
  • 43. Data Structures and Algorithms Activity: Implementing a Stack Using a Linked List Problem Statement: Write a program to implement a stack by using a linked list. Ver. 1.0 Session 10
  • 44. Data Structures and Algorithms Just a minute What will be the condition for stack full in a stack implemented as a linked list? Answer: When a stack is implemented as a linked list, there is no upper bound limit on the size of the stack. Therefore, there will be no stack full condition in this case. Ver. 1.0 Session 10
  • 45. Data Structures and Algorithms Just a minute If a stack is represented in memory by using a linked list, then insertion and deletion of data will be done ________. 1. At the end of the list 2. At the beginning of the list 3. Anywhere in the list 4. At the beginning and at the end of the list respectively Answer: 2. At the beginning of the list Ver. 1.0 Session 10
  • 46. Data Structures and Algorithms Applications of Stacks Some of the applications of stacks are: Implementing function calls Maintaining the UNDO list for an application Checking the nesting of parentheses in an expression Evaluating expressions Ver. 1.0 Session 10
  • 47. Data Structures and Algorithms Implementing Function Calls Implementing function calls: Consider an example. There are three functions, F1, F2, and F3. Function F1 invokes F2 and function F2 invokes F3, as shown. Ver. 1.0 Session 10
  • 48. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() Assuming these instructions at the { given locations in the memory. 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 49. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() { The execution starts from 1100 int x; function F1 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 50. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 51. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x=5 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 52. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x=5 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } Address and the local variable of F1 void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 53. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x = 10 5 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 54. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x = 10 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 1122, x = 10 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } Address and the local variable of F2 void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 55. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x = 20 10 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 1122, x = 10 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } Address and the local variable of F2 void F3(int x) { 1140 x = x × 2; 1141 print x; } Ver. 1.0 Session 10
  • 56. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x = 20 10 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) 1122, x = 10 { 1120 1122, x = 10 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; 20 } Ver. 1.0 Session 10
  • 57. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x=5 10 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) 1103, x = 5 { 1120 x = x + 5; 1121 F3(x); 1103, x = 5 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; 20 10 } Ver. 1.0 Session 10
  • 58. Data Structures and Algorithms Implementing Function Calls (Contd.) void F1() x=5 { 1100 int x; 1101 x = 5; 1102 F2(); 1103 print(x); } void F2(int x) { 1120 x = x + 5; 1121 F3(x); 1122 print(x); } void F3(int x) { 1140 x = x × 2; 1141 print x; 20 10 5 } Ver. 1.0 Session 10
  • 59. Data Structures and Algorithms Maintaining the UNDO list for an Application Maintaining the UNDO list for an application: Consider that you made some changes in a Word document. Now, you want to revert back those changes. You can revert those changes with the help of an UNDO feature. The UNDO feature reverts the changes in a LIFO manner. This means that the change that was made last is the first one to be reverted. You can implement the UNDO list by using a stack. Ver. 1.0 Session 10
  • 60. Data Structures and Algorithms Checking the Nesting of Parentheses in an Expression Checking the nesting of parentheses in an expression: You can do this by checking the following two conditions: – The number of left parenthesis should be equal to the number of right parenthesis. – Each right parenthesis is preceded by a matching left parenthesis. Ver. 1.0 Session 10
  • 61. Data Structures and Algorithms Evaluating Expressions Evaluating an expression by using stacks: Stacks can be used to solve complex arithmetic expressions. The evaluation of an expression is done in two steps: Conversion of the infix expression into a postfix expression. Evaluation of the postfix expression. Ver. 1.0 Session 10
  • 62. Data Structures and Algorithms Activity: Implementing a Stack using a Linked List Problem Statement: Write a program that accepts an infix expression, and then converts it into a postfix expression. You can assume that the entered expression is a valid infix expression. Ver. 1.0 Session 10
  • 63. Data Structures and Algorithms Summary In this session, you learned that: A stack is a collection of data items that can be accessed at only one end, called top. The last item inserted in a stack is the first one to be deleted. A stack is called a LIFO data structure. There are two operations that can be performed on stacks. They are: – PUSH – POP – Stacks can be implemented by using both arrays and linked lists. Ver. 1.0 Session 10
  • 64. Data Structures and Algorithms Summary (Contd.) Stacks are used in many applications. Some of the application domains of stacks are as follows: Implementing function calls Maintaining the UNDO list for an application Checking the nesting of parentheses in an expression Evaluating expressions Ver. 1.0 Session 10

Editor's Notes

  1. To start the session, you need to get a set of playing cards in the class. Follow the instructions as given below to begin the game of Rummy. 1. The game begins by dealing a fixed number of cards to all players. The remaining cards are placed face down to form a “stock” pile. 2. There is also a face-up pile called the “discard” pile. 3. Initially, the discard pile contains only one card which is obtained by picking the topmost card from the stock pile. 4. Each player can draw either the topmost card of the stock pile or the topmost card on the discard pile to make a valid sequence in his/her hand. 5. After this, the player must discard one card on top of the discard pile. 6. The next player, can then draw either the topmost card of the draw pile or the topmost card of the discard pile. 7. Therefore, if a player has to draw a card from the discard pile, he/she can draw only the topmost card of the discard pile. 8. Similarly, when a player has to discard a card, he/she must discard it on the top of the discard pile. 9. The discard pile can therefore be considered a Last-In-First-Out list. 10. The last card placed on top of the discard pile is the first one to be drawn. 11. To represent and manipulate this kind of a discard pile in a computer program, you would like to use a list that: a. Contains the details of all the cards in the discard pile. b. Implements insertion and deletion of card details in such a way that the last inserted card is the first one to be removed. This kind of a list can be implemented by using a stack. Ask students to define a stack? Ask them to refer to the game and come up with some characteristics of a stack. Then come to next slide and give them the definition of stacks.
  2. You can give some more explanation of stacks by with the help of the following example. 1. A stack is like an empty box containing books, which is just wide enough to hold the books in one pile. 2. The books can be placed as well as removed only from the top of the box. 3. The book most recently put in the box is the first one to be taken out. 4. The book at the bottom is the first one to be put inside the box and the last one to be taken out.
  3. 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.
  4. 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.
  5. This is one of the applications where stack is used. You need to run through the slides and then ask students to write an algorithm to check the correctness of the nested parenthesis. The algorithm will be: Scan the expression. Push the opening bracket into the stack. If a closing bracket is encountered, check if the closing bracket is same as the opening bracket. If this condition holds true, POP the corresponding opening bracket from the stack. If the condition is false, the expression is not a valid expression.
  6. Tell students that stacks can be implemented using both arrays and Linked List. The slides to explain the implementation of stacks using an array are given.
  7. Make this session an interactive one by asking students to write an algorithm to implement POP operation using an array. Let students first try to write the algorithm. Then you can explain the algorithm given in the student guide. In addition, also explain the exception conditions that is encountered while popping an element from the stack, using an array.
  8. 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.
  9. Student have learnt the implementation of Linked List in chapter 2, therefore ask students to write an algorithm for the PUSH and POP operations using linked lists.
  10. To demonstrate this activity, you do not need to write the complete code. You can use the data files provided at the following location. TIRM  Datafiles for Faculty  Chapter 06  Activities  StackUsingArrays_CSharp.txt and TIRM  Datafiles for Faculty  Chapter 06  Activities  StackUsingArrays_CPP.txt .
  11. To demonstrate this activity, you do not need to write the complete code. You can use the data files provided at the following location. TIRM  Datafiles for Faculty  Chapter 06  Activities  StackUsingLinkedList_CSharp.txt and TIRM  Datafiles for Faculty  Chapter 06  Activities  StackUsingLinkedList_CPP.txt .
  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.
  13. 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.
  14. In this section, you need to discuss the applications of Stacks. In addition, to the given applications, you can discuss some real life applications also. For example, ask students to notice the operations in their Mobile phones. When you go to the Menu  log  Recent calls  Missed calls. On the missed call page, you will be able to view all the missed call number. To back to the main page, you need to press the back option and it will take you page, you last visited.
  15. In this section, you need to discuss the applications of Stacks. In addition, to the given applications, you can discuss some real life applications also. For example, ask students to notice the operations in their Mobile phones. When you go to the Menu  log  Recent calls  Missed calls. On the missed call page, you will be able to view all the missed call number. To back to the main page, you need to press the back option and it will take you page, you last visited.
  16. Explain this concept to students by further telling them that the changes are internally stored in the stack data structure inside the computer. The last change made is the first one to be reverted.
  17. In this slide, ask students to relate to the example they have learnt in the beginning of the session.
  18. In this slide, ask students to relate to the example they have learnt in the beginning of the session.
  19. To demonstrate this activity, you do not need to write the complete code. You can use the data files provided at the following location. TIRM  Datafiles for Faculty  Chapter 06  Activities  InfixToPostfix_CSharp.txt and TIRM  Datafiles for Faculty  Chapter 06  Activities  InfixToPostfix_CPP.txt .