Push, Pop, Traverse
class stack
public int []stack;
public int top;
public int size;
public stack()
top = -1;
size = 50;
stack = new int[size];
public Boolean push(int item)
top++;
stack[top] = item;
return true;
public int pop()
return stack[top--];
}
public void traverseStack()
if (top < 0)
Console.WriteLine("Stack Underflow");
return;
else
Console.WriteLine("Items in the Stack are :");
for (int i = top; i >= 0; i--)
Console.WriteLine(stack[i]);
Balanced Bracket
public static bool CheckString(string exp){
bool res = true;
char[] charArray = exp.ToCharArray();
Stack st = new Stack();
foreach(char i in charArray){
string item = i.ToString();
if(item == "[" || item == "{" || item == "("){
st.Push(item);
}else if(st.Count > 0){
if(item == "]"){
if(st.Peek().ToString() == "["){
st.Pop();
else{
res =false;
else if(item == "}"){
if(st.Peek().ToString() == "{"){
st.Pop();
}else{
res =false;
}
else if(item == ")"){
if(st.Peek().ToString() == "("){
st.Pop();
}else{
res =false;
}else{
res = false;
return res;
Deletion from Stack
void delete(stack<char> &st, int n, int curr=0)
{
if (st.empty() || curr == n)
return;
int x = st.top();
st.pop();
delete(st, n, curr+1);
if (curr != n/2)
st.push(x);