Skip to main content

Posts

Showing posts with the label stack

Infix to Prefix conversion using Stack

This post is about conversion of Infix expression to Prefix conversion. For this conversion we take help of stack data structure, we need to push and pop the operators in and out of the stack. Infix expressions are the expressions that we normally use, eg. 5+6-7; a+b*c etc. Prefix expressions are the expressions in which the 2 operands are preceded by the operator eg. -+56 7 , +a*bc etc. This method is very similar to the method that we used to convert Infix to Postfix but the only difference is that here we need to reverse the input string before conversion and then reverse the final output string before displaying it. NOTE: This changes one thing that is instead of encountering the opening bracket we now first encounter the closing bracket and we make changes accordingly in our code. So, to convert an infix expression to a prefix expression we follow the below steps (we have 2 string, 1st is the input infix expression string 2nd is the output string which is empty initially)...

Stack implementation using Linked List

Stack is a data structure it serves as a collection of elements. It has 2 principle operation push and pop.Stack works on the principle of  Last In First Out or LIFO i.e the element that is entered last is removed first. To do this is has 2 methods  push () :  push operation pushes the element on the top. pop () :  pop operation removes the element on the top. This can be seen in  the below image. Linked list  is a simple linear data structure formed by collection of data elements called nodes. Each node consists of a data element and link field.The data field consists of element and link field consists of address of next element. To know more about Stack Data Structure click  here To know more about Linked List click  here To view Simple Linked List implementation click  here      Now inorder to implement Stack using linked list we need to implement push and pop operation in Linked List. Push and...

Stack implementation using array

A stack is a data structure it serves as a collection of elements. It has 2 principle operation push and pop. The push operation pushes the element on the top and the pop operation removes the element on the top. This can be seen in the below image. Stack works on the principle of  Last In First Out or LIFO i.e the element that is entered last is removed first. Stacks can be implemented using arrays as well as linked list. There are a lot of applications of Stacks like Recursion Backtracking  Memory management  Activation records etc To know more about Stacks click here  The following program is to implement a stack using an array with maximum size of 20. C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program You might also be interested in  Linked List in python Linked List in C++ Finding the next Smallest Palindrome Finding facto...

Program to implement Balanced Brackets using stack

This post is about detecting if the brackets in a string are balanced, this problem is usually a sub problem of expression conversion (infix, prefix, postfix) using stack. In the following program we traverse the string and search for the opening bracket if an opening bracket is found then we push it on the stack and if the closing bracket is found then we pop the stack and compare if they are the same type of opening and closing brackets if yes then we continue else we stop. C++ Program Java program Python Program C++ Program Java Program Python Program You might also be interested in  Singly Linked List Double Linked List Linked List in Python Infix to Prefix Conversion Infix to Postfix Conversion Binary Search Tree