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.
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;   ...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
This is based on LinkedLists. There is a type mismatch in my first method and I...
This is based on LinkedLists. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch public class Node {    public T info; public Node link; public Node(T i, Node l) {    info = i; link = l;    } } class LinkedList> {    protected Node head = null; public LinkedList add(T el) { head = new Node(el, head); return this;...