Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Help In Java: This project (StackWithDoublyLinkedNodes) shows how to create a Stack using doubly linked nodes. It is a template class that needs the

Please Help In Java:

This project (StackWithDoublyLinkedNodes) shows how to create a Stack using doubly linked nodes. It is a template class that needs the Node class. You must use this same Node class (no alterations) to create a Queue class . Use the Stack class as an example.

There is a driver program. This driver program should then run with your Queue class (no modifications allowed to the driver program).

Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue, peek, isEmpty, size, makeEmpty.

StackWithDoublyLinkedNodes:

public static void main(String[] args) { // TODO code application logic here Stack s = new Stack();

int[] x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int i = 0; i < x.length; i++) { Integer t = x[i]; s.push(t); } while (!s.isEmpty()) { System.out.println(s.pop()); } System.out.println(s.pop()); System.out.println(s.peek()); }

}

Stack Class:

public class Stack {

private Node head; private int size;

Stack() { head = null; size = 0; }

public void push(T newItem) { Node temp = new Node(newItem); if (this.isEmpty()) { head = temp; } else { temp.setNext(head); head.setPrev(temp); head = temp; } size++; }

public T pop() { if (isEmpty()) { return (T)"Empty List"; } T temp = (T) head.getItem(); head = head.getNext(); if (head != null) { head.setPrev(null); } size--; return temp; }

public T peek() { if(isEmpty()){ return (T)"Empty List"; } return (T) head.getItem(); }

public int getSize() { return size; }

public boolean isEmpty() { return size == 0; }

}

Node Class: (Do not alter)

public class Node {

private T item; private Node next; private Node prev;

Node(T newItem) { item = newItem; next = null; prev = null; }

Node(T newItem, Node nextNode, Node prevNode) { item = newItem; next = nextNode; prev = prevNode; }

/** * @return the item */ public T getItem() { return item; }

/** * @param item the item to set */ public void setItem(T item) { this.item = item; }

/** * @return the next */ public Node getNext() { return next; }

/** * @param next the next to set */ public void setNext(Node next) { this.next = next; }

/** * @return the prev */ public Node getPrev() { return prev; }

/** * @param prev the prev to set */ public void setPrev(Node prev) { this.prev = prev; }

}

Driver Program:

public static void main(String[] args) { //Create a new queue QueueL myQ = new QueueL(); //Create an array of strings String[] names = {"joe", "mary", "harry", "sadie","tom", "janice"}; //Add them to the queue for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

How would you rate yourself against these criteria?

Answered: 1 week ago