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();
}
}
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.
Get Answers For Free
Most questions answered within 1 hours.