Question
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 explain why you chose the relationship type you did. The stack class should have the following interface push pop The queue should have the following interface enqueue de queue peek Again I will leave it up to you as to the parameters to the functions and the return type. As usual please have good reasoning for why you have been doing things
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**************************Node.java***********************/


public class Node {
   /**
   * variable declarations
   */
   int data;
   Node next;
   Node prev;

   public Node(int data) {
       this.data = data;
       this.next = null;
       this.prev = null;

   }
}

/*****************************Stack.java***********************/


public class Stack {

   private Node head = null;

   /**
   * pushing element into stack. We are adding element at beginning in list
   *
   * @param data
   */
   public void push(int data) {
       Node temp = new Node(data);
       if (head == null) {
           head = temp;
       } else {
           temp.next = head;
           head.prev = temp;
           head = temp;
       }
   }

   /**
   * pop method implementation
   *
   * @return int
   */
   public int pop() {
       Node temp = head;
       head = head.next;
       return temp.data;
   }

   /**
   * peek method implementation return top element means first element from
   * list
   *
   * @return int
   */
   public int peek() {

       return head.data;
   }

   public static void main(String[] args) {
       Stack stack = new Stack();
       /**
       * Inserting element into stack
       */
       stack.push(10);
       stack.push(20);
       stack.push(30);
       stack.push(40);
       stack.push(50);
       /**
       * peek into stack
       */
       System.out.println("Peek element: " + stack.peek());
       /**
       * pop one element
       */
       System.out.println("Popped Element: " + stack.pop());
       /**
       * Peek Element
       */
       System.out.println("Peek element: " + stack.peek());

   }
}

/****************************Output***********************/

Peek element: 50
Popped Element: 50
Peek element: 40

/**********************************Queue.java***************************/

public class Queue {
   /**
   * variable declaration
   */
   private Node head = null;
   private Node front = null;
   private Node rear = null;

   /**
   * pushing element into stack. We are adding element at beginning in list
   *
   * @param data
   */
   public void enqueue(int data) {
       Node temp = new Node(data);
       if (head == null) {
           head = temp;
           front = temp;
           rear = temp;
       } else {
           Node curr = head;
           while (curr.next != null) {
               curr = curr.next;
           }
           curr.next = temp;
           temp.prev = curr;
           rear = temp;
       }
   }

   /**
   * pop method implementation
   *
   * @return int
   */
   public int dequeue() {
       Node temp = head;
       head = head.next;
       front = head;
       return temp.data;
   }

   /**
   * peek method implementation return top element means first element from
   * list
   *
   * @return int
   */
   public int peek() {

       return front.data;
   }

   public static void main(String[] args) {
       Queue queue = new Queue();
       /**
       * Inserting element into stack
       */
       queue.enqueue(10);
       queue.enqueue(20);
       queue.enqueue(30);
       queue.enqueue(40);
       queue.enqueue(50);
       /**
       * peek into stack
       */
       System.out.println("Peek element: " + queue.peek());
       /**
       * pop one element
       */
       System.out.println("Dequeue Element: " + queue.dequeue());
       /**
       * Peek Element
       */
       System.out.println("Peek element: " + queue.peek());

   }
}

/***********************************output*************************/

Peek element: 10
Dequeue Element: 10
Peek element: 20

Thanks a lot

Add a comment
Know the answer?
Add Answer to:
I already created a doubly linked list class. I need help doing this and adding the...
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
  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

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

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

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

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirstmethod for...

    Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirstmethod for a list of size N? Group of answer choices O(1) O(log N) O(N) O(N log N) Flag this Question Question 21 pts Java's ArrayList class represents a basic array. As a convenience for the user, when the capacity of the backing array is exceeded, the class handles creating a new larger array and copying over the existing items. Its add(int index, E element) method...

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

  • Using C++ language, Design and implement a class representing a doubly linked list. The class must...

    Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

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