How do i get this program to out put \"Empty List\" when printing forward or bac
ID: 3691738 • Letter: H
Question
How do i get this program to out put "Empty List" when printing forward or backwards when the list is has no values in it?
help please!
import java.io.*;
import java.util.*;
public class DLinkList
{
private class DNode
{
private DNode next, prev;
private int data;
}
private DNode head, tail, newNode, current;
private int size;
/*---------------------------------------------------
Insert
----------------------------------------------------*/
public void insert(int i)
{
newNode = new DNode();
newNode.data = i;
if (head == null)
{
head = newNode;
tail = newNode;
}
else
{
current = head;
if(current.data >= i)
{
newNode.next = current;
current.prev = newNode;
head = newNode;
}
else
{
while(current.next != null && current.next.data < i)
current = current.next;
if(current == tail)
{
newNode.prev = current;
current.next = newNode;
tail = newNode;
}
else
{
newNode.prev = current;
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
}
}
}
size++;
}//end insert
/*---------------------------------------------------
Delete
----------------------------------------------------*/
public void delete(int i)
{
if(head == null)
{
System.out.println("Empty List");
return;
}
else
{
if(head.data == i)
{
if(head == tail)
{
head = null;
tail = null;
}
else
{
head = head.next;
head.prev = null;
}
}
else
{
if(head.data > i)
{
System.out.println("Value not Found");
return;
}
else
{
current = head;
while(current.next != null && current.next.data < i)
current = current.next;
if(current == tail)
{
System.out.println("Value not Found");
return;
}
else
{
if(current.next.data == i)
{
if(current.next == tail)
{
current.next = null;
tail = current;
}
else
{
current.next = current.next.next;
current.next.prev = current;
}
}
else
{
System.out.println("Value not Found");
return;
}
}
}
}
}
size--;
}//end method delete
/*------------------------------
boolean isEmpty
-------------------------------*/
public boolean isEmpty()
{
return head.next == null;
} //end empty
/*------------------------------
getSize
-------------------------------*/
public int getSize()
{
return size;
}//end size
/*------------------------------
printing
-------------------------------*/
public void printForward()
{
DNode newNode = head;
while (newNode !=null)
{
System.out.print(newNode.data+" ");
newNode = newNode.next;
}
System.out.println();
}//end printforward()
public void printBackward()
{
DNode newNode = tail;
while (newNode != null)
{
System.out.print(newNode.data + " ");
newNode = newNode.prev;
}
System.out.println();
}//end printBackward
}//end DLinkList class
/*--------------------------------------
Demo
----------------------------------------*/
class dLinkDemo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
DLinkList list = new DLinkList();
boolean continueInput = true;
do
{
System.out.println(" Sorted double link list");
System.out.println("a. Add");
System.out.println("d. Delete");
System.out.println("p. Print forward");
System.out.println("r. Print backward");
System.out.println("q. Quit");
String choice = scan.next().toUpperCase();
char ch = choice.charAt(0);
switch(ch)
{
case 'A':
System.out.println("Enter integer element to insert");
try
{
list.insert(scan.nextInt());
}
catch(InputMismatchException e)
{
System.out.println("That is not a number, do you think this is a game? Input a valid integer: ");
}
break;
case 'D' :
System.out.println("Enter position");
try
{
int i = scan.nextInt() ;
list.delete(i);
}
catch(InputMismatchException e)
{
System.out.println("That is not a number, do you think this is a game? Input an interger: ");
}
break;
case 'P' :
System.out.println("Display in ascending order: ");
list.printForward();
break;
case 'R' :
System.out.println("Display in reverse order: ");
list.printBackward();
break;
case 'Q' :
System.exit(1);
continueInput = false;
break;
default :
System.out.println("Invalid entry, try agin.");
break;
}
}//do
while (continueInput);
}//end main
}//end class
Explanation / Answer
Hello there ,
It is quite simple .
When you are going to print forward , check whether "head" is null , if null print "Empty List" .
and when you are going to print backward,check whether "tail" is null , if null print "Empty List" .
Please find below code and i have just changed printForward and printBackward methods only .
Let me know if you have any doubts.
Thanks.
import java.io.*;
import java.util.*;
class DLinkList {
private class DNode
{
private DNode next, prev;
private int data;
}
private DNode head, tail, newNode, current;
private int size;
/*---------------------------------------------------
Insert
----------------------------------------------------*/
public void insert(int i) {
newNode = new DNode();
newNode.data = i;
if (head == null) {
head = newNode;
tail = newNode;
} else {
current = head;
if (current.data >= i) {
newNode.next = current;
current.prev = newNode;
head = newNode;
} else {
while (current.next != null && current.next.data < i)
current = current.next;
if (current == tail) {
newNode.prev = current;
current.next = newNode;
tail = newNode;
} else {
newNode.prev = current;
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
}
}
}
size++;
}// end insert
/*---------------------------------------------------
Delete
----------------------------------------------------*/
public void delete(int i) {
if (head == null) {
System.out.println("Empty List");
return;
} else {
if (head.data == i) {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
head.prev = null;
}
} else {
if (head.data > i) {
System.out.println("Value not Found");
return;
} else {
current = head;
while (current.next != null && current.next.data < i)
current = current.next;
if (current == tail) {
System.out.println("Value not Found");
return;
} else {
if (current.next.data == i) {
if (current.next == tail) {
current.next = null;
tail = current;
} else {
current.next = current.next.next;
current.next.prev = current;
}
}
else {
System.out.println("Value not Found");
return;
}
}
}
}
}
size--;
}// end method delete
/*------------------------------
boolean isEmpty
-------------------------------*/
public boolean isEmpty() {
return head.next == null;
} // end empty
/*------------------------------
getSize
-------------------------------*/
public int getSize() {
return size;
}// end size
/*------------------------------
printing
-------------------------------*/
public void printForward() {
if (head != null) {
DNode newNode = head;
while (newNode != null) {
System.out.print(newNode.data + " ");
newNode = newNode.next;
}
System.out.println();
} else {
System.out.println("Empty List");
}
}// end printforward()
public void printBackward() {
if (tail != null) {
DNode newNode = tail;
while (newNode != null) {
System.out.print(newNode.data + " ");
newNode = newNode.prev;
}
System.out.println();
} else {
System.out.println("Empty List");
}
}// end printBackward
}// end DLinkList class
/*--------------------------------------
Demo
----------------------------------------*/
public class dLinkDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
DLinkList list = new DLinkList();
boolean continueInput = true;
do {
System.out.println(" Sorted double link list");
System.out.println("a. Add");
System.out.println("d. Delete");
System.out.println("p. Print forward");
System.out.println("r. Print backward");
System.out.println("q. Quit");
String choice = scan.next().toUpperCase();
char ch = choice.charAt(0);
switch (ch) {
case 'A':
System.out.println("Enter integer element to insert");
try {
list.insert(scan.nextInt());
}
catch (InputMismatchException e) {
System.out.println("That is not a number, do you think this is a game? Input a valid integer: ");
}
break;
case 'D':
System.out.println("Enter position");
try {
int i = scan.nextInt();
list.delete(i);
}
catch (InputMismatchException e) {
System.out.println("That is not a number, do you think this is a game? Input an interger: ");
}
break;
case 'P':
System.out.println("Display in ascending order: ");
list.printForward();
break;
case 'R':
System.out.println("Display in reverse order: ");
list.printBackward();
break;
case 'Q':
System.exit(1);
continueInput = false;
break;
default:
System.out.println("Invalid entry, try agin.");
break;
}
}// do
while (continueInput);
}// end main
}// end clas
======== See this portion>
/*------------------------------
printing
-------------------------------*/
public void printForward() {
if (head != null) {
DNode newNode = head;
while (newNode != null) {
System.out.print(newNode.data + " ");
newNode = newNode.next;
}
System.out.println();
} else {
System.out.println("Empty List");
}
}// end printforward()
public void printBackward() {
if (tail != null) {
DNode newNode = tail;
while (newNode != null) {
System.out.print(newNode.data + " ");
newNode = newNode.prev;
}
System.out.println();
} else {
System.out.println("Empty List");
}
}// end printBackward
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.