Stack

  • Stack is a linear data structure where items are kept according to the LIFO (Last In First Out) principle, meaning that the first element to be eliminated is the one that was added last.
  • Stack can be implemented using arrays as well as linked lists.
    • Array based stack : The components of the stack are stored in an array in this implementation. It is easy to use and provides quick access to items, but unless you use dynamic arrays, its size is fixed.
    • Linked list based stack : In this implementation, the elements are stored in a linked list. Its size is adjustable, but because of the pointers stored, there is an increased memory overhead.This will increase the flexibility of organizing and storing of the data in the linked list.

UML class diagram of a Stack-node

Structure of a Stack

UML classs diagram of a stack – array based

UML classs diagram of a stack – linked list based

1.2 Operations on stack

  1. Push
    • Add an element to the top of the stack.
      Array based

      Linked list based
  2. Pop
    • Remove the top element of the stack.
      Array based

      Linked list based
  3. Peek
    • Get the top element without deleting.
  4. IsEmpty
    • Check if the stack is empty
  5. Printing the stack
    • print every element after iterating through the stack.
      Array based

      Linked list based
  6. searching the stack
    • Check for a element in the stack.

1.3 Applications of stack

  • Function call management : Programming’s call stack controls function calls and returns.
  • Expression evaluation : Stacks are used in parsing expressions (e.g., converting infix to postfix expressions).
  • Undo / Redo : Used in software applications to provide undo and redo functions, which allow users to return back activities.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top