Question

Create a MyQueue class in Java, similar to what we did when we created the MyStack...

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 testing, not O(1)

{

Node current = start;

while(current != null)

{

System.out.println(current.value);

current = current.next;

}

}

public void push(E val)//O(1)

{

Node newItem = new Node(val);

//if list is empty

if(start == null)

{

start = newItem;

}

else

{

newItem.next = start;

start = newItem;

}

currentCount++;

}

public E pop()//O(1)

{

if(start == null)//empty stack

{

return null;

}

else

{

E val = start.value;

start = start.next;

currentCount--;

return val;

}

}

public E top()//O(1)

{

if(start == null)//empty stack

{

return null;

}

else

{

E val = start.value;

return val;

}

}

public int size()//O(1)

{

return currentCount;

}

private class Node

{

E value;

Node next;

public Node(E v)

{

value = v;

next = null;//no node after this one

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/

class MyQueue<E> {

private Node start;

private int currentCount;

public MyQueue()

{

start = null;

currentCount = 0;

}

public void printQueue()//available for testing, not O(1)

{

Node current = start;

while(current != null)

{

System.out.println(current.value);

current = current.next;

}

}
//method to add element to queue at the end
public void Enqueue(E val)//O(1)

{

Node newItem = new Node(val);

//if list is empty

if(start == null)

{

start = newItem;

}

else

{
Node temp=start;

while(temp.next!=null)
{
temp=temp.next;
}

temp.next=newItem;
}

currentCount++;

}
//deleting first element
public E Dequeue()//O(1)

{

if(start == null)//empty stack

{

return null;

}

else

{

E val = start.value;

start = start.next;

currentCount--;

return val;

}

}

public E top()//O(1)

{

if(start == null)//empty stack

{

return null;

}

else

{

E val = start.value;

return val;

}

}

public int size()//O(1)

{

return currentCount;

}

private class Node

{

E value;

Node next;

public Node(E v)

{

value = v;

next = null;//no node after this one

}

}

}
public class QueueTester {
  
  
public static void main(String argv[])
{
//creating queue object
MyQueue<Integer> m = new MyQueue<Integer>();
//adding to queue
m.Enqueue(1);
m.Enqueue(2);
m.Enqueue(3);
m.Enqueue(4);
System.out.println("Queue:");
m.printQueue();
//deleting from queue
System.out.println("Deleted :"+m.Dequeue());
System.out.println("Deleted :"+m.Dequeue());
System.out.println("Queue:");
m.printQueue();
  
  
}
  
}

output:

run:
Queue:
1
2
3
4
Deleted :1
Deleted :2
Queue:
3
4
BUILD SUCCESSFUL (total time: 1 second)


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.

Add a comment
Know the answer?
Add Answer to:
Create a MyQueue class in Java, similar to what we did when we created the MyStack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add a method to the DoubleLinkedList class built in class to reverse every set of values...

    Add a method to the DoubleLinkedList class built in class to reverse every set of values For example: 1, 2, 3, 4, 5, 6 Reverse 3: 3,2,1,6,5,4 Reverse 2: 2,1,4,3,6,5 Reverse 6: 6,5,4,3,2,1 Method header: public void reverseSegments(int setSize) outcome should be like this: Input:​​​​​​​​​​​​​​ 3 1 2 3 4 5 6 output: 3 2 1 6 5 4 Input:​​​​​​​​​​​​​​​​​​​​​ 2 ​​​​​​​1 2 3 4 5 6 output: 2 1 6 5 4 3 ============================================code====================================================================== public class MyDoubleLinkedList<E> { private...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    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...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    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...

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • I was told I need three seperate files for these classes is there anyway to tie...

    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: ");...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    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...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    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...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    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....

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • JAVA language Add a recursive method to the program shown in the previous section that states...

    JAVA language Add a recursive method to the program shown in the previous section that states how many times a particular value appears on the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT