Question

class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...

class Car{
private String make;

public Car(String make){
this.make = make;
}

public String toString(){
return make;
}
}


class Node<T>{

public T data;
public Node next;

public Node(T data){
this.data = data;
this.next = null;
}

}

public class StackLinkedList<T>{

//Single field of type Node   
//to represent the front of the stack



//operation push()
/*
The push operation is equivalent to the inserting a node at
the head of the list.

*/
public void push(T data){

}


//operation pop()
/*
Pop is equivalent to deleting the node at the front
*/
public int pop() throws Exception{
if(head == null){
System.err.println("Stack is empty");
throw new Exception("Stack is empty");
}

int returnValue = head.data;
head = head.next;
return returnValue;

}

//operation peek()
/*
Peek is equivalent to the pop operation
but instead of removing the node, simply return the data
*/
  
public int peek(){
if(head == null){
System.err.println("Stack is empty");
throw new Exception("Stack is empty");
}
return head.data;
}

//operation print()
/*
Walk through the list (stack) and print it out.
*/

public void print(){
Node temp = head;
while(temp != null){
System.out.print(temp.data + "->");
temp = temp.next;
}
}

//main()
public static void main(String[] args){
/*
Test cases have been set up to test the program. The stack will be
populated with different types.
You can comment them out when while implementing the operations.
*/
  
StackLinkedList s1 = new StackLinkedList();
s1.push(new Car("Honda"));
s1.push(new Car("BMW"));
s1.print();
  
System.out.println("top of the stack: "+ s1.peek());
  
s1.pop();
s1.print();
  
s1.push(new Car("Mercedes"));
s1.print();
  
s1.pop();
s1.pop();
s1.pop();
System.out.println("\n--------------------------------\n");
  
  
StackLinkedList s2 = new StackLinkedList();
s2.push(10);
s2.push(20);
s2.print();
  
System.out.println("top of the stack: "+ s2.peek());
  
s2.pop();
s2.print();
  
s2.push(30);
s2.print();
  
s2.pop();
s2.pop();
s2.pop();
System.out.println("\n--------------------------------\n");

StackLinkedList s3 = new StackLinkedList();
s3.push("cat");
s3.push("dog");
s3.print();
  
System.out.println("top of the stack: "+ s3.peek());
  
s3.pop();
s3.print();
  
s3.push("tiger");
s3.print();
  
s3.pop();
s3.pop();
s3.pop();
}

}

Homework Answers

Answer #1

class Car {

private String make;

public Car(String make) {

this.make = make;

}

public String toString() {

return make;

}

}

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

class Node<T> {

public T data;

public Node next;

public Node(T data) {

this.data = data;

this.next = null;

}

}

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

public class StackLinkedList<T> {

//Single field of type Node

//to represent the front of the stack

Node<T> head;

//operation push()

/*

* The push operation is equivalent to the inserting a node at the head of the

* list.

*

*/

public void push(T data) {

if(head == null)

head = new Node(data);

else {

Node k = new Node(data);

k.next = head;

head = k;

}

}

//operation pop()

/*

* Pop is equivalent to deleting the node at the front

*/

public T pop() throws Exception {

if (head == null) {

// System.err.println("Stack is empty");

// //throw new Exception("Stack is empty");

return null;

}

T returnValue = head.data;

head = head.next;

return returnValue;

}

//operation peek()

/*

* Peek is equivalent to the pop operation but instead of removing the node,

* simply return the data

*/

public T peek() throws Exception {

if (head == null) {

System.err.println("Stack is empty");

throw new Exception("Stack is empty");

}

return head.data;

}

//operation print()

/*

* Walk through the list (stack) and print it out.

*/

public void print() {

Node temp = head;

while (temp != null) {

System.out.print(temp.data + "->");

temp = temp.next;

}

}

//main()

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

/*

* Test cases have been set up to test the program. The stack will be populated

* with different types. You can comment them out when while implementing the

* operations.

*/

StackLinkedList s1 = new StackLinkedList();

s1.push(new Car("Honda"));

s1.push(new Car("BMW"));

s1.print();

System.out.println("top of the stack: " + s1.peek());

s1.pop();

s1.print();

s1.push(new Car("Mercedes"));

s1.print();

s1.pop();

s1.pop();

s1.pop();

System.out.println("\n--------------------------------\n");

StackLinkedList s2 = new StackLinkedList();

s2.push(10);

s2.push(20);

s2.print();

System.out.println("top of the stack: " + s2.peek());

s2.pop();

s2.print();

s2.push(30);

s2.print();

s2.pop();

s2.pop();

s2.pop();

System.out.println("\n--------------------------------\n");

StackLinkedList s3 = new StackLinkedList();

s3.push("cat");

s3.push("dog");

s3.print();

System.out.println("top of the stack: " + s3.peek());

s3.pop();

s3.print();

s3.push("tiger");

s3.print();

s3.pop();

s3.pop();

s3.pop();

}

}

==================================================================
SEE OUTPUT

----------------------------------------------------------------------------------
​​​​​​​Thanks, let me know if you need more information. PLEASE COMMENT if there is any concern.

Know the answer?
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
Similar Questions
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(),...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty); public void push(T element), which...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line),...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line), insert each separate word into the stack, and read/print on the screen. So, if the user enters: This is a test the application output should be: test a is This. This is SLLSTACK.java below package linked_lists; public class SLLStack {    //instance variabels    private SLLNode top;    private int numOfItems;       //constructors    public SLLStack() {        top = null;   ...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT