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

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

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

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

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

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

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