Implementing Many Stacks in the Same Program

Joined
Aug 10, 2021
Messages
2
Reaction score
1
I am trying to make a program that implements a new stack every time the capacity of the previous stack is exceeded. Therefore, implementing many stacks in the same program. Here is the code:

Java:
package StackOfPlate;

import java.util.ArrayList;
import java.util.Stack;

public class StackOfPlates3 {
     public static class Node {
         public Node above, below;
         public int value;
         public Node (int value) {
             this.value = value;
         }
     }
     public static class SetOfStacks {
         ArrayList <Stack> stacks = new ArrayList<Stack>();
         public int capacity;
         public SetOfStacks(int capacity) {
             this.capacity = capacity;
         }
         public Stack getLastStack() {
             if (stacks.size() == 0) {
            return null;
             }
             return stacks.get(stacks.size()-1);
         }
         public void push (int v) {
             Stack last = getLastStack();
             if (last != null && !last.isFull()) {
                 last.push(v);
                 System.out.println("Element Pushed on Stack is :" + v);
             } else {
                 Stack stack = new Stack (capacity);
                 System.out.println("A new stack was created.");
                 stack.push(v);
                 System.out.println("Element Pushed on Stack is :" + v);
                 stacks.add(stack);
             }
         }
         public int pop() {
             Stack last = getLastStack();
             if(last == null) {
                 System.out.println("Stack underflow");
             }
                 int v = last.pop();
                 System.out.println("Element poped from stack is:"+v);
                 if(last.size == 0) {
                     stacks.remove(stacks.size() - 1);
                     System.out.println("A stack have been removed.");
                 }
                     return v;
             }
         public boolean isEmpty() {
             Stack last = getLastStack();
             return (last == null || last.isEmpty());
         }
         public int popAt (int index) {
             return leftShift(index, true);
         }
         public int leftShift (int index, boolean removeTop) {
             Stack stack = stacks.get(index);
             int removed_item;
             if (removeTop) {
                 removed_item = stack.pop();
                 } else {
                  removed_item = stack.removeBottom();
                 }
             if (stack.isEmpty()) {
                 stacks.remove(index);
             } else if(stacks.size() > index + 1) {
                 int v = leftShift (index + 1, false);
                 stack.push(v);
                 }
             return removed_item;
         }
     }
     public static class Stack {
         private int capacity;
         public Node top, bottom;
         public int size = 0;
         public Stack (int capcity) { this.capacity = capacity; }
         public boolean isFull() {return capacity == size;}
         public boolean isEmpty() {return size == 0;}
         public void join (Node above, Node below) {
             if (below != null) {below.above = above;}
             if (above != null) {above.below = below;}
         }
         public boolean push (int v) {
             if (size >= capacity) {
                 return false;
             }
                 size++;
                 Node n = new Node (v);
             if (size == 1) {
                 bottom = n;
                 }
                 join(n, top);
                 top = n;
                return true;
         }
         public int pop () {
             Node t = top;
             top = top.below;
            size--;
             return t.value;
         }
         public int removeBottom() {
             Node b = bottom;
             bottom = bottom.above;
             if (bottom != null) {
                 bottom.below = null;
             }
                 size--;
            return b.value;
         }
     }
     public static void main(String[] args) {
            SetOfStacks impl = new SetOfStacks (10);
            System.out.println("--------------");
            impl.push(210);
            impl.push(310);
            impl.push(50);
            impl.push(400);
            impl.push(410);
            impl.push(610);
            impl.push(70);
            impl.push(4);
            impl.push(1);
            impl.push(20);
            impl.push(21);
            impl.push(41);
            impl.push(101);
            impl.push(8);
            impl.push(10);
            System.out.println("------------------");
            impl.pop();
            impl.pop();
            impl.pop();
            System.out.println("------------------------");
            }
}

The pop() method does not work. A new stack is created every time I add an element, but the capacity of a stack should be 10. Thus, a new stack should only be created after I added 10 elements. There is an exception in thread "main" because "this.top" is null. I don't understand why the program is not working.

Thank you!
 
Last edited:
Joined
Aug 10, 2021
Messages
2
Reaction score
1
I am trying to make a program that implements a new stack every time the capacity of the previous stack is exceeded. Therefore, implementing many stacks in the same program. Here is the code:

Java:
package StackOfPlate;

import java.util.ArrayList;
import java.util.Stack;

public class StackOfPlates3 {
     public static class Node {
         public Node above, below;
         public int value;
         public Node (int value) {
             this.value = value;
         }
     }
     public static class SetOfStacks {
         ArrayList <Stack> stacks = new ArrayList<Stack>();
         public int capacity;
         public SetOfStacks(int capacity) {
             this.capacity = capacity;
         }
         public Stack getLastStack() {
             if (stacks.size() == 0) {
            return null;
             }
             return stacks.get(stacks.size()-1);
         }
         public void push (int v) {
             Stack last = getLastStack();
             if (last != null && !last.isFull()) {
                 last.push(v);
                 System.out.println("Element Pushed on Stack is :" + v);
             } else {
                 Stack stack = new Stack (capacity);
                 System.out.println("A new stack was created.");
                 stack.push(v);
                 System.out.println("Element Pushed on Stack is :" + v);
                 stacks.add(stack);
             }
         }
         public int pop() {
             Stack last = getLastStack();
             if(last == null) {
                 System.out.println("Stack underflow");
             }
                 int v = last.pop();
                 System.out.println("Element poped from stack is:"+v);
                 if(last.size == 0) {
                     stacks.remove(stacks.size() - 1);
                     System.out.println("A stack have been removed.");
                 }
                     return v;
             }
         public boolean isEmpty() {
             Stack last = getLastStack();
             return (last == null || last.isEmpty());
         }
         public int popAt (int index) {
             return leftShift(index, true);
         }
         public int leftShift (int index, boolean removeTop) {
             Stack stack = stacks.get(index);
             int removed_item;
             if (removeTop) {
                 removed_item = stack.pop();
                 } else {
                  removed_item = stack.removeBottom();
                 }
             if (stack.isEmpty()) {
                 stacks.remove(index);
             } else if(stacks.size() > index + 1) {
                 int v = leftShift (index + 1, false);
                 stack.push(v);
                 }
             return removed_item;
         }
     }
     public static class Stack {
         private int capacity;
         public Node top, bottom;
         public int size = 0;
         public Stack (int capcity) { this.capacity = capacity; }
         public boolean isFull() {return capacity == size;}
         public boolean isEmpty() {return size == 0;}
         public void join (Node above, Node below) {
             if (below != null) {below.above = above;}
             if (above != null) {above.below = below;}
         }
         public boolean push (int v) {
             if (size >= capacity) {
                 return false;
             }
                 size++;
                 Node n = new Node (v);
             if (size == 1) {
                 bottom = n;
                 }
                 join(n, top);
                 top = n;
                return true;
         }
         public int pop () {
             Node t = top;
             top = top.below;
            size--;
             return t.value;
         }
         public int removeBottom() {
             Node b = bottom;
             bottom = bottom.above;
             if (bottom != null) {
                 bottom.below = null;
             }
                 size--;
            return b.value;
         }
     }
     public static void main(String[] args) {
            SetOfStacks impl = new SetOfStacks (10);
            System.out.println("--------------");
            impl.push(210);
            impl.push(310);
            impl.push(50);
            impl.push(400);
            impl.push(410);
            impl.push(610);
            impl.push(70);
            impl.push(4);
            impl.push(1);
            impl.push(20);
            impl.push(21);
            impl.push(41);
            impl.push(101);
            impl.push(8);
            impl.push(10);
            System.out.println("------------------");
            impl.pop();
            impl.pop();
            impl.pop();
            System.out.println("------------------------");
            }
}

The pop() method does not work. A new stack is created every time I add an element, but the capacity of a stack should be 10. Thus, a new stack should only be created after I added 10 elements. There is an exception in thread "main" because "this.top" is null. I don't understand why the program is not working.

Thank you!
The mistake was that I wrote: public Stack (int capcity) { this.capacity = capacity; }. Instead, I should have written: public Stack (int capacity) { this.capacity = capacity; }. Now the code works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,091
Latest member
IsaacLuna

Latest Threads

Top