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.
MyTest.java
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 }
MyQueue.java
import java.util.Arrays;
public class MyQueue {
// Primary stack will always be used for dequeue operations
// Secondary stack will be used as helper.
private MyStack primary, secondary;
public MyQueue(String[] list) {
primary = new MyStack(new String[]{});
secondary = new MyStack(list);
//System.out.println( secondary.isEmpty() );
while (!secondary.isEmpty()) {
primary.push(secondary.pop());
}
//System.out.println( primary.isEmpty() );
}
public void enqueue(String item) {
// first make primary empty
while (!primary.isEmpty()) {
secondary.push(primary.pop());
}
primary.push(item);
// again put items back from secondary
while (!secondary.isEmpty()) {
primary.push(secondary.pop());
}
}
public String dequeue() {
if (isEmpty()) {
return null;
}
return primary.pop();
}
public boolean isEmpty() {
return primary.isEmpty();
}
public void printQueue() {
primary.printStack();
}
public static void main(String[] args) {
String[] testArray = new String[]{"a","b","c"};
MyQueue mq = new MyQueue(testArray);
mq.printQueue();
System.out.println(mq.dequeue());
mq.enqueue("d");
System.out.println(mq.dequeue());
System.out.println(mq.dequeue());
System.out.println(mq.dequeue());
}
}
MyStack.java
class MyStack {
public static class Node {
String data;
Node next;
}
private Node head;
MyStack(String[] list) {
head = null;
for (String aList : list) {
push(aList);
}
}
void push(String item) //push a given value onto the stack
{
Node node = new Node();
node.data = item;
//checking for edge cases in case empty
if (head == null) {
head = node;
} else {
node.next = head;
head = node;
}
}
boolean isEmpty() //returns true or false depending on if the stack is empty or not
{
return head == null;
}
String pop() //return and removes the last value on the stack
{
if (head == null) {
System.out.print("empty");
}
/** here is the change which you were missing
* you were not changing the head therefore it went's into infinite loop
* also you were sending head.data instead of String variable t
*
*/
String t = "";
if (head != null) {
t = head.data;
head = head.next;
}
return t;
}
void printStack() {
StringBuilder result = new StringBuilder();
Node temp = head;
while (temp != null) {
result.append(temp.data).append(" ");
temp = temp.next;
}
System.out.println(result);
}
}
class MyStackTest {
public static void main(String[] args) {
MyStack stack = new MyStack(new String[]{"a", "d", "b", "c"});
System.out.println("The elements of the list are: ");
stack.printStack();
System.out.println("pop an element:");
System.out.println(stack.pop());//checking pop method
System.out.println("pushing an element:");
stack.push("e");
System.out.println("now the new list is:");
stack.printStack();
System.out.println("now pop last element:");
System.out.println(stack.pop());
}
}
//Look at String pop() method in at line 42
//small thing which you were missing
//Here's the output
a b c
a
b
c
d
JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...
Create a MyQueue class in Java, similar to what we did when we created the MyStack class. Include a test class to show everything works correctly. Include all the methods mentioned in the power points that are normally included in a Queue. This is MyStack class we did in a class. Have to make Queue class. public class MyStack<E> { private Node start; private int currentCount; public MyStack() { start = null; currentCount = 0; } public void printStack()//available for...
The class pictured below is designed to implement an integer
queue using two stacks. Assume the stack methods all work as
desired (though their implementations are not shown).
.(a) Trace what happens in the following situation, showing
intermediate steps (values of variables, what is in the stacks, and
what is in the queue at various points in the methods, not just the
results of the methods).
• Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue
twice....
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
Here's the problem that I have to solve: Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression. What I'm having trouble is getting it to reading and calculating it as postfix Here's my code: import java.util.Scanner; public class Assignment2 { public static void main(String[] args) { Scanner scan = new...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...