STACK DATA STRUCTURE

 

 

What is Stack Data Structure? Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack and the only element that can be removed is the element that is at the top of the stack, just like a pile of objects.  Basic features of Stack  1. Stack is an ordered list of similar data type. 2. Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out). 3. push() function is used to insert new elements into the Stack and pop() function is used to remove an element from the stack. Both insertion and removal are allowed at only one end of Stack called Top. 4. Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.  Applications of Stack  The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack. There are other uses also like:1. Parsing 2. Expression Conversion(Infix to Postfix, Postfix to Prefix etc)  Implementation of Stack Data Structure  Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in size. Here we will implement Stack using array.  Algorithm for PUSH operation 1. Check if the stack is full or not. 2. If the stack is full, then print error of overflow and exit the program. 3. If the stack is not full, then increment the top and add the element.  Algorithm for POP operation 1. Check if the stack is empty or not. 2. If the stack is empty, then print error of underflow and exit the program. 3. If the stack is not empty, then print the element at the top and decrement the top.  Basic Operations of Stack in a Python Program :  class Stack:     def __init__(self):        self.items = []     def isEmpty(self):         return self.items == []     def push(self, item):         self.items.insert(0,item)      def pop(self):         return self.items.pop(0)     def peek(self):         return self.items[0]     def size(self):         return len(self.items) s = Stack() s.push('hello') s.push('true') s.pop() print (s.items)  OUTPUT: ['hello']

Comments

Popular posts from this blog

IMPORTANT INTERVIEW RELATED QUESTION

Survey Paper

C++ Interview Questions

Git Cheat Sheet: A Beginner to Intermediate Guide

Java Interview Questions

INTERVIEW QUS ON AUTOMATA

OPERATION RESEARCH SYLLABUS

CPU CACHE MEMORY

MAKING POWERPOINT SLIDES