Question

Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T info; private LinkListNode link; public LinkListNode ( T info) this, info = info; link = null; public T getInfo() return info; public LinkListNode getlink() return link; public void setInfo( T info) this.infoinfo; public void setLink (LinklistNode link) this.link = link; public interface StackInterface <T T top() throws StackUnderflowException; void pop () throws StackUnderflowException; void push (T element);
media%2F7c1%2F7c15ef9e-b67f-4155-9db2-23
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Stack<T> implements StackInterface<T>{

private LinkListNode<T> stkTop;

public Stack() {

stkTop=null;

}

@Override

public T top() throws StackUnderflowException {

if(stkTop == null)

throw new StackUnderflowException();

return stkTop.getInfo();

}

@Override

public void pop() throws StackUnderflowException {

if(stkTop == null)

throw new StackUnderflowException();

LinkListNode<T> temp = stkTop;

stkTop = stkTop.getLink();

temp.setInfo(null);

temp.setLink(null);

}

@Override

public void push(T element) {

LinkListNode<T> newNode = new LinkListNode<T>(element);

if(stkTop == null){

stkTop = newNode;

}

else{

newNode.setLink(stkTop);

stkTop = newNode;

}

}

public String toString(){

String str="";

LinkListNode<T> temp = stkTop;

while(temp != null){

str=temp.getInfo().toString()+" "+str;

temp =temp.getLink();

}

return str;

}

}

=================================================

public class LinkListNode<T> {

private T info;

private LinkListNode<T> link;

public LinkListNode(T info) {

super();

this.info = info;

this.link = null;

}

public T getInfo() {

return info;

}

public void setInfo(T info) {

this.info = info;

}

public LinkListNode<T> getLink() {

return link;

}

public void setLink(LinkListNode<T> link) {

this.link = link;

}

}

========================================================

public interface StackInterface<T> {

T top() throws StackUnderflowException;

void pop() throws StackUnderflowException;

void push(T element);

}

=======================================


public class StackUnderflowException extends Exception {

}

===================================================

public class Main {

public static void main(String[] args) throws StackUnderflowException {

Stack<String> stk = new Stack<>();

stk.push("one");

stk.push("two");

stk.push("three");

stk.push("four");

stk.push("five");

System.out.println("Stack : [ "+stk+" ]");

System.out.println("Top : [ "+(String)stk.top()+" ]");

stk.pop();

System.out.println("After pop : ");

System.out.println(stk);

}

}

======================================================

@ Javadoc島Declaration Console X 굴좀 Diagrams td> Main (5) Java Application] C:AProgram FilesJavajre1.8.0_161\binjavaw.exe Stac

Add a comment
Know the answer?
Add Answer to:
Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info;...

    public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info; this.link = null; } public CharNode(char c, CharNode link) { this.info = info; this.link = link; } public CharNode getLink() { return link; } public void setLink(CharNode link) { this.link = link; } public char getInfo() { return info; } public void setInfo(char info) { this.info = info; } } //Remember Queue is a first-in-first-out data structure public class CharQueue { // front is...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info...

    Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T) Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: setInfo Access modifier: public Parameters: info (data type T) Return type: void Task: sets the...

  • Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support...

    Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support package) Files required: 1. ListInterface.java (given) 2. LLUnsortedList.java (implements ListInterface.java) 3. LLSortedList.java (extends LLUnsortedList.java, implements ListInterface.java) 4. LLIndexedList.java (implements LLIndexedListInterface.java, extends LLUnsortedList.java) 5. Driver ** Maybe create LLIndexedListInterface.java file too even though it is not required file! I need the bolded Java Files (5) to be written separate  classes please! The interfaces can be implemented from below! Thank you zip all your files and submit....

  • Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list...

    Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list and returns a reference to the new list. For our example list the statement values = remove(6, values); would result in values referencing the list containing 3 9 12 15 18 19 19 and 20. If target is not contained in list then the list remains unchanged. Please also add a driver class to demonstrate that the method is working correctly. LLNode.java //---------------------------------------------------------------------------- //...

  • In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...

    In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty. The "LLNode" class is given: public class LLNode { protected T info; protected LLNode link; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode...

  • use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier:...

    use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: Queue Node<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name:...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty...

    Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT