Question: So, i got it to print the list stack, but I am having trouble trying to remove the bottom half. This is java program import

So, i got it to print the list stack, but I am having trouble trying to remove the bottom half. This is java program

So, i got it to print the list stack, but I am

import javax.naming.LinkLoopException;

public class LinkedListStack {

private Node top; // the first node

// nest class to define linkedlist node

private class Node {

int value;

Node next;

public Object item;

}

public LinkedListStack() {

top = null;

}

// Remove value from the beginning of the list for demonstrating behaviour of stack

public int pop() throws LinkLoopException {

if (top == null) {

throw new LinkLoopException();

}

int value = top.value;

top=top.next;

Node first = null;

int size;

return value;

}

// Add value to the beginning of the list for demonstrating behaviour of stack

public void push(int value) {

Node oldHead = top;

top = new Node();

top.value = value;

top.next = oldHead;

}

public class removeBottomHalf

{

//for (Node x1 = top ; x1 != null ; x1 = x1.next)

// System.out.print(" " + x1.value);

}

public static void main(String args[]) throws LinkLoopException

{

LinkedListStack stack1=new LinkedListStack();

stack1.push(1);

stack1.push(7);

stack1.push(3);

stack1.push(4);

stack1.push(9);

stack1.push(2);

//stack1.pop();

//stack1.pop();

//stack1.pop();

//stack1.pop();

//stack1.pop();

//stack1.pop();

printList(stack1.top);

}

public static void printList(Node top) {

Node temp = top;

while (temp != null) {

System.out.print(" " + temp.value);

temp = temp.next;

}

System.out.println(" ");

}

}

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!