0% found this document useful (0 votes)
29 views5 pages

Push, Pop, Traverse: Stack

The document discusses stacks and operations on stacks like push, pop and traversal. It defines a Stack class with methods to push an item onto the stack, pop an item from the stack and traverse the entire stack by iterating from top to bottom. It also shows examples of using a stack to check for balanced brackets in a string and deleting an element from a specified position in the stack.

Uploaded by

Rao Afrasiab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Push, Pop, Traverse: Stack

The document discusses stacks and operations on stacks like push, pop and traversal. It defines a Stack class with methods to push an item onto the stack, pop an item from the stack and traverse the entire stack by iterating from top to bottom. It also shows examples of using a stack to check for balanced brackets in a string and deleting an element from a specified position in the stack.

Uploaded by

Rao Afrasiab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy