Advertisement
mhrabbi

Queue 0.1

Nov 7th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<limits.h>
  4.  
  5. struct queue
  6. {
  7.     int data;
  8.     struct queue *next;
  9. }*rear=NULL,*front=NULL;
  10.  
  11. int size=0;
  12.  
  13. int main()
  14. {
  15.     int data,choice;
  16.  
  17.     while(1)
  18.     {
  19.         printf("1: Enqueue \n");
  20.         printf("2: Dequeue \n");
  21.         printf("3: Show The Number of Item\n");
  22.         printf("4: Minimum And Maximum items\n");
  23.         printf("5: Find An Item\n");
  24.         printf("6: Print All Item\n");
  25.         printf("0: Exit\n");
  26.  
  27.         printf("     \nSelect your option: ");
  28.         scanf("%d",&choice);
  29.  
  30.         switch(choice)
  31.         {
  32.         case 1:
  33.             printf("Enter data to Enqueue: ");
  34.             scanf("%d",&data);
  35.             enqueue(data);
  36.             break;
  37.         case 2:
  38.             dequeue();
  39.             break;
  40.         case 3:
  41.             printf("The Number of item is %d",size);
  42.             break;
  43.  
  44.         case 4:
  45.             minmax();
  46.             break;
  47.         case 5:
  48.             find();
  49.             break;
  50.         case 6:
  51.             displayList();
  52.             break;
  53.         case 0:
  54.             printf("Exiting from terminal\n");
  55.             exit(0);
  56.  
  57.         }
  58.         printf("\n\n");
  59.     }
  60. }
  61.  
  62. void enqueue(int data)
  63. {
  64.     struct queue *temp;
  65.  
  66.     temp=(struct queue *)malloc(sizeof(struct queue));
  67.  
  68.     temp->data=data;
  69.     temp->next=NULL;
  70.  
  71.     if(front==NULL)
  72.     {
  73.         front=temp;
  74.         rear=temp;
  75.     }
  76.     else
  77.     {
  78.         rear->next=temp;
  79.         rear=temp;
  80.     }
  81.  
  82.     printf("%d enqueued\n",temp->data);
  83.     size++;
  84.  
  85. }
  86.  
  87. void dequeue()
  88. {
  89.     struct queue *temp;
  90.     int data;
  91.  
  92.  
  93.     if(size<= 0 || !front)
  94.     {
  95.         printf("Queue is Empty");
  96.     }
  97.  
  98.     temp=front;
  99.  
  100.     data=temp->data;
  101.     front=front->next;
  102.  
  103.     free(temp);
  104.  
  105.     size--;
  106.  
  107.     printf("%d Dequeued",data);
  108.  
  109.     return 0;
  110. }
  111.  
  112. void displayList()
  113. {
  114.     struct queue *temp;
  115.  
  116.     temp=front;
  117.  
  118.     while(temp!= NULL)
  119.     {
  120.         printf("\n%d",temp->data);
  121.         temp=temp->next;
  122.     }
  123. }
  124.  
  125. void minmax()
  126. {
  127.     struct queue *temp;
  128.     int max,min;
  129.  
  130.     temp=front;
  131.  
  132.     max=temp->data;
  133.     min=temp->data;
  134.  
  135.     while(temp!=NULL)
  136.     {
  137.         if(temp->data>max)
  138.         {
  139.             max=temp->data;
  140.         }
  141.         if(temp->data<min)
  142.         {
  143.             min=temp->data;
  144.         }
  145.         temp=temp->next;
  146.     }
  147.     printf("Maximum element is : %d\n",max);
  148.     printf("Minimum element is : %d\n",min);
  149. }
  150.  
  151. void find()
  152. {
  153.     struct queue *temp;
  154.     int num,i,Item;
  155.  
  156.     i=0;
  157.  
  158.     temp=front;
  159.  
  160.     printf("Enter a number to find: ");
  161.     scanf("%d",&num);
  162.  
  163.     while(temp!=NULL)
  164.     {
  165.         if(temp->data==num)
  166.            {
  167.             i++;
  168.             Item=temp->data;
  169.            }
  170.  
  171.         temp=temp->next;
  172.     }
  173.    printf("%d Found %d times\n",Item,i);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement