(java)
Please implement your own Dequeue class which has following methods
•boolean add(E e)= void addLast(E e) // two methods are the same. You could implement either one
•
•void addFirst(E e)
•
•E getFirst( ) = E peek( ) // two methods are the same. You could implement either one
•E getLast( )
•E removeFirst()
•E removeLast()
Case 1 : Queue
•Create a queue which is an instance of Dequeue. The queue should perform with given following input and print the correct output.
•Input format
•The first line contains an integer of q :
•q specifies the number of queries which follows in the next lines
•Each of the q next lines contains a single query in the form of one of following 3 types :
•1 x: Enqueue element into the end of the queue.
•2: Dequeue the element at the front of the queue.
•3: Print the element at the front of the queue.
Case 2. Stack
•Create a a stack which is an instance of Dequeue. The stack should perform with given following input and print the correct output.
•Input format
•Any string of postfix expression
•Example “34+78+*”
•Output
•The value of evaluation
•105
Program:
import java.util.*;
import java.io.*;
//Dequeue class
class Dequeue<E>
{
//data members
private int first;
private int last;
private int size;
private E dq[];
//static data members
static int MAX = 10;
//constri=uctor
public Dequeue()
{
first = -1;
last = -1;
size = 0;
dq = (E[]) new Object[MAX];
}
//add an item to the last
void addLast(E e) throws IOException
{
if(size==MAX)
throw new
IOException();
size++;
if(last==-1)
{
first = last =
0;
}
else
last =
(last+1)%size;
dq[last] = e;
}
//add an item to the first
void addFirst(E e) throws IOException
{
if(size==MAX)
throw new
IOException();
size++;
if(first==-1)
{
first = last =
0;
}
else if (first == 0)
first = size-1;
else
first = first-1;
dq[first] = e;
}
//return an item from the first
E getFirst() throws IOException
{
if(size==0)
throw new
IOException();
return dq[first];
}
//return an item from the last
E getLast() throws IOException
{
if(size==0)
throw new
IOException();
return dq[last];
}
//remove an item from the first
E removeFirst() throws IOException
{
if(size==0)
throw new
IOException();
E item = dq[first];;
if(first==last)
{
first = last =
-1;
}
else if(first == size-1)
{
first=0;
}
else
first= first
+1;
return item;
}
//remove an item from the last
E removeLast() throws IOException
{
if(size==0)
throw new
IOException();
E item = dq[last];;
if(first==last)
{
first = last =
-1;
}
else if(last == 0)
{
last =
size-1;
}
else
last =
last-1;
return item;
}
}
//class Tester
class Tester
{
//method to test case 1 for queue operations
static void queueCaseOne() throws IOException
{
//create a queue
Dequeue<Integer> queue = new
Dequeue<Integer>();
//create an instance of Scanner
class
Scanner sc = new
Scanner(System.in);
//number of queries
System.out.println ("Enter number
of queries");
int q = sc.nextInt();
//execute q number of queries
for(int i=0; i<q; i++)
{
System.out.println ("1. Enqueue element into the end of the
queue.");
System.out.println ("2. Dequeue the element at the front of the
queue.");
System.out.println ("3. Print the element at the front of the
queue.");
int op =
sc.nextInt();
int x;
switch(op)
{
case 1:
x = sc.nextInt();
queue.addLast(x);
break;
case 2:
x =
queue.removeFirst();
System.out.println (x + " is
removed");
break;
case 3:
x = queue.getFirst();
System.out.println ("The
element at the front is " + x);
break;
}
}
}
//method to test case 2 for stack operations
static void stackCaseTwo() throws IOException
{
//create a stack
Dequeue<Integer> stack = new
Dequeue<Integer>();
//postfix expression
String postfix = "34+78+*";
//evaluate postfix expression using
stack
for(int i=0; i<postfix.length();
i++)
{
char ch =
postfix.charAt(i);
//check for
operand
if(Character.isLetterOrDigit(ch))
{
stack.addLast(ch - '0');
}
//when operator
found
else
{
//pop two items from the stack
int y = stack.removeLast();
int x = stack.removeLast();
//perform operation
switch(ch)
{
case '+':
stack.addLast(x+y);
break;
case '-':
stack.addLast(x-y);
break;
case '*':
stack.addLast(x*y);
break;
case '/':
stack.addLast(x/y);
break;
}
}
}
//display the output
System.out.println ("Output = " +
stack.removeLast());
}
//main method
public static void main (String[] args) throws
IOException
{
System.out.println ("Test case 1
for queue operations:\n");
queueCaseOne();
System.out.println ("\n\nTest case
2 for stack operations:\n");
stackCaseTwo();
}
}
Output:
Test case 1 for queue operations:
Enter number of queries
5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
1 5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
1 7
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
3
The element at the front is 5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
2
5 is removed
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
2
7 is removed
Test case 2 for stack operations:
Output = 105
(java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...
My CSC 220 teacher has given this as a homework assignment and starting it has not made much sense and am generally lost on this assignment help would be much appreciated. I put everything from the assignment power point slides she gave us in here so the expert has everything im working with as well. Dequeue Please implement your own Dequeue class which has following methods boolean add(E e)= void addLast(E e) void addFirst(E e) E getFirst( ) = E...
C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...
In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
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...
LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as explained in class. Refer to the class slides on linked lists. Specifically, implement your generic classes as: public class LinkedListNode { public E element; public LinkedListNode next; } public class LinkedListBox { public LinkedListNode head; public LinkedListNode tail; public int count = 0; } Within the LinkedListBox class, implement the following methods: public boolean add(E e). Returns true after adding an element to the...
Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...
In Java. What would the methods of this class look like?
StackADT.java
public interface StackADT<T>
{
/** Adds one element to the top of this stack.
* @param element element to be pushed onto stack
*/
public void push (T element);
/** Removes and returns the top element from this stack.
* @return T element removed from the top of the stack
*/
public T pop();
/** Returns without removing the top element of this
stack.
* @return T...
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...
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...