Advertisement
ramytamer

Untitled

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