نوشته شده توسط : hadavi

قسمت main برنامه

public class Stack {
final int MAX_SIZE = 1024;
private int top;
private T[] buffer;

public Stack() {
top = 0;
//buffer = new T[MAX_SIZE];
buffer=(T[]) new Object[MAX_SIZE];
}

public boolean isEmpty() {
return (top == 0);
}

public boolean isFull() {
return (top == MAX_SIZE);
}

public void push(T x) throws FullStackException{
if (isFull()) {
throw new FullStackException("پشته پر است");
} else {
buffer[top] = x;
top++;
}
}

public T pop() throws EmptyStackException{
if (isEmpty()) {
throw new EmptyStackException("پشته خالی است");
}
top--;
return buffer[top];
}

public T top() {
return buffer[top - 1];
}

}


قسمت main برنامه

public class Stack {
final int MAX_SIZE = 1024;
private int top;
private T[] buffer;

public Stack() {
top = 0;
//buffer = new T[MAX_SIZE];
buffer=(T[]) new Object[MAX_SIZE];
}

public boolean isEmpty() {
return (top == 0);
}

public boolean isFull() {
return (top == MAX_SIZE);
}

public void push(T x) throws FullStackException{
if (isFull()) {
throw new FullStackException("پشته پر است");
} else {
buffer[top] = x;
top++;
}
}

public T pop() throws EmptyStackException{
if (isEmpty()) {
throw new EmptyStackException("پشته خالی است");
}
top--;
return buffer[top];
}

public T top() {
return buffer[top - 1];
}

}

 


 

public class Stack_test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Stack s = new Stack();
try {
s.pop();
s.push("mona");
s.push("hadavi");
s.push("20");
System.out.println(s.pop());
System.out.println(s.isFull());
System.out.println(s.isEmpty());
} catch (FullStackException fse) {
System.err.println(fse.getMessage());
} catch (EmptyStackException ese) {
System.err.println(ese.getMessage());
}
}

}


public class FullStackException extends Exception{
public FullStackException(String message){
super(message);
}
}


 

public class EmptyStackException extends Exception{
public EmptyStackException(String message){
super(message);
}
}



:: بازدید از این مطلب : 503
|
امتیاز مطلب : 0
|
تعداد امتیازدهندگان : 0
|
مجموع امتیاز : 0
تاریخ انتشار : دوشنبه 18 آبان 1394 | نظرات (0)
مطالب مرتبط با این پست
لیست
می توانید دیدگاه خود را بنویسید


💬 نظرات کاربران
💬ثبت نام کاربران
💬ورود کاربران