Skip to main content

Posts

Showing posts with the label Stack

Stack implementation in C++

Stack is a versatile data structure used in situations that need LIFO - last in first out. For an introduction to stack, read this post  Data members To implement a stack using an array, we will need an array and an index to the top of the stack.  We can have a static array or a dynamic array. Dynamic array has the advantage that size of array can be given at run time.  So let us have a dynamic array and also a data member called size. class stack { int max; int * arr; int top; public: stack( int n); void push ( int n); int pop (); bool is_empty (); bool is_full (); int peek (); ~ stack(); stack(stack & other); }; Methods In the constructor of this class we must initialize the data. We need to set top to -1, because we do not have any value in the stack currently. And as we are using dynamic array, we should allocate memory to array in the constructor. We need to also write a destru...

Stack with find minimum function

Question : Implement a stack which finds the minimum of elements in the stack. All the three functions push(), pop() and findmin() must have time complexity of O(1) This is one of the common questions asked in interviews. The problem is finding minimum requires you to traverse through the stack, which is not possible without popping elements. So you can pop elements, find minimum and then push the elements back. But the requirement says that findmin() function must have a constant complexity - O(1). You can achieve this with the help of one more stack, to store minimum values. Let us call this minstack. This is how the operations will work push -  push a value to stack compare the value with top of minstack if value is smaller than minstack top element push it to minstack if not push minstack top element again to minstack pop  pop a value from stack pop a value from minstack findmin get the value from minstack without popping it (peek) ...

Balanced brackets

Have you observed something? When ever you are writing code using any IDE, if you write mismatched brackets, immediately an error is shown by IDE. So how does IDE  know if an expression is having balanced brackets? For that, the expression must have equal number of opening and closing brackets of matching types and also they must be in correct order. Let us look at some examples (a+b)*c+d*{e+(f*g)}   - balanced (p+q*[r+u )] - unbalanced (p+q+r+s) ) - unbalanced (m+n*[p+q]+{g+h}) - balanced So we do we write a program to check if an expression is having balanced brackets? We do need to make use of stack to store the brackets. The algorithm is as follows Scan a character - ch from the expression If the character is opening bracket, push it to stack If the character is closing bracket pop a character from stack If popped opening bracket and ch are not of same type ( ( and ) or [ and ] ) stop the function and return false Repeat steps 2 and 3 till all characters are scanned. On...

Conversion of postfix expression to infix

How do we convert a postfix expression to infix expression? A postfix expression or a "Reverse polish notation" is useful for programming. Because our programs can easily evaluate a postfix expression. But for humans, postfix expressions are difficult to understand. So is it possible to convert a given postfix expression to infix expression? Why not? It is possible with the help of stack data structure.  Remember that when converting an expression from infix to postfix , we used operator stack. But in this case we need an operand stack. What we need to do is - we extract values from expression. If there is an operand, we push it to stack. If there is an operator encountered, we pop two most recent values from stack, apply operator to them, enclose them in paranthesis and push the expression back to stack. This procedure is continued until the entire expression is scanned. In the next step, the content of stack is popped out - which will be our infix expression. So let me bul...

Stack implementation using linked list

A stack data structure is a versatile data structure which is useful whenever we want output to be reverse of input, or we want a LIFO - last in first out behavior Image from Wikipedia . A function call in most languages employs a stack. The parameters and local variables are pushed to call stack. And a return statement pops out most recent call stack. Similarly when we want to check if an expression has balanced brackets, we can use a stack to store brackets. Top of stack:  Unlike a linked list where elements can be added to the end of list or beginning of a list, values are always added to the top of the stack. Just like a plate is added to the top of a stack of plates. And a value is always removed from top of the stack - similar to a stack of plates again. A stack abstract data type should define three functions push - push function inserts a value at the top of stack  pop - pop function removes a value from top of stack isempty - isempty function checks if the stack is em...

Infix, prefix and postfix notations

Mathematical expressions can be written in 3 different methods - infix, postfix and prefix. Infix notation All these days we have been using infix notation. In this operators are written between the operands. Some operators are having higher priorities than others. If some expressions are enclosed in brackets, then they are evaluated first. e.g.     (A+B)*C Here + operator will operate on A and B as it is between them. Though * has higher priority, as A+B is enclosed in brackets, that is evaluated before multiplication. Postfix notation In a postfix notation - also called Reverse Polish Notation, operator is written after the operands. The operator operates on two operands to its immediate left. And expressions are always evaluated from left to right. Brackets do not have any effect on this. e.g. (A+B)*C in postfix is  AB+C* A+B*C+D in postfix is ABC*+D+ Prefix notation In a prefix notation, operator is written before the operands. And just like postfix, expressions ...