Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a Stack class by using Linked list. I push the new data at the tail, so I pop at the tail too. However,

This is a Stack class by using Linked list. I push the new data at the tail, so I pop at the tail too. However, there are something wrong for the pop method. I push "R","a","c","e","c","a", and "r", but I cannot pop out the very last Node "R". How to fix it?

public class Stack { private class Node{ private T data; private Node link; public Node(){ data = null; link = null; } public Node(T input, Node next){ data = input; link = next; } } private Node head; public Stack(){ head = null; } public void push(T input){ if (head==null){ head = new Node(input, null); } else{ Node position = head; while (position.link!=null){//stop at last node position = position.link; } Node a = new Node(input, null); position.link = a; } } public T pop(){ Node removed = new Node(); Node position = head; int count=1; int length = size(); while (count a = new Stack(); // Queue q = new LinkedList(); a.push('R'); a.push('a'); a.push('c'); a.push('e'); a.push('c'); a.push('a'); a.push('r'); System.out.println("Size : " + a.size()); a.outputList(); while(!a.isEmpty()) { System.out.println(a.pop()); } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

6. Vanguard

Answered: 1 week ago