ramytamer

[Lab3] - Linux OS version 2

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