Question

Implement a class for a stack and a queue. Implement as a parent a superclass called...

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

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

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

<terminated> Templat 4 1

Add a comment
Know the answer?
Add Answer to:
Implement a class for a stack and a queue. Implement as a parent a superclass called...
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
  • This assignment helps you understand Inheritance. We want to create a superclass or parent class 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...

  • plz write if it is in another class or package Question 1: 1. Create a new...

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

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

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

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

  • I already created a doubly linked list class. I need help doing this and adding the...

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

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

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

  • (JAVA) In this assignment, you will implement a superclass Appointment and subclasses Onetime, Daily, and Monthly....

    (JAVA) In this assignment, you will implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date. Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the mon.th matches. In your application part, fill an array of Appointment objects with a mixture of appointments. Have the...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

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
ADVERTISEMENT