Iterative pre-order traversal using stack

We have seen the in-order traversal of a binary tree. It is a recursive code. Recursion stack can be simulated using an explicit Stack data structure and keeping the function non-recursive. Let us see how:

Check duplicate parenthesis in an expression

A valid mathematical expression can also have duplicate parenthesis as shown below: ((a+b)) (((a+(b)))+(c+d)) Write code to find if an expression has duplicate parenthesis. You may assume that expression does not have any white spaces.

Code to evaluate a postfix expression

Given an expression in postfix notation, write code to evalute a mathematical expression given in postfix notation. For Example: Input: 4 2 3 + * 6 – Output: 14

Code to convert In-Fix to postfix notation

Given an arithmetic expression in in-fix notation. Write code to convert that algorithm in corresponding post-fix notation. For example: Infix Postfix —— ——– a+b ab+ (a+b)*c ab+c* a+b*c abc*+ a*(b+c)-d/e abc+*de/-

Implement Stack using two Queues

We have seen the code to implement a Queue using two stacks. This question is opposite of that. Use only Queue data structure to implement a Stack.

Implementing a Queue using two Stacks

We have seen how to implement Queue using a linked list and using Array as the base data structure.  The question here is that given two Stacks, say S1 and S2, and no other data structure, simulate the functionality of a Queue, hence use two stacks as the base data structure for a Queue. i.e […]

Understanding stack data structure

Stack is a collection of objects where objects are inserted and removed in the Last-In-First-Out order. Element inserted at the end will be deleted first. It is a limited access data structure which essentially allows two operations push and pop. Push: Inserting data at the top of stack. Pop: deleting data from the top of stack. The […]

Array implementation of Stack

A better implementation of Stack is usually using linked list unless you are sure of the number of elements in array. But if you are just starting with data structures and are not familiar with linked list, you can try implementing stack in an array. While using array to implement stack is easy, it has limitation that […]

Linked List Implementation of Stack in C++

Write a class which will implement the Stack data structure in C++. Give the declaration & definition of the class. Also define the Node. For simplicity, you may assume stack to hold integer data type.

Implement two stacks in one array

Given a linear memory (in the form of an array). Implement two stacks such that memory is used in an optimal manner. i.e if user wants to push an element in either of the two stacks, then the implementation should not give error until the entire array is full (So cannot use half array for […]