The follwing code has error : 1.option 6 is not displaying anything if it is tri
ID: 666150 • Letter: T
Question
The follwing code has error :
1.option 6 is not displaying anything if it is tried after few insertions followed by some other options like 3, 4, or 5.
2.the deletion is suffering from runtime exception error.
3. Option 3 is suffering run time errors after a couple insertion
Can you correct and debug the errors in the code ?
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.Scanner;
public class LinkedlistQue <T>implements Queue<T>
{
private int total;
private Node front, rear;
public class Node
{
public T ele;
public Node next;
Node()
{
next=null;
}
}
public LinkedlistQue()
{
total=0;
front=null;
rear=null;
}
public LinkedlistQue<T>insert(T ele)
{
Node current = rear;
rear = new Node();
rear.ele= ele;
if(total==0)
{
front=rear;
total=total+1;
}
else
{
current.next= rear;
total+=1;
}
return this;
}
public T delete()
{
if(total !=0)
{
T ele = front.ele;
front = front.next;
if(--total==0) rear=null;
return ele;
}
else
{
System.out.println("there are no items in Queue to delete");
return null;
}
}
public void displayFront()
{
if(total !=0)
{
T ele = front.ele;
System.out.println(ele + "");
}
else
{
.out.println("there are no item in Queue to display");
}
}
public void displayFrontItems()
{
if(total !=0)
{
while(front!=null)
{
T ele = front.ele;
System.out.println(ele + "");
front = front.next;
}
}
else
System.out.println("there are no items in Queue to Display");
}
public void displayRear()
{
if(total !=0)
{
T ele = rear.ele;
System.out.println(ele + "");
}
else
{
System.out.println("there are no item in Queue to display");
}
}
public void displayRearItems()
{
if(total !=0)
{
T ele = rear.ele;
System.out.println(ele + "");= rear.next;
total--;
}
}
{
System.out.println("there are no items in Queue to Display");
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
Node tmp = front;
while (tmp != null)
{
sb.append(tmp.ele).append(",");
tmp = tmp.next;
}
return sb.toString();
}
@Override
public boolean addAll(Collection<? extends T> arg0)
{
return false;
}
@Override
public void clear()
{
}
@Override
public boolean contains(Object arg0)
{
return false;
}
@Override
public boolean containsAll(Collection<?>c)
{
return false;
}
@Override
public boolean isEmpty()
{
return false;
}
@Override
public Iterator<T> iterator()
{
return null;
}
@Override
public boolean remove(Object o)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?>c)
{
return false;
}
@Override
public boolean retainAll(Collection<?>c)
{
return false;
}
@Override
public int size()
{
return 0;
}
@Override
public Object[] toArray()
{
return null;
}
@Override
public<T> T[]toArray(T[]a)
{
return null;
}
@Override
public boolean add(T arg0)
{
return false;
}
@Override
public T element ()
{
return null;
}
@Override
public boolean offer(T arg0)
{
return false;
}
@Override
public T peek()
{
return null;
}
@Override
public T poll()
{
return null;
}
@Override
public T remove()
{
return null;
}
public static void main(String[] args)
{
Queue<String>
que= new LinkedlistQue<>();
Scanner s = new Scanner(System.in);
char ch;
do
{
System.out.println(" Queue Operations");
System.out.println("1. Insert an item to the queue");
System.out.println("2. Delete an item from the queue");
System.out.println("3. Display the item at front");
System.out.println("4. Display the item at rear");
System.out.println("5. Display total number of items currently present in the queue");
System.out.println("6. Print the items currently present in the queue");
System.out.println("7. Quit");
int choice = s.nextInt();
switch (choice)
{
case 1:
{
System.out.println("Please eneter element int the Queue ");
((LinkedlistQue)que).insert(s.nextInt());
break;
}
case 2:
{
System.out.println(" Deleted an item from the queue ");
((LinkedlistQue)que).delete();
break;
}
case 3:
{
System.out.println("Item at the front ");
((LinkedlistQue)que).displayFront();
break;
}
case 4:
{
System.out.println("Item at the rear ");
((LinkedlistQue)que).displayRear();
break;
}
case 5:
{
System.out.println(" Total number of items currently present in the queue are ");
System.out.println(((LinkedlistQue)que).total);
break;
}
case 6:
{
System.out.println("Items currently present in the queue are ");
((LinkedlistQue)que).displayFrontItems();
break;
}
case 7:
{
System.out.println(" Quit GoodBye ");
System.exit(0);
break;
}
default:
{
System.out.println("wrong entry ");
break;
}
}
System.out.println(" Do you want to continue (type y or n) ");
ch =s.next().charAt(0);
}while (ch == 'Y'||ch == 'y');
}
}
Explanation / Answer
I think best you follow this code instead of debugging your code :
Here you go :
import java.util.*;
class arrayQueue
{
protected int Queue[] ;
protected int front, rear, size, len;
/* Constructor */
public arrayQueue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
public boolean isEmpty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return len ;
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
public void insert(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
public int remove()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{
len-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
public void display()
{
System.out.print(" Queue = ");
if (len == 0)
{
System.out.print("Empty ");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}
/* Class QueueImplement */
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test ");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
/* creating object of class arrayQueue */
arrayQueue q = new arrayQueue(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println(" Queue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
try
{
q.insert( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("Removed Element = "+q.remove());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
break;
default : System.out.println("Wrong Entry ");
break;
}
/* display Queue */
q.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Another approach :
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void displayNode()
{
System.out.print(data+" ");
}
}
class LinkList
{
private Node first;
private Node last;
public LinkList()
{
first=null;
last=null;
}
public void insertLast(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
first=newNode;
else
last.next=newNode;
last=newNode;
}
public int deleteFirst()
{
int t=first.data;
if(first.next==null)
last=null;
first=first.next;
return t;
}
public int peekFirst()
{
return(first.data);
}
public boolean isEmpty()
{
return(first==null);
}
public void displayList()
{
Node current=first;
while(current!=null)
{
current.displayNode();
current=current.next;
}
}
}
class Queue
{
private LinkList l;
public Queue()
{
l=new LinkList();
}
public void insert(int x)
{
l.insertLast(x);
System.out.print("Inserted");
}
public int delete()
{
return l.deleteFirst();
}
public boolean isQueueEmpty()
{
return l.isEmpty();
}
public void display()
{
l.displayList();
}
public int peek()
{
return l.peekFirst();
}
}
class QueueApp
{
public static void main(String args[])throws IOException
{
String ch="y";
DataInputStream inp=new DataInputStream(System.in);
int n,d;
Queue q=new Queue();
while(ch.equals("y"))
{
System.out.println("MENU");
System.out.println("--------");
System.out.println("1.Insert");
System.out.println("2.Delete");
System.out.println("3.Peek");
System.out.println("4.Display");
System.out.println("Enter your choice ");
n=Integer.parseInt(inp.readLine());
switch(n)
{
case 1: System.out.println("Enter the data ");
d=Integer.parseInt(inp.readLine());
q.insert(d);
break;
case 2: if(q.isQueueEmpty())
System.out.print("Queue is Empty ");
else
{
d=q.delete();
System.out.print("Deleted data:- "+d);
}
break;
case 3: if(q.isQueueEmpty())
System.out.print("Queue is Empty ");
else
{
d=q.peek();
System.out.print("Deleted data:- "+d);
}
break;
case 4: if(q.isQueueEmpty())
System.out.print("Queue is Empty ");
else
{
System.out.print("Datas in Queue ");
q.display();
}
break;
default: System.out.print("Invalid choice ");
}
System.out.println("");
System.out.print("Enter y to continue ");
ch=inp.readLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.