ramytamer

deque

May 9th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <unistd.h> // Working in Linux
  4. #include <windows.h> // Working on Windows
  5.  
  6. #define TYPE int
  7.  
  8. typedef struct Node{
  9.     TYPE data;
  10.     struct Node *next,*back;
  11. }Node;
  12.  
  13. typedef struct Deque{
  14.     Node *head,*tail;
  15. }Deque;
  16.  
  17. void reset(Deque *queue){ queue->head = queue->tail = NULL;}
  18. int isEmpty(Deque *queue){ return !queue->head; }
  19.  
  20. void insertFirst(Deque *queue,TYPE val){
  21.     Node *node = (Node*) malloc(sizeof(Node));
  22.     node->data = val;
  23.     node->back = NULL;
  24.     if(isEmpty(queue)){
  25.         node->next = NULL;
  26.         queue->head = node;
  27.     }else{
  28.         node->next = queue->tail;
  29.         (queue->tail)->back = node;
  30.     }
  31.     queue->tail = node;
  32. }
  33.  
  34. TYPE first(Deque queue){
  35.     if(!isEmpty(&queue))
  36.         return (queue.tail)->data;
  37.     return 0;
  38. }
  39.  
  40. void removeFirst(Deque *queue){
  41.     if(!isEmpty(queue)) {
  42.         if(!(queue->tail)->next) // in case of last element in the queue
  43.             reset(queue);
  44.         else{
  45.             Node *node = (queue->tail)->next;
  46.             node->back = NULL;
  47.             free(queue->tail);
  48.             queue->tail = node;
  49.         }
  50.     }
  51. }
  52.  
  53. void insertLast(Deque *queue, TYPE val){
  54.     Node *node = (Node*) malloc(sizeof(Node));
  55.     node->data = val;
  56.     node->next = NULL;
  57.     if(isEmpty(queue)){
  58.         node->back = NULL;
  59.         queue->tail = node;
  60.     }else{
  61.         (queue->head)->next = node;
  62.         node->back = queue->head;
  63.     }
  64.     queue->head = node;
  65. }
  66.  
  67. TYPE last(Deque queue){
  68.     if(!isEmpty(&queue))
  69.         return (queue.head)->data;
  70.     return 0;
  71. }
  72.  
  73. void removeLast(Deque *queue){
  74.     if(!isEmpty(queue)) {
  75.         if(!(queue->tail)->next) // in case of last element in the queue
  76.             reset(queue);
  77.         else{
  78.             Node *node = (queue->head)->back;
  79.             node->next = NULL;
  80.             free(queue->head);
  81.             queue->head = node;
  82.         }
  83.     }
  84. }
  85.  
  86. void display(Deque queue){
  87.     printf("Queue: \n");
  88.     while(queue.tail) {
  89.         printf("[%d]\n",queue.tail->data); queue.tail = queue.tail->next;
  90.     }
  91. }
  92.  
  93. int size(Deque queue){
  94.     unsigned int i=0;
  95.     while(queue.tail) {
  96.         i++; queue.tail = queue.tail->next;
  97.     }
  98.     return i;
  99. }
  100.  
  101. void BuildQueue(Deque *queue,int limit){
  102.     unsigned int i=1;
  103.     while(i<=limit)
  104.         insertLast(queue,i++);
  105. }
  106.  
  107. void status(char x[],int time){
  108.     printf("%sReturning to main menu...\n",x); sleep(time); // Wait time
  109. }
  110.  
  111. int main(){
  112.     system("clear");
  113.     Deque queue; reset(&queue);
  114.     unsigned int i=0;
  115.     printf("Welcome to the \"Deque\" program :\n\n");
  116.     do{
  117.         printf("Please choose what you want to do : \n");
  118.         printf("1.Build a queue.\n2.Insert at the FIRST of the queue\n");
  119.         printf("3.Insert at the END of the queue.\n4.Get value of FIRST element in the queue\n");
  120.         printf("5.Get value of LAST element in the queue.\n6.Check if queue is empty\n");
  121.         printf("7.Size of the queue\n8.Display the queue\n");
  122.         printf("9.Exit\n");
  123.         printf("\nChoise: ");
  124.     }while(scanf("%d",&i) && !i);
  125.     unsigned int val,limit;
  126.     system("clear");
  127.     switch(i){
  128.         case 1:
  129.             printf("What limit of this queue to be built: "); scanf("%d",&limit);
  130.             BuildQueue(&queue,limit);
  131.             status("Success Build.",2);
  132.             break;
  133.         case 2:
  134.             printf("Value to be inserted at the FIRST of queue: "); scanf("%d",&val);
  135.             insertFirst(&queue,val);
  136.             status("Success insert operation.",2);
  137.             break;
  138.         case 3:
  139.             printf("Value to be inserted at the LAST of queue: "); scanf("%d",&val);
  140.             insertLast(&queue,val);
  141.             status("Success insert operation.",2);
  142.             break;
  143.         case 4:
  144.             printf("Value of FIRST element : [%d]\n", first(queue));
  145.             status("Success operation.",2);
  146.             break;
  147.         case 5:
  148.             printf("Value of LAST element : [%d]\n", last(queue));
  149.             status("Success operation.",2);
  150.             break;
  151.         case 6:
  152.             !isEmpty(&queue) ? printf("Queue is not Empty.\n") :  printf("Queue is Empty.\n") ;
  153.             status("Success Checking operation.",2);
  154.             break;
  155.         case 7:
  156.             printf("Size of queue : [%d]\n", size(queue));
  157.             status("Success operation.",2);
  158.             break;
  159.         case 8:
  160.             display(queue);
  161.             status("Success display operation.",2);
  162.             break;
  163.         case 9:
  164.             exit(0);
  165.     }
  166.     printf("[%d]\n", size(queue));
  167.     // main();
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment