0% found this document useful (0 votes)
7 views3 pages

Stacks Activity

Uploaded by

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

Stacks Activity

Uploaded by

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

// Josemari P.

De Goma / Shaeirra Jimenez


//INF231
package stacksactivity;

/**
*
* @author Josemari
*/
public class StacksActivity {
static final int intMax = 10;
int intTop;
int a[] = new int[intMax];

boolean isEmpty(){
return (intTop < 0);
}

StacksActivity(){
intTop = -1;
}

boolean push(int x){


if (intTop >= (intMax - 1)){
System.out.println("Stack Overflow");
return false;
}

else {
a[++intTop] = x;
System.out.println(x + "pushed to stack");
return true;
}
}
int intPop() {
if (intTop < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[intTop--];
return x;
}
}
int intPeek() {
if (intTop < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[intTop--];
return x;
}
}

void print() {
for(int i = intTop; i > -1; i--){
System.out.print(" " + a[i]);
}
}

public static void main(String[] args) {


StacksActivity stack = new StacksActivity();
stack.push(5);
stack.push(2);
stack.push(8);
stack.push(9);
stack.push(1);
stack.push(3);
stack.push(4);
stack.push(6);
stack.push(7);
stack.push(10);
stack.push(11);

System.out.println(stack.intPop() + " Popped from stack");


System.out.println(stack.intPop() + " Popped from stack");
System.out.println(stack.intPop() + " Popped from stack");
System.out.println(stack.intPop() + " Popped from stack");

System.out.println("Top element is: " + stack.intPeek());


System.out.print("Elements present in stack: ");
stack.print();
}

}
Output:

You might also like