Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw4;
- import com.sun.org.apache.xpath.internal.SourceTree;
- public class Stack {
- Node[] arr; // regular array
- int capacity;
- int size;
- public Stack(int cap){
- capacity = cap;
- size = 0;
- arr = new Node[cap];
- }
- public void push(Node node){
- if (!isFull()){
- arr[size++] = node;
- }else{
- System.out.println("Stack Overflow!!!");
- }
- }
- public Node pop(){
- if (!isEmpty()){
- return arr[--size];
- }else{
- System.out.println("Stack Overflow!!!");
- }
- return null; // fix this (out of place)
- }
- public boolean isFull(){
- return size == capacity; // fix this
- }
- public boolean isEmpty(){
- return size==0; // fix this
- }
- public void printStack(){
- if (!isEmpty()) {
- System.out.print("[Bottom] ");
- for(int i = 0; i<size; i++)
- System.out.print(arr[i].data+" ");
- System.out.println("[Top]");
- } else {
- System.out.println("Empty Stack!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement