Posted by Abhishek on December 21, 2019
Stacks are powerful data structures that can help us solve many complex programming problems. Stacks can be used in some use-cases like:
Stacks can be visualized as a stack of books. The peculiarity of a Stack is that items can be added to the stack only on the top & removed only from the top. If you want to access the bottom-most item in the stack, you have to remove the books from the top until you reach the bottom-most item. This principle is called LIFO (Last In First Out).
All the operations can be done in O(1) complexity because these operations are done on the top of the stack. There is no need to iterate.
String str = "abcd";
Stack stack = new Stack();
foreach (char ch in str)
{
stack.Push(ch);
}
StringBuilder builder = new StringBuilder();
while (stack.Count != 0)
{
builder.Append(stack.Pop());
}
Console.WriteLine(builder.ToString());
An expression is balanced if the sequence of opening brackets matches with sequence of closing brackets. For example: ((1+2)) is balanced ((1+2>) is not balanced
public class Expression
{
private ArrayList leftBrackets = new ArrayList() { ')', ']', '>', '}' };
private ArrayList rightBrackets = new ArrayList() { '(', '[', '<', '{' };
public Boolean IsBalanced(string str)
{
Stack stack = new Stack();
foreach (char ch in str)
{
if (isLeftBracket(ch))
{
stack.Push(ch);
}
if (isRightBracket(ch))
{
if (stack.Count == 0) return false;
var poppedBracket = Convert.ToChar(stack.Pop());
if (!doesBracketsMatch(poppedBracket, ch)) return false;
}
}
return (stack.Count == 0 ? true : false);
}
private bool doesBracketsMatch(char left, char right)
{
return (rightBrackets.IndexOf(right) == leftBrackets.IndexOf(left));
}
private bool isRightBracket(char ch)
{
return rightBrackets.Contains(ch);
}
private bool isLeftBracket(char ch)
{
return leftBrackets.Contains(ch);
}
}
Expression exp = new Expression();
System.Console.WriteLine(exp.IsBalanced("((1+2>)"));
public class Stack
{
private int[] items;
private int count;
public Stack()
{
items = new int[2];
}
public void push(int number)
{
if (items.Length == count)
{
int[] newItems = new int[count * 2];
for (int i = 0; i < count; i++)
{
newItems[i] = items[i];
}
items = newItems;
}
items[count++] = number;
}
public int peek()
{
if (count == 0)
throw new System.Exception("Nothing to Peek");
return items[count - 1];
}
public int pop()
{
if (count == 0)
throw new System.Exception("Cannot Pop");
return items[--count];
}
public bool isEmpty()
{
return count == 0;
}
}
Stack stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
System.Console.WriteLine(stack.peek());
stack.pop();
System.Console.WriteLine(stack.peek());
System.Console.WriteLine(stack.isEmpty());
public class Stack2
{
private Node First;
private Node Last;
private int size;
public int Count
{
get { return size; }
}
public class Node
{
public int Value;
public Node Next;
public Node(int value)
{
Value = value;
}
}
public bool isEmpty()
{
return First == null;
}
public void push(int number)
{
var newNode = new Node(number);
if (isEmpty())
{
First = Last = newNode;
}
else
{
Last.Next = newNode;
Last = Last.Next;
}
size++;
}
public int peek()
{
if (isEmpty())
throw new System.Exception("Nothing to Peek");
return Last.Value;
}
public int pop()
{
if (First == Last)
{
First = Last = null;
}
else
{
var previous = GetPrevious(Last);
Last = previous;
Last.Next = null;
}
if (isEmpty())
throw new System.Exception("Cannot Pop");
size--;
return Last.Value;
}
private Node GetPrevious(Node node)
{
var currentNode = First;
while (currentNode != null)
{
if (currentNode.Next == node)
return currentNode;
else
currentNode = currentNode.Next;
}
return null;
}
}
Stack2 stack = new Stack2();
stack.push(10);
stack.push(20);
stack.push(30);
System.Console.WriteLine(stack.peek());
stack.pop();
System.Console.WriteLine(stack.peek());
System.Console.WriteLine(stack.isEmpty());