Skip to content

Instantly share code, notes, and snippets.

@Arjun2002tiwari
Last active September 30, 2021 13:44
Show Gist options
  • Save Arjun2002tiwari/76c4fe8a8fcd85793198f0ef6226fb24 to your computer and use it in GitHub Desktop.
Save Arjun2002tiwari/76c4fe8a8fcd85793198f0ef6226fb24 to your computer and use it in GitHub Desktop.
//EXAMPLE OF STACK
class Stack {
int stck[] = new int[10];
int tos;
Stack(){
tos = -1;
}
//METHOD TO ENTER THE DATA IN THE STACK
void push(int item){
if (tos==9){
System.out.println("stack is full");
}
else{
stck[++tos] = item;
}
}
//METHOD TO RETRIVE THE DATA FROM THE STACK
int pop(){
if (tos<0){
System.out.println("stack is underflow");
return 0;
}
else{
return stck[tos--];
}
}
}
class TestStack{
public static void main(String[] args) {
Stack myStack1 = new Stack();
Stack myStack2 = new Stack();
for (int i=0;i<10;i++){
myStack1.push(i);
}
for (int i=10;i<20;i++){
myStack2.push(i);
}
System.out.println("the first stack is: ");
for (int i=0;i<10;i++){
System.out.println(myStack1.pop());
}
System.out.println("the second stack is: ");
for (int i=0;i<10;i++){
System.out.println(myStack2.pop());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment