JavaEx
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GStack<T> {
int tos;
Object [] stck;
public GStack() {
tos = 0;
stck = new Object [5];
}
public boolean isFull() {
for (tos = 4; tos >= 0; tos--)
if (stck[tos] == null) {
System.out.print("빈 배열>>" + tos);
return false;
}
System.out.println("가득찬 배열");
return true;
}
public boolean isEmpty() {
for ( tos = 0; tos < 5; tos++) {
if (stck[tos] != null) {
System.out.print("채워진 배열" + tos);
return false;
}
}
System.out.println("빈 배열");
return true;
}
public void push(T item) {
if ( tos == 5 ) return ;
stck[tos] = item;
System.out.print(">>>" + tos);
}
public T pop() {
return (T)stck[tos];
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
GStack<String> g = new GStack<String>();
int cnt = 4;
while (true) {
String s = scanner.next();
if (s.equals("exit") || g.isFull()) break;
g.push(s);
System.out.println(">>"+g.stck[cnt]+">>"+ cnt);
cnt--;
}
for (int i = 0; i<5; i++) {
if (g.isEmpty()) return ;
System.out.println("-" + g.pop());
g.stck[i] = null;
}
}
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.