Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working

JAVA LANG PLEASE:

I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please!

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(String[] 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.

MyTest.java

  • You can have other methods in this class but will be graded to have a:
  • Main method which tests every method you implemented above in MyStack and MyQueue.
  • As you test the methods, print to console (1) what you are testing, (2) what value you are expecting, (3) whether your test passed or failed.
  • Test for edge cases and clearly state in your comment/print out which edge case youre testing.

MyStack.java code:

5 public class MyStack { 6 public static class Node { 7 String data; 8 Node next; 9 } 10 11 private Node head; 12 13 public MyStack(String[] list) { 14 head = null; 15 for (int i = 0; i < list.length; i++) { 16 push(list[i]); 17 } 18 } 19 20 21 public void push(String item) //push a given value onto the stack 22 { 23 Node node = new Node(); 24 node.data = item; 25 26 //checking for edge cases in case empty 27 if(head == null){ 28 head = node; 29 } 30 31 else{ 32 node.next = head; 33 head = node; 34 } 35 } 36 37 public boolean isEmpty() //returns true or false depending on if the stack is empty or not 38 { 39 return head == null; 40 } 41 42 /* public String peek() { 43 return head.data; 44 //System.out.println(); 45 }*/ 46 47 /* public String pop() 48 { 49 if(head !=null){ 50 result= head.data; 51 head= head.next; 52 } 53 return result; */ 54 55 public String pop() //return and removes the last value on the stack 56 { 57 if(head ==null){ 58 System.out.printf("empty"); 59 } 60 61 if (head!= null){ 62 String t = head.data; 63 } 64 65 return head.data; 66 67 } 68 69 public void printStack() { 70 String result = ""; 71 Node temp = head; 72 while (temp != null) { 73 result += temp.data + " "; 74 temp = temp.next; 75 } 76 System.out.println(result); 77 } 78 } 79 80 class MyStackTest { 81 82 public static void main(String[] args) { 83 MyStack stack = new MyStack(new String[] {"a","d", "b", "c"}); 84 System.out.println("The elements of the list are: "); 85 stack.printStack(); 86 System.out.println("pop an element:"); 87 System.out.println(stack.pop());//checking pop method 88 System.out.println("pushing an element:"); 89 stack.push("e"); 90 System.out.println("now the new list is:"); 91 stack.printStack(); 92 System.out.println("now pop last element:"); 93 System.out.println(stack.pop()); 94 } 95 96 }

MyQueue.java code

1 public class MyQueue { 2 3 // Primary stack will always be used for dequeue operations 4 // Secondary stack will be used as helper. 5 private MyStack primary, secondary; 6 7 public MyQueue(String[] list) { 8 primary = new MyStack(new String[] {}); 9 secondary = new MyStack(list); 10 11 while(!secondary.isEmpty()) { 12 primary.push(secondary.pop()); 13 } 14 } 15 16 public void enqueue(String item) { 17 // first make primary empty 18 while(!primary.isEmpty()) { 19 secondary.push(primary.pop()); 20 } 21 primary.push(item); 22 23 // again put items back from secondary 24 while(!secondary.isEmpty()) { 25 primary.push(secondary.pop()); 26 } 27 } 28 public String dequeue() { 29 30 if(isEmpty()) { 31 return null; 32 } 33 34 return primary.pop(); 35 } 36 37 public boolean isEmpty() { 38 return primary.isEmpty(); 39 } 40 41 public void printQueue() { 42 primary.printStack(); 43 } 44 45 public static void main(String[] args) { 46 MyQueue mq = new MyQueue(new String[] {"a", "b", "c"}); 47 mq.printQueue(); 48 System.out.println(mq.dequeue()); 49 mq.enqueue("d"); 50 System.out.println(mq.dequeue()); 51 System.out.println(mq.dequeue()); 52 System.out.println(mq.dequeue()); 53 } 54 55 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

Solve the following 1,4 3 2TT 5x- 1+ (15 x) dx 5X

Answered: 1 week ago