MyStack.java Implement a stack using linked lists data structure. Implement your
ID: 3748308 • Letter: M
Question
MyStack.java Implement a stack using linked lists data structure. Implement your own linked list. You cannot use Java's java.util.LinkedList. pop0: returns and removes the last value on the stack push(String item): Push a given value onto the stack .isEmpty0: returns true or false depending on if the stack is empty printStack0: prints the items on the stack to console . Handles errors for all possible edge cases (i.e. doesn't throw an unhandled exception to the user) ·MyStack(String[] list): constructor which creates the stack with the items in list on the stack. So if list had {"a", "b", "c"} the stack would look like: "c" on top of "b" on top of "a"Explanation / Answer
SOlution:
StackImplementation.java#############
import java.util.*;
/* Class Node */
class Node
{
protected String data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = null;
}
/* Constructor */
public Node(String d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(String d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public String getData()
{
return data;
}
}
/* Class linkedStack */
class MyStack
{
protected Node top ;
protected int size ;
/* Parameterized Constructor */
public MyStack(String[] list)
{
//Pushing list of the data into the stack
for(int i = 0; i < list.length; i++) {
this.push(list[i]);
}
}
//Default Constructor
public MyStack()
{
top = null;
size = 0;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == null;
}
/* Function to get the size of the stack */
public int getSize()
{
return size;
}
/* Function to push an element to the stack */
public void push(String data)
{
Node nptr = new Node (data, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++ ;
}
/* Function to pop an element from the stack */
public String pop()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
Node ptr = top;
top = ptr.getLink();
size-- ;
return ptr.getData();
}
/* Function to display the status of the stack */
public void printStack()
{
System.out.print(" Stack = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = top;
while (ptr != null)
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
/* Class LinkedStackImplement */
public class StackImplementation
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); //Creating a scanner object to take the input from the user
/* Creating object of class linkedStack */
MyStack ms = new MyStack();
char ch;
do
{
System.out.println(" Linked Stack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. Check Empty");
System.out.println("4. Print Stack");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter a String element to push");
String item = scan.next();
ms.push(item);
break;
case 2 :
try
{
System.out.println("Popped Element = "+ ms.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
System.out.println("Empty status = "+ ms.isEmpty());
break;
case 4 :
System.out.println("Stack = ");
ms.printStack();
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Sample Run:
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
1
Enter a String element to push
Hello
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
1
Enter a String element to push
World
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
1
Enter a String element to push
How
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
1
Enter a String element to push
Are
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
1
Enter a String element to push
You
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
3
Empty status = false
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
4
Stack =
Stack = You Are How World Hello
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
2
Popped Element = You
Do you want to continue (Type y or n)
y
Linked Stack Operations
1. push
2. pop
3. Check Empty
4. Print Stack
4
Stack =
Stack = Are How World Hello
Do you want to continue (Type y or n)
n
Not: If yu have any doubt please do comment below before giving any negative feedback. If you lked the solutiont hen please do hit the thumbs up button.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.