Page 133 - DCAP407_DATA_STRUCTURE
P. 133

Data Structure



                                          7.  When the user  enters 3, the function display() is called. In the display()
                                              function, the if loop checks for the stack size. If the stack array is empty, the
                                              program displays  a message  “Stack is empty“.  Else, the for loop displays
                                              each element present in the array.
                                          8.  When the user enters 4, the program terminates.



                          Did you know?   In Computer Science, a call stack stores information about the active subroutines of a
                                        computer program. This stack is known as control stack, machine stack or execution
                                        stack. It is usually shortened to “stack”. The details of the call stack are hidden and
                                        automatic in high-level programming language.

                                        Write a C program to convert the following expression into prefix and postfix
                                        expressions using stacks.
                                            (A + B) * (C – D)/ (E – F)

                          7.5   Summary
                          •   Stacks are simple data structures and important tools in programming language.

                          •   A stack is a linear data structure in which allocation and deallocation are made in a last-in-first-out
                              (LIFO) method.
                          •   The basic operations of stack are inserting an element on the stack (push operation) and deleting
                              an element from the stack (pop operation).

                          •   Stacks are represented in main memory by using one-dimensional array or a singly linked list.
                          •   To implement a stack structure, an array can be used as its storage structure. Each element of the
                              stack occupies one array element. Static implementation of stack can be achieved using arrays.
                          7.6   Keywords

                          Deallocation: A process by which memory is reclaimed.
                          Dynamic Allocation: Automatic memory allocation where memory is allocated as required at run-time.
                          Linear Lists: A sequential list.
                          Static  Allocation:  Process of allocating memory at compile-time before  the associated program is
                          executed.
                          7.7   Self Assessment

                          1.  State whether the following statements are true or false:
                              (a)  Stacks are ordered linear list in which insertion and deletion is done at both the ends.
                              (b)  A stack is a linear data structure in which allocation and deallocation are made in a last-in-
                                  last-out (LILO) method.
                              (c)   The insertions and deletions in a stack is done at one end which known as top of the stack.

                              (d)  The size of the array once declared, cannot be changed during the program execution.
                              (e)  When the array is full, the condition is called stack overflow.











                          126                     LOVELY PROFESSIONAL UNIVERSITY
   128   129   130   131   132   133   134   135   136   137   138