class BoundedStack{
int count=0;
String []stack;
BoundedStack(){}
BoundedStack(int i){
stack= new String[i];
}
void push(String s){
if(count>=stack.length){
System.out.println("stack-overflow");
count--;
}else{
stack[count]=s;
count++;
}
}
String pop(){
if(count>=0){
String s=stack[count];
count--;
return s;
}else{
return "stack-is-empty";
}
}
boolean empty(){
return count==-1;
}
}
public class JPA06_1 {
public static void main(String args[]) {
BoundedStack s = new BoundedStack(3);
s.push("abc");
s.push("def");
s.push("ghi");
s.push("jkl");
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.empty());
}
}
第二題
import java.util.*;
class UnboundedStack{
LinkedList <String> list;
UnboundedStack(){
list = new LinkedList <String>();
}
void push(String s){
list.addFirst(s);
}
String pop(){
if(list.isEmpty()){
return "stack-is-empty";
}else{
String s=list.getFirst();
list.removeFirst();
return s;
}
}
boolean empty(){
return list.isEmpty();
}
}
public class JPA06_2 {
public static void main(String args[]) {
UnboundedStack s = new UnboundedStack();
s.push("abc");
s.push("def");
s.push("ghi");
s.push("jkl");
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.empty());
System.out.println(s.pop());
System.out.println(s.empty());
}
}
第三題
import java.util.*;
abstract class Stack{
abstract String pop();
abstract void push(String s);
String top(){
String s=pop();
push(s);
return s;
}
}
class BoundedStack extends Stack{
int count=0;
String []stack;
BoundedStack(){}
BoundedStack(int i){
stack= new String[i];
}
void push(String s){
if(count>=stack.length){
System.out.println("stack-overflow");
count--;
}else{
stack[count]=s;
count++;
}
}
String pop(){
if(count>=0){
String s=stack[count];
count--;
return s;
}else{
return "stack-is-empty";
}
}
boolean empty(){
return count==-1;
}
}
class UnboundedStack extends Stack{
LinkedList <String> list;
UnboundedStack(){
list = new LinkedList <String>();
}
void push(String s){
list.addFirst(s);
System.out.println("Pushing:"+s);
}
String pop(){
if(list.isEmpty()){
return "stack-is-empty";
}else{
String s=list.getFirst();
list.removeFirst();
System.out.println("Poping:"+s);
return s;
}
}
boolean empty(){
return list.isEmpty();
}
}
class TraceUnboundedStack extends UnboundedStack{
int getSize(){
return list.size();
}
}
public class JPA06_3 {
public static void main(String args[]) {
TraceUnboundedStack s2 = new TraceUnboundedStack();
s2.push("abc");
s2.push("def");
s2.push("ghi");
s2.push("jkl");
System.out.println(s2.getSize());
System.out.println(s2.top());
System.out.println(s2.pop());
System.out.println(s2.pop());
System.out.println(s2.pop());
System.out.println(s2.empty());
System.out.println(s2.pop());
System.out.println(s2.empty());
System.out.println(s2.getSize());
}
}
第四題
import java.util.*;
abstract class Stack{
abstract Object pop();
abstract void push(Object s);
Object top(){
Object s=pop();
push(s);
return s;
}
}
class UnboundedStack extends Stack{
LinkedList <Object> list;
UnboundedStack(){
list = new LinkedList <Object>();
}
void push(Object s){
list.addFirst(s);
//System.out.println("Pushing:"+s);
}
Object pop(){
if(list.isEmpty()){
return "null";
}else{
Object s=list.getFirst();
list.removeFirst();
//System.out.println("Poping:"+s);
return s;
}
}
boolean empty(){
return list.isEmpty();
}
}
class TraceUnboundedStack extends UnboundedStack{
int getSize(){
return list.size();
}
}
public class JPA06_4 {
public static void main(String args[]) {
UnboundedStack s = new UnboundedStack();
s.push("abc");
s.push(2);
s.push("ghi");
System.out.println(s.top());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
第五題
import java.util.*;
class UnboundedStack extends Stack{
LinkedList <Object> list;
UnboundedStack(){
list = new LinkedList <Object>();
}
void push(Object s){
list.addFirst(s);
}
Object pop() throws IndexOutOfBoundsException{
if(list.isEmpty()){
throw new IndexOutOfBoundsException("stack is empty!");
}else{
Object s=list.getFirst();
list.removeFirst();
return s;
}
}
boolean empty(){
return list.isEmpty();
}
}
public class JPA06_5 {
public static void main(String args[]) {
try {
UnboundedStack s = new UnboundedStack();
s.push("abc");
s.push(2);
s.push("ghi");
System.out.println(s.top());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
} catch(IndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
}
沒有留言:
張貼留言