0% found this document useful (0 votes)
1 views2 pages

Project Class

The document contains two Java classes, Stks and Istak, demonstrating stack operations. The Stks class implements a custom stack with methods for pushing, popping, peeking, and displaying elements, while the Istak class utilizes Java's built-in Stack class to manage strings representing infinity stones. Both classes showcase stack functionality, including checking if the stack is empty and displaying its contents.

Uploaded by

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

Project Class

The document contains two Java classes, Stks and Istak, demonstrating stack operations. The Stks class implements a custom stack with methods for pushing, popping, peeking, and displaying elements, while the Istak class utilizes Java's built-in Stack class to manage strings representing infinity stones. Both classes showcase stack functionality, including checking if the stack is empty and displaying its contents.

Uploaded by

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

PROJECT CLASS

package stks;
public class Stks {

public static void main(String[] args) {


Stack num = new Stack();
num.push(5);
num.push(10);
num.push(15);
num.push(20);
num.push(25);

System.out.println("Element popped:"+num.pop());
System.out.println("Top Element: "+num.peek());
num.show();
}
}
Note: CREATE ANOTHER CLASS
package stks;
public class Stack {

int stk[]= new int [5];


int i = 0;
public void push(int data)
{
stk[i]=data;
i++;
}
public int pop(){

int data;
i--;
data = stk[i];
stk[i]=0;
return data;
}
public int peek() {
i--;
if (i == -1) {

return -1;
} else {
return stk[i];
}
}
public void show()
{
for(int n:stk)
{
System.out.println(n+" ");
}
}

}
Element popped:25
Top Element: 20
5
10
15
20
0

package istak;
import java.util.Stack;
public class Istak {

public static void main(String[] args) {


Stack myStk= new Stack<>();

System.out.println("\tINFINITY STONES");
System.out.println("Is my stack empty? " + myStk.empty());

myStk.push("Reality");
myStk.push("Time");
myStk.push("Space");
myStk.push("Power");
myStk.push("Soul");
myStk.push("Mind");

System.out.println("Elements in Stack: " + myStk);


System.out.println("Is my stack empty? " + myStk.empty());

while (!myStk.isEmpty()) {
myStk.pop();
System.out.println("Elements in Stack: " + myStk);
System.out.println("Is my stack empty? " + myStk.empty());
}
}
}

You might also like