Question

In: Computer Science

Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...

Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively.

Your main method should be as follow:

public static void main(String args[]) {

xxxxx dq = new xxxxx (); // xxxxx is the first 5 characters of your last name

try{

Scanner inf = new Scanner (System.in);                    

// Read input from KB/ File

while(inf.hasNext()){

// read operation type (optype):

//1:add front, 2:delete front, 3:add rear and delete rear            

optype = inf.nextInt();

       switch (optype){

                             case 1: x = inf.nextInt(); // read x

                                       dq.addfront(x);

                    dq.printfwd(); // print forward dequeuer elements

                                       break;

                             case 2: j = dq.delfront();// delete front

                                       if(j……….

                                       dq.printbwd(); // print backward dequeuer elements

                                       break;

                             case 3: x = inf.nextInt(); // read x

                                       dq.addrear(x);

                    dq.printfwd(); // print forward dequeuer elements

                                       break;

                             default: j = dq.delrear();// delete front

                                       if(j……….

                                       dq.printbwd(); // print backward dequeuer elements

       }//end switch

}// end while

inf.close();// close input file

}catch (Exception e) {prt("\nException " + e + "\n");}

}// end main method      

NOTE: Projects will not be graded if:

Your complete project is not in a SINGLE xxxxx.java file, where xxxxx is the first 5 characters of your last name.

It does not read input from System.in. i.e. command prompt.

Sample data file can be as follow:

1 100 2 2 3 500 3 600 3 250 4 1 150 1 220 2 4 1 50 3 55 1 20 1 80 3 90

Solutions

Expert Solution

Java Code:

package queue;

import java.util.Scanner;

import queue.Deque.DoublyLinkedListDeque.Node;


public class Deque {
public static void main(String args[]) { //main class
DoublyLinkedListDeque dq = new DoublyLinkedListDeque(); //create a queue object
try {
Scanner inf = new Scanner(System.in);
while (inf.hasNext()) { //loop through the input
int optype = inf.nextInt(); //store option
int x;
   Node j;
   switch (optype) { //check which option to excute
case 1:
x = inf.nextInt(); //get the number
dq.addfront(x); //call add to front
dq.printfwd(); //call print from front to rear
break;
case 2:
j = dq.delfront(); //delete the node at the front
if (j != null) {
   dq.printbwd(); //if successful print from rear to front
}
break;
case 3:
x = inf.nextInt(); //get the number
dq.addrear(x); //add to rear
dq.printfwd(); //call print from front to rear
break;
default:
j = dq.delrear(); //delete rear node
if (j != null) {
   dq.printbwd(); //if successful print from rear to front
}
}
}
inf.close();
} catch (Exception e) {
System.out.println("\nException " + e + "\n"); //catch any exception thrown
}
}

public static class DoublyLinkedListDeque { //creating a queue class
private Node head; //to store the head of the queue
private Node tail; //to store the tail of the queue

class Node { //creating a class node
int data;
Node next; //to reference the next element in the queue
Node prev; //to reference the previous element in the queue
Node(int data) {
   this.data = data; //contructor to create a new node
}
}
public DoublyLinkedListDeque() { //set the head and tail to null at the beginning
this.head = null;
this.tail = null;
}

public boolean isEmpty() { //check if queue is empty
return head == null;
}

public void printfwd() { //method to print from front to rear
   System.out.println("Printing front to rear");
Node currentNode = head;
while (currentNode != null) {
System.out.print(currentNode.data + " "); //print the node
currentNode = currentNode.next;
}
System.out.println("");
System.out.println("");
}

public void printbwd() { //method to print from rear to front
   System.out.println("Printing rear to front");
Node currentNode = tail;
while (currentNode != null) {
System.out.print(currentNode.data + " "); //print the node
currentNode = currentNode.prev;
}
System.out.println("");
System.out.println("");
}

public void addfront(int data) { //method to add to front of the queue
Node newNode = new Node(data);
if (isEmpty()) {
tail = newNode; //if queue is empty tail should should point to the head
} else {
head.prev = newNode; //else set the newNode at the previous position of the current head
newNode.next = head;
}
System.out.println("Adding "+ data +" at front");
System.out.println("");
head = newNode; //set the new node as the head
}

public void addrear(int data) { //method to add to the rear of the queue
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode; //if queue is empty head should point to the tail
} else {
tail.next = newNode; //else set the newNode at the next position of the current tail
newNode.prev = tail;
}
System.out.println("Adding "+ data +" at rear");
System.out.println("");
tail = newNode; //set the new node as the tail
}

public Node delfront() { //method to delete the node at the front
   System.out.println("Deleting at front");
   System.out.println("");
if (head == null) {
System.out.println("Deque is empty");
System.out.println("");
return null; //if queue is empty return null
}
Node first = head; //get the head
if (head.next == null) {
tail = null; //if there is only one node in the queue set the tail as null
} else {
head.next.prev = null; //else set the previous of the next element of the head as null (this is to deference the head)
}
head = head.next; //move the head to the next position
return first;
}

public Node delrear() { //method to delete the node the rear
   System.out.println("Deleting at rear");
   System.out.println("");
if (tail == null) {
System.out.println("Deque is empty");
System.out.println("");
return null; // if queue is empty return null
}
Node last = tail; //get the tail
if (head.next == null) {
head = null; //if there is only one node in the queue set the head as null
} else {
tail.prev.next = null; //set the next node of the previous of the tail as null (this is to deference the tail)
}
tail = tail.prev; //move the tail to the previous position
return last;
}


}
}

Sample Output:

1 100 2 2 3 500 3 600 3 250 4 1 150 1 220 2 4 1 50 3 55 1 20 1 80 3 90
Adding 100 at front

Printing front to rear
100

Deleting at front

Printing rear to front


Deleting at front

Deque is empty

Adding 500 at rear

Printing front to rear
500

Adding 600 at rear

Printing front to rear
500 600

Adding 250 at rear

Printing front to rear
500 600 250

Deleting at rear

Printing rear to front
600 500

Adding 150 at front

Printing front to rear
150 500 600

Adding 220 at front

Printing front to rear
220 150 500 600

Deleting at front

Printing rear to front
600 500 150

Deleting at rear

Printing rear to front
500 150

Adding 50 at front

Printing front to rear
50 150 500

Adding 55 at rear

Printing front to rear
50 150 500 55

Adding 20 at front

Printing front to rear
20 50 150 500 55

Adding 80 at front

Printing front to rear
80 20 50 150 500 55

Adding 90 at rear

Printing front to rear
80 20 50 150 500 55 90


Related Solutions

In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT