ramytamer

Untitled

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