Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 4
  5.  
  6. int queue[SIZE]; // |0|1|2|3|
  7.  
  8. int head = -1;
  9. int tail = -1;
  10.  
  11.  
  12. void initialise() {
  13.     head = -1;
  14.     tail = -1;
  15.  
  16.     for (int i = 0; i < SIZE; i++) {
  17.         queue[i] = 0;
  18.     }
  19. }
  20.  
  21.  
  22. int isFull() {
  23.     if ( ( head == tail + 1 ) )
  24.     {
  25.         return 1;
  26.     }
  27.     else if ((head == 0 && tail == SIZE - 1)) {
  28.         return 1;
  29.     }
  30.     else {
  31.         return 0;
  32.     }
  33. }
  34.  
  35. int isEmpty() {
  36.     if (head == -1) {
  37.         return 1;
  38.     }
  39.     else {
  40.         return 0;
  41.     }
  42. }
  43.  
  44. void push(int number) {
  45.     if (head == -1) {
  46.         head = 0;
  47.         tail = (tail + 1) % SIZE;
  48.         queue[tail] = number;
  49.         //printf("ATM->Head is at position:%d\n", head);
  50.         //printf("ATM->Tail is at position:%d\n", tail);
  51.  
  52.     }
  53.     else if (head != -1) {
  54.         if (isFull()) {
  55.             head = (head + 1) % SIZE;
  56.             tail = (tail + 1) % SIZE;
  57.             queue[tail] = number;
  58.             //printf("ATM->Head is at position:%d\n", head);
  59.             //printf("ATM->Tail is at position:%d\n", tail);
  60.         }
  61.  
  62.         else {
  63.             tail = (tail + 1) % SIZE;
  64.             queue[tail] = number;
  65.             //printf("ATM->Head is at position:%d\n", head);
  66.             //printf("ATM->Tail is at position:%d\n", tail);
  67.         }
  68.  
  69.         /*
  70.         else if(head != -1){
  71.             if(isFull() && tail>head){
  72.                 printf("Tail reached head!\n");
  73.                 head = (head + 1)% SIZE;
  74.                 tail = (tail + 1) % SIZE;
  75.                 queue[tail] = number;
  76.                 printf("ATM->Head is at position:%d\n", head);
  77.                 printf("ATM->Tail is at position:%d\n", tail);
  78.             }
  79.             else if(tail<head){
  80.                 head = (head + 1) % SIZE;
  81.                 queue[tail-1] = number;
  82.             }
  83.             else {
  84.                 tail = (tail + 1) % SIZE;
  85.                 queue[tail] = number;
  86.                 printf("ATM->Head is at position:%d\n", head);
  87.                 printf("ATM->Tail is at position:%d\n", tail);
  88.             }
  89.         }
  90.         */
  91.     }
  92. }
  93.  
  94. void pop() {
  95.  
  96.     if (head == -1)
  97.     {
  98.         printf("Queue Underflow\n");
  99.         return;
  100.     }
  101.     printf("Element deleted from queue is : %d\n", queue[head]);
  102.     queue[head] = 0;
  103.     if (head == tail) /* queue has only one element */
  104.     {
  105.         head = -1;
  106.         tail = -1;
  107.     }
  108.     else
  109.     {
  110.         if (head == SIZE - 1)
  111.             head = 0;
  112.         else
  113.             head = head + 1;
  114.     }
  115.    
  116.     /*
  117.     int delete;
  118.  
  119.     if (head == -1) {
  120.         printf("Queue is empty! Don't have anything to pop!\n");
  121.     }
  122.     else if(head==tail) {
  123.         queue[head] = 0 ;
  124.         head = -1;
  125.         tail = -1;
  126.         printf("ATM->Head is at position:%d\n", head);
  127.         printf("ATM->Tail is at position:%d\n", tail);
  128.     }
  129.     else if (head != tail) {
  130.  
  131.         if (tail > head) {
  132.        
  133.             if (tail == head + 1) {
  134.                 queue[head] = 0;
  135.                 head = tail;
  136.                 printf("ATM->Head is at position:%d\n", head);
  137.                 printf("ATM->Tail is at position:%d\n", tail);
  138.             }
  139.  
  140.             else {
  141.                 queue[head] = 0;
  142.                 head = (head + 1) % SIZE;
  143.                 printf("ATM->Head is at position:%d\n", head);
  144.                 printf("ATM->Tail is at position:%d\n", tail);
  145.             }
  146.         }
  147.         else{
  148.             if (head == tail + 1) {
  149.                 queue[head] = 0;
  150.                 if (head == tail) {
  151.                     head = (head + 1) % SIZE;
  152.                 }
  153.        
  154.        
  155.         printf("ATM->Head is at position:%d\n", head);
  156.         printf("ATM->Tail is at position:%d\n", tail);
  157.             }
  158.             else {
  159.                 queue[head] = 0;
  160.                 head = (head + 1) % SIZE;
  161.                 printf("ATM->Head is at position:%d\n", head);
  162.                 printf("ATM->Tail is at position:%d\n", tail);
  163.             }
  164.         }
  165.     }
  166.     */
  167.  
  168. }
  169.  
  170. void display() {
  171.     for (int i = 0; i < SIZE; i++) {
  172.         printf("queue[%d] = %d \n", i, queue[i]);
  173.     }
  174.     printf("ATM->Head is at position:%d\n", head);
  175.     printf("ATM->Tail is at position:%d\n", tail);
  176. }
  177.  
  178. int main() {
  179.  
  180.     //case 1
  181.     int numberPush;
  182.  
  183.     int choice;
  184.  
  185.     do
  186.     {
  187.         printf("\n\t---MENU---\n");
  188.         printf("\n\t---1.PUSH---\n");
  189.         printf("\n\t---2.POP---\n");
  190.         printf("\n\t---3.EMPTY---\n");
  191.         printf("\n\t---4.FULL---\n");
  192.         printf("\n\t---5.INITILIASE---\n");
  193.         printf("\n\t---6.DISPLAY---\n");
  194.         printf("\n\t---7.EXIT---\n");
  195.  
  196.         printf("Choose an option:");
  197.         scanf("%d", &choice);
  198.  
  199.         switch (choice)
  200.         {
  201.         case 1: {
  202.             printf("Push:");
  203.             scanf("%d",&numberPush);
  204.             push(numberPush);
  205.             break;
  206.         }
  207.         case 2: {
  208.             pop();
  209.             break;
  210.         }
  211.  
  212.         case 3: {
  213.             if (isEmpty() == 1) {
  214.                 printf("Empty!All the positions are empty!\n");
  215.             }
  216.             else {
  217.                 printf("Not empty! We have elements!\n");
  218.             }
  219.             break;
  220.         }
  221.  
  222.         case 4: {
  223.             if (isFull() == 1) {
  224.                 printf("Full! Each position has an element stored!\n");
  225.             }
  226.             else {
  227.                 printf("Not each position has an element stored!\n");
  228.             }
  229.             break;
  230.         }
  231.  
  232.         case 5: {
  233.             initialise();
  234.             printf("The list has been initialized!\n");
  235.             break;
  236.         }
  237.  
  238.         case 6: {
  239.             display();
  240.             break;
  241.         }
  242.  
  243.         default:
  244.             printf("Program quited!\n");
  245.             break;
  246.         }
  247.  
  248.     } while (choice>0 && choice<7);
  249.  
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement