June 09, 2012

DS: Circular Queue

import java.util.Scanner;

public class CQueue {
 public static void main(String args[]) {

  Scanner s = new Scanner(System.in);
  int choice, n;
  Object value;
  System.out.println("Enter a size");
  n = s.nextInt();

  List c = new List(n);
  do {
   System.out
     .println("CQueue operations\n1.puts()\n2.remove()\n3.getfrontelement()\n4.getrearelement()\n5.empty()\n6.size()\n7.display()\nEnter ur choice");
   choice = s.nextInt();
   switch (choice) {
   case 1:
    System.out.println("Enter a value");
    value = s.nextInt();
    c.puts(value);
    break;
   case 2:
    System.out.println("The deleted element is=" + c.remove());
    break;
   case 3:
    System.out.println("The front element is=" + c.front_element());
    break;
   case 4:
    System.out.println("The rear element is=" + c.rear_element());
    break;
   case 5:
    if (c.isempty())
     System.out.println("CQueue is not empty");
    else
     System.out.println("CQueue is not empty");
    break;
   case 6:
    System.out.println("The size is=" + c.size());
    break;
   case 7:
    System.out.println("The elements in the queue is=" + c);
    break;
   }
  } while (choice < 8);

 }
}

class List {
 int front, rear;
 Object x[];
 int size = 0;

 public List(int y) {
  front = -1;
  rear = -1;
  x = new Object[y];

 }

 public int size() {
  return size;
 }

 public boolean isempty() {
  return front == -1;
 }

 public void puts(Object value) {

  Scanner s = new Scanner(System.in);
  if (rear == x.length - 1)
   rear = 0;
  else
   rear++;
  if (front == rear) {
   if (rear == 0)
    rear = x.length - 1;
   else
    rear--;

   System.out.println("Queue is overflow");
   return;
  } else {
   if (front == -1)
    front = 0;
   x[rear] = value;
  }
  size++;
 }

 public Object remove() {

  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;
  }
  Object value = x[front];
  x[front] = null;
  if (front == rear) {
   front = rear = -1;

  } else {
   if (front == x.length - 1)
    front = 0;
   else
    front++;
  }
  size--;
  return value;

 }

 public String toString() {
  StringBuffer y = new StringBuffer("[");
  if (front < rear) {
   for (int i = front; i <= rear; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + " ");

   }
  } else {
   for (int i = front; i < x.length; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + " ");

   }

   for (int i = 0; i <= rear; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + ", ");

   }

  }
  y.append("]");
  return new String(y);

 }

 public Object front_element() {
  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;

  }
  return x[front];

 }

 public Object rear_element() {
  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;

  }
  return x[rear];

 }
}
Output:

Enter a size                                                                   
5                                                                               
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                      
3.getfrontelement()                                                            
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                    
Enter ur choice                                                               
 1                                                                               
Enter a value                                                                  
10                                                                             
CQueue operations                                                               
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                             
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                        
7.display()                                                                    
Enter ur choice                                                                
1                                                                               
Enter a value                                                                 
 20                                                                             
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                            
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                     
Enter ur choice                                                                
1                                                                              
Enter a value                                                                   
30                                                                             
CQueue operations                                                              
1.puts()                                                                        
2.remove()                                                                     
3.getfrontelement()                                                            
4.getrearelement()                                                              
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                    
Enter ur choice  
1
Enter a value
40
CQueue operations
1.puts()
2.remove()
3.getfrontelement()
4.getrearelement()
5.empty()
6.size()
7.display()
Enter ur choice
2
The deleted element is=10                                                      
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                            
 4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                        
7.display()                                                                   
Enter ur choice  
8   

1 comment:

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts