Question
Using java, Add a method to the LinkedStack class (code below) called removeBottomHalf which removes the half of the elements at the bottom of the
Using java, Add a method to the LinkedStack class (code below) called removeBottomHalf which removes the half of the elements at the bottom of the stack. Text the method usinga driver program and find the time complexity of the method.
// This class implements a Stack ADT as a linked list public class LinkedStack { LinkedNode front; // Reference to the first LinkedNode in the list int count; // Number of nodes in the list // Constructor - initializes the front and count variables LinkedStack() { front = null; count = 0; } // Implements the push operation void push(int x) { LinkedNode newNode = new LinkedNode(x); newNode.next = front; front = newNode; count++; } // Implements the pop operation int pop() { int x = front.x; front = front.next; count--; return x; } // Implements the peek operation int peek() { return front.x; } // Implements the isEmpty operation boolean isEmpty() { return front==null; } // Implements the size operation int size() { return count; } // This method returns a String containing // a space separated representation of the underlying linked list public String toString() { String str = ""; LinkedNode cur = front; while (cur!=null) { str += cur.x + " "; cur = cur.next; } return str; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started