Showing posts with label DS. Show all posts
Showing posts with label DS. Show all posts

Tuesday, December 22, 2015

Write an algorithm for the double ended queue (DEQUE) ?

Solution:-
 
v  INSERTION OF ITEM
ü  AT REAR END
             Insrear (queue [maxsize ],item , front , rear )
Step1:-        if (rear == maxsize -1)
                               printf “  QUEUE OVERFLOW “
                     Other wise
                               Read item
                               If ( front ==rear== -1)
                               {
                                          Set rear= -1
                                          Set front = -1
                               }
                               Other wise
                                          Set rear = rear + 1
                              End if
                              Set  queue[rear]=item
                              print “ SUCCESS INSERT ITEM “
                     End if
Step2:-       stop.
 
ü  AT FRONT END
 
          Insfront (queue [maxsize ] , item , front , rear )
Step1:-       if(front == 0)
                            print “ QUEUE OVERFLOW “
                    else
                            Read item
                            front = front -1
                            queue[front]=item
                   End if
Step2:-      stop
 
v  DELETION OF ITEM
 
ü  DELETION FROM FRONT END
 
          Delfront (queue [maxsize ] , item , front , rear )
Step1:-        if(front == -1)
                             print “ QUEUE UNDERFLOW “
                     else
                             Set   item = queue [front]
                             if(rear == front)
                                         Set    front = -1
                                         Set   rear = -1
                             else
                                         Set   front = front +1
                             End if
                             Print “ SUCCESS DELETE ITEM “
                      End if
Step2:-         stop.
 
ü  DELETION FROM REAR END
 
Delrear (queue[maxsize] , item , front , rear)
Step1:-       if(rear == -1)
                           print “ QUEUE UNDERFLOW “
                   else
                           item = queue[rear]
                           if(rear == front)
                           {
                                      Set  front = -1
                                      Set  rear = -1
                           }
                           else
                                      rear = rear -1
                           End if
                   End if
Step2:-      stop
 
ü      DISPLAY  FUNCTION
Display (queue[maxsize ] , i , rear , front)
Step1:-       if(rear == -1)
                           print  “ NO ITEM”
                   else
                           for(i=front; i<=rear; i++)
                           print (“ %d  \t “ queue[ i ])
                   End if
Step2:-      stop
 

Write A Program to implement operation of circular queue using array.

Solution:-
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
# define maxsize 5
int queue[maxsize];
int rear=-1;
int front=-1;
void cenque( );
void cdeque( );
void display( );
void main( )
{
  int choice;
  clrscr( );
  do
  {
            printf("\n 1.ENQUEUW");
            printf("\n 2.DEQUEUE");
            printf("\n 3.DISPLAY");
            printf("\n 4.EXIT");
            printf("\n Enter your choice: \t");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: cenque( );
                        break;
                case 2: cdeque( );
                        break;
                case 3: display( );
                        break;
                case 4: exit(0);
                        break;
            }
   }
   while(choice!=4);
   getch( );
}
void cenque( )
{
  int item;
  if(front==(rear+1)%maxsize)
  printf("\n CIRCULAR QUEUE OVERFLOW");
  else
  {
       printf("\n Enter item: \t");
       scanf("%d",&item);
       if(front==-1)
       {
            rear=0;
            front=0;
       }
       else
       rear=(rear+1)%maxsize;
       queue[rear]=item;
       printf("\n %d is inserted successfully",queue[rear]);
   }
}
void cdeque( )
{
  int item;
  if(front==-1)
  printf("\n CIRCULAR QUEUE UNDERFLOW");
  else
  {
      item=queue[front];
      if(rear==front)
      {
         front=-1;
         rear=-1;
      }
      else
      front=(front+1)%maxsize;
      printf("\n %d is deleted successfully",item);
   }
}
void display( )
{
  int i;
  if(rear==-1)
  printf("\n NO ITEM");
  else
  {
            if(rear>=front)
            {
              for(i=front;i<=rear;i++)
              printf("%d \t",queue[i]);
            }
            else
            {
              for(i=front;i<maxsize;i++)
              printf("%d \t",queue[i]);
              for(i=0;i<=rear;i++)
              printf(" %d \t",queue[i]);
            }
   }
}
 

Write an algorithm for the circular Queue INSERTION & DELETION operations.

Solution:-
* Algorithm to insert  item into circular queue.
 
Cenque (queue [maxsize] , item , front , rear)
Step1:-       if(front == (rear+1)% maxsize)
                          print “ CIRCULAR QUEUE OVERFLOW “
                   else
                          Read item
                          if(rear == -1)
                                  Set front = 0
                                  Set rear = 0
                          else
                                  rear=(rear+1)% maxsize
                          End if
                          Queue [rear ]= item
                          pirnt “ SUCCESS INSERT ITEM “
                   End if
Step2:-      stop.
 
* Algorithm to delete item in to circular queue.
 
Cdeque ( queue [maxsize] , item , front , rear)
Step1:-       if(front == -1)
                           print “ CIRCULAR QUEUE UNDERFLOW “
                   else
                           item = queue [front ]
                           if(front == rear)
                                     Set  front = -1
                                     Set rear = -1
                           else
                                     front = (front+1)%maxsize
                           End if
                           print “ SUCCESS DELETE ITEM “
                   End if
Step2:-      stop.