Advertisement
sudhanshuptl

queue in c array

Jul 25th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. //Queue Implemented in array
  2. // C-Program
  3.  
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. #include<stdlib.h>
  7.  
  8. void insert(int *ar,int *front,int *rear,int N){
  9.         int n;
  10.         printf("Enter number to insert :");
  11.         scanf("%d",&n);
  12.        
  13.         if((*front==0 && *rear== N) || *front==*rear+1){
  14.             printf("overflow condition -> queue is full");
  15.         }
  16.         else if(*front==-1){
  17.             *front=0;
  18.             *rear=0;
  19.             *(ar+(*rear))=n;
  20.         }
  21.         else if(*rear==N && *front !=0){
  22.             *rear=0;
  23.             *(ar+(*rear))=n;
  24.         }
  25.         else{
  26.             *rear=*rear+1;
  27.             *(ar+(*rear))=n;
  28.         }
  29. }
  30. int del(int *ar,int *front,int *rear,int N){
  31.     int x;
  32.        if(*front == -1 ){   //queue is empty
  33.         printf("\nUnderflow cond! -> No element in queue to delete");
  34.        }
  35.        else if(*front==*rear){ //only one element in queue
  36.             x=*(ar+(*front));
  37.             *front=-1;
  38.             *rear=-1;
  39.        }
  40.        else if(*front ==N){
  41.         x=*(ar+(*front));
  42.         *front=0;
  43.        }
  44.        else{
  45.         x=*(ar+(*front));
  46.         *front=*front+1;
  47.        }
  48.        return x;
  49.        
  50. }
  51. void display(int *ar,int f,int r,int N){
  52.     printf("\n");
  53.     if(f<=r){
  54.         while(f<=r){
  55.             printf(" %d ",*(ar+f));
  56.             f=f+1;
  57.         }
  58.     }
  59.     else{
  60.         while(f<=N){
  61.             printf(" %d ",*(ar+f));
  62.             f=f+1;
  63.         }
  64.         f=0;
  65.         while(f<=r){
  66.             printf(" %d ",*(ar+f));
  67.             f=f+1;
  68.         }
  69.     }
  70.     printf("\n");
  71. }
  72.  
  73. main(){
  74.     int maxsi,front=-1,rear=-1;
  75.     int ch,x;
  76.     int *ar;
  77.     printf("Enter Max size of Queue : ");
  78.     scanf("%d",&maxsi);   // max number of element you wanna put in queue
  79.    
  80.     ar=(int*)malloc(sizeof(int)*maxsi); //create array of size maxsi
  81.    
  82.     while(1){
  83.         printf(" 1 : Insert \n 2 : Delete \n 3 : Exit\n  Enter your choice :");
  84.         scanf("%d",&ch);
  85.         switch(ch){
  86.             case 1:
  87.                
  88.                 insert(ar,&front,&rear,maxsi-1);
  89.                 display(ar,front,rear,maxsi-1);
  90.                 break;
  91.             case 2:
  92.                 x=del(ar,&front,&rear,maxsi-1);
  93.                 printf("\n%d is deleted from queue",x);
  94.                 display(ar,front,rear,maxsi-1);
  95.                 break;
  96.             case 3:
  97.                 exit(0);
  98.                 break;
  99.             default:
  100.                 printf("Wrong choice try again ??");
  101.                 break;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement