Implement a class for a stack and a queue. Implement as a parent a superclass called “ Container and two subclasses called “ Stack and Queue” How would you use templates on these classes to make in generic?
please I need step by step
class Node<T>{
T data;
Node<T> next;
Node(T val){
this.data=val;
this.next=null;
}
}
class Container<T>{
Node<T> n;
Container(){
this.n=null;
}
}
class Queue<T> extends Container<T>{
Node<T> front, rear;
public Queue() {
this.front = this.rear =
null;
}
// Method to add an key to the queue.
void enqueue(T key)
{
Node<T> temp = new
Node<T>(key);
if (this.rear ==
null)
{
this.front = this.rear = temp;
return;
}
this.rear.next =
temp;
this.rear = temp;
}
// Method to remove an key from queue.
Node<T> dequeue()
{
//return null if queue
is empty
if (this.front ==
null)
return
null;
// Store previous front
and move front one node ahead
Node<T> temp =
this.front;
this.front =
this.front.next;
// If front becomes
NULL, then change rear also as NULL
if (this.front ==
null)
this.rear = null;
//
System.out.println(temp);
return temp;
}
}
class Stack<T> extends Container<T>{
Node<T> Top;
Stack(){
this.Top=null;
}
//Function to check stack is empty or not
public boolean isEmpty()
{
return this.Top ==
null;
}
//Function to get the top element in stack
public Node<T> peek()
{
// check for empty
stack
if (!isEmpty()) {
return this.Top;
}
else {
System.out.println("Stack is empty");
return null;
}
}
public void push(T x) // insert at the beginning
{
// create a temp
node
Node<T> temp = new
Node<T>(x);
//if heap is not full
insert the node
if (temp != null)
{
temp.next = Top;
Top = temp;
} else {
System.out.println("\nOverflow!!!!!! Can't insert into
stack");
}
}
// Function to pop top element from the
stack
public void pop() {
// check for stack
underflow
if (Top != null) {
Top =
Top.next;
} else {
System.out.println("\nUnderflow!!!!!! Cant pop element from
stack");
}
}
}
//main class to test queue and stack
public class TemplateClass {
public static void main(String[] args) {
//creating queue object
Queue<Integer> q1=new
Queue<Integer>();
q1.enqueue(4);
q1.enqueue(6);
q1.enqueue(1);
System.out.println(q1.dequeue().data);
//creating stack object
Stack<Character> s1=new
Stack<Character>();
s1.push('d');
s1.push('a');
s1.push('i');
System.out.println(s1.peek().data);
}
}
//############################### PGM END
#############################
OUTPUT
##########

Implement a class for a stack and a queue. Implement as a parent a superclass called...
This assignment helps you understand Inheritance. We want to create a superclass or parent class called Shape. This shape class will have just one method to setBorderColor. Now we need 3 subclasses that extend the Shape class, Circle, Square and Rectangle. In these classes we will need to compute the area of each of the shapes and print it to the console using System.out.println You will need to take in an additional parameter in each of these subclasses -- side...
Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions called push and pop. Answer the following questions: 1.1) Does the push perform a different action in the stack class then in the queue class? Explain the answer. 1.2) Does the pop perform a different action in the stack class then in the queue class? Explain the answer. Question 2) 2.1) Suppose you are tasked with implementing a reverse queue in which elements are...
plz
write if it is in another class or package
Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...
I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...
Develop a generic stack ADT class by once again implementing the StackInterface.java and use Queue object as underlying ADT(implement stack using queue) Include two constructors: one that sets the capacity at 100 elements, and the other that allows the application level to provide a capacity value as an actual parameter with the constructor call. Also include a toString method. StackInterface : package interfaces; public interface StackInterface<E> { void push(E element); // add an element to the stack - always at...
I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...
Suppose I want to implement the public member functions of a Stack (push, pop, top, size, isEmpty). However, instead of the private member data we had in class, I have only a single Queue. You may assume that the Queue has unbounded capacity, but is otherwise as per the one shown in class. Explain at a high level (you do not need to provide C++ code, but someone should be able to understand how you would code it from what...
I
already created a doubly linked list class. I need help doing this
and adding the doubly linked list class to this. I need this for
Java language if someone can please help me
Step 2 Stack and Queue Using the linked list class you created in Step 1 create stack and queue classes. Iwill leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to...
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...
e. public class Queue { // Uses the correct Stack class from ME2
Ex 19 private Stack mStack; public Queue() { setStack(new Stack());
} public Queue enqueue(E pData) { getStack().push(pData); return
this; } public E dequeue() { return getStack().pop(); } public E
peek() { return getStack.peek(); } private Stack getStack() {
return mStack; } private void setStack(Stack pStack) { mStack =
pStack; } }
f. public class Queue extends Stack { // Uses the correct Stack
class from ME2 Ex...