Step 2 Stack and Queue Using the linked list class you creat

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

Solution

/**************************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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site