MyStack.java Implement a stack using linked lists data structure. Implement your
ID: 3748880 • 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 printStack): 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 Stringl] 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 eb" on top of a- MyQueue.java Implement a queue using the MyStack.java implementation as your data structure. In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item: inserts item into the queue dequeue: returns and deletes the first element in the queue isEmpty:returns true or false depending on if the queue is empty printQueue): prints the items in the queue to console MyQueue(Stringl] list): constructor which creates the queue with the items in list in the queue: So if list had ['a", "b" "c") the queue would look like: "a" first, then "b", then "c"Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
MyStack.java
------------
public class MyStack {
class Node{
String data;
Node next;
Node(String s, Node nxt){
data = s;
next = nxt;
}
}
private Node top;
public MyStack(){
top = null;
}
public MyStack(String[] list){
for(int i = 0; i < list.length; i++){
push(list[i]);
}
}
public void push(String s){
top = new Node(s, top);
}
public String pop(){
String s = null;
if(!isEmpty()){
s = top.data;
top = top.next;
}
return s;
}
public boolean isEmpty(){
return top == null;
}
public void printStack(){
System.out.print("->");
if(!isEmpty()){
System.out.print(top.data);
Node curr = top.next;
while(curr != null){
System.out.print(", " + curr.data);
curr = curr.next;
}
}
System.out.println();
}
}
MyQueue.java
-------
public class MyQueue {
private MyStack elements;
public MyQueue(){
elements = new MyStack();
}
public MyQueue(String[] list){
elements = new MyStack();
for(int i = 0; i < list.length; i++)
enqueue(list[i]);
}
public void enqueue(String item){
//empty all the elements into temporary stack and then push the new element to bottom
//then push all eleemnts from temporary stack back on to this stack
MyStack temp = new MyStack();
while(!elements.isEmpty())
temp.push(elements.pop());
elements.push(item);
while(!temp.isEmpty())
elements.push(temp.pop());
}
public String dequeue(){
return elements.pop();
}
public boolean isEmpty(){
return elements.isEmpty();
}
public void printQueue(){
elements.printStack();
}
public static void main(String[] args) {
String[] s = {"a", "b", "c"};
MyQueue mq= new MyQueue(s);
mq.dequeue();
mq.printQueue();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.