Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can push and pop all a stack, but my book doesn\'t show a good code to represe

ID: 3890475 • Letter: I

Question

I can push and pop all a stack, but my book doesn't show a good code to represent the difference between array stack and the LinkedStack

Exercise1 Create a new Driver class with a main method. In this method, do the following: 1. Create an instance of an ArrayStack and a LinkedStack class 2. Push the following int's onto the two stacks: (1,7,3,4,9,2) 3. Pop off all the elements from the stacks, displaying each int as it's removed Exercise 2 Modify the LinkedStack class to include a new method called removeBottomHalf, which removes the half of elements sitting at the bottom of the stack Test the method using the Driver program. What is the time complexity of this method?

Explanation / Answer

Please find my implementation.

I have answered Q1.

Please repost other question in separate post.

#########

public class ArrayStack {

  

   private int[] arr;

   private int n;

   private int capacity;

  

   public ArrayStack(int cap) {

       capacity = cap;

       n = 0;

       arr = new int[cap];

   }

  

   public int size() {

       return n;

   }

  

   public void push(int item) {

       if(n == capacity)

           System.out.println("Stack is full");

       else {

           arr[n] = item;

           n++;

       }

   }

  

   public int pop() {

       int data = -999;

       if(n == 0)

           System.out.println("Stack is full");

       else {

           data = arr[n-1];

           n--;

       }

       return data;

   }

}

###########

public class LinkedStack {

   class Node {

       int data;

       Node next;

       Node(int item) {

           data = item;

       }

   }

   private Node top;

   private int n;

   public LinkedStack() {

       top = null;

       n = 0;

   }

   public void push(int item) {

       Node newNode = new Node(item);

       newNode.next = top;

       top = newNode;

       n++;

   }  

   public int size() {

       return n;

   }

   public int pop() {

       if(n == 0) {

           return -999;

       }

       int data = top.data;

       top = top.next;

       n--;

       return data;

   }

}

########

public class Driver {

  

   public static void main(String[] args) {

      

       ArrayStack stack = new ArrayStack(10);

      

       stack.push(1);

       stack.push(7);

       stack.push(3);

       stack.push(4);

       stack.push(9);

       stack.push(2);

      

       LinkedStack stack1 = new LinkedStack();

       stack1.push(1);

       stack1.push(7);

       stack1.push(3);

       stack1.push(4);

       stack1.push(9);

       stack1.push(2);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote