Advertisement
RenHao

DS_mid_HW

Nov 25th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_QUEUE_SZ 100
  5. #define MALLOC(p,s) \
  6.         if(!((p) = malloc(s)))    {\
  7.             fprintf(stderr, "Insufficient memory\n");   \
  8.             exit(EXIT_FAILURE);\
  9.         }
  10. typedef int bool;
  11. enum{false,true};
  12. /*
  13. Complex number system : x+iy
  14. Features:
  15.  User-friendly
  16.  Menu-driven
  17.  
  18. Use linked list to implement a queue
  19. Functions:
  20.  a. Read a sequence of complex numbers from input
  21.  b. Determine if a queue is empty
  22.  c. Insert a complex number at the end of a list
  23.  d. Search for a complex number
  24.  e. Delete a complex number
  25.  f. Print out a queue
  26.  g. Count the number of points in a queue
  27. */
  28.  
  29. //  Declare a new type
  30. typedef struct listNode* listPtr;
  31. typedef struct listNode
  32. {
  33.     int Re;
  34.     int Im;
  35.     listPtr link;
  36. }listNode;
  37. listPtr front=NULL,rear=NULL;
  38.  
  39. //  Declare functions
  40. void Attach(void);
  41. bool QEmpty(void);
  42. void Insertion(int x, int y);
  43. int SearchQ(int x, int y);
  44. void Delete(void);
  45. void PrintQ(void);
  46. int Traverse(void);
  47. void Menu(void);
  48.  
  49. int main(int argc, const char * argv[])
  50. {
  51.     //char input;
  52.     int input2;
  53.     int insert1,insert2;
  54.     Menu();
  55.     for(;;)
  56.     {
  57.         printf("Your input: ");
  58.         //input = getchar();
  59.         scanf("%d",&input2);
  60.         switch (input2)
  61.         {
  62.             case 1:
  63.                 Attach();
  64.                 break;
  65.             case 2:
  66.                 if(QEmpty()) printf("This is a empty queue\n");
  67.                 else         printf("This queue have numbers\n");
  68.                 break;
  69.             case 3:
  70.                 printf("Please enter the Real part: ");
  71.                 scanf("%d",&insert1);
  72.                 printf("Please enter the Imagine part: ");
  73.                 scanf("%d",&insert2);
  74.                 Insertion(insert1, insert2);
  75.                 break;
  76.             case 4:
  77.                 printf("Which complex number you want to search ?\n");
  78.                 printf("Re :");
  79.                 scanf("%d",&insert1);
  80.                 printf("Im :");
  81.                 scanf("%d",&insert2);
  82.                 SearchQ(insert1, insert2);
  83.                 break;
  84.             case 5:
  85.                 Delete();
  86.                 break;
  87.             case 6:
  88.                 PrintQ();
  89.                 break;
  90.             case 7:
  91.                 Traverse();
  92.                 break;
  93.             case 0:
  94.                 printf("Now program terminated...\n");
  95.                 return 0;
  96.             default:
  97.                 printf("Please enter 0-9 to choose function!\n");
  98.                 break;
  99.         }
  100.     }
  101.    
  102.     return 0;
  103. }
  104.  
  105. //  Functions implementation
  106. void Menu(void)
  107. {
  108.     printf("Please enter a characater to implement a function\n");
  109.     printf("===================================================\n");
  110.     printf("1. Read a sequence of complex numbers from input\n");
  111.     printf("2. Determine if a queue is empty\n");
  112.     printf("3. Insert a complex number at the end of a list\n");
  113.     printf("4. Search for a complex number\n");
  114.     printf("5. Delete a complex number\n");
  115.     printf("6. Print out a queue\n");
  116.     printf("7. Count the number of points in a queue\n");
  117.     printf("0. To terminate the program\n");
  118.     printf("===================================================\n");
  119. }
  120.  
  121. void Attach(void)
  122. {
  123.     int n;
  124.     printf("Please enter n numbers you want to insert, 0<n<100\n");
  125.     scanf("%d",&n);
  126.     if(n>MAX_QUEUE_SZ && n<0)
  127.     {
  128.         printf("Error !");
  129.         exit(EXIT_FAILURE);
  130.     }
  131.     int re,im;
  132.     printf("Start insertion...\n\n");
  133.     for (int i=0; i<n; i++)
  134.     {
  135.         printf("Please enter Re part of %d number : ",i);
  136.         scanf("%d",&re);
  137.         printf("Please enter Im part of %d number : ",i);
  138.         scanf("%d",&im);
  139.         Insertion(re, im);
  140.     }
  141.     printf("Insertion done...\n");
  142. }
  143.  
  144. bool QEmpty(void)
  145. {
  146.     if(front == NULL )   return true;
  147.     else                return false;
  148. }
  149.  
  150. void Insertion(int x, int y)
  151. {
  152.     listPtr temp;
  153.     MALLOC(temp, sizeof(*temp));
  154.     temp->Re = x;
  155.     temp->Im = y;
  156.     temp->link = NULL;
  157.     if(QEmpty())
  158.     {
  159.         front = temp;
  160.         rear = temp;
  161.     }
  162.     else
  163.     {
  164.         rear->link = temp;
  165.         rear = temp;
  166.     }
  167. }
  168.  
  169. int SearchQ(int x,int y)
  170. {
  171.     listPtr temp = front;
  172.     int i=1;
  173.     while (temp && !QEmpty())
  174.     {
  175.         if( (temp->Re == x) && (temp->Im == y))
  176.         {
  177.             printf("Target located at %d member of queue\n",i);
  178.             return i;
  179.         }
  180.         temp = temp->link;
  181.         i++;
  182.     }
  183.     i=0;
  184.     printf("Target doesn't exist!\n");
  185.     return i;
  186. }
  187.  
  188. void Delete(void)
  189. {
  190.     listPtr now;
  191.     if (QEmpty())
  192.     {
  193.         printf("Empty Queue...\n");
  194.         exit(EXIT_FAILURE);
  195.     }
  196.     printf("The number you delete is : %d + i%d\n",front->Re,front->Im);
  197.     now = front;
  198.     front = front->link;
  199.     if(front == NULL)   rear = NULL; //Clear pointer
  200.     free(now);
  201. }
  202.  
  203. void PrintQ(void)
  204. {
  205.     listPtr ptr = front;
  206.     while (ptr)
  207.     {
  208.         printf("Real part: %d, Imagine part: %d\n",ptr->Re,ptr->Im);
  209.         ptr = ptr->link;
  210.     }
  211. }
  212. int Traverse(void)
  213. {
  214.     listPtr ptr = front;
  215.     int i =0;
  216.     while (ptr)
  217.     {
  218.         ptr = ptr->link;
  219.         i++;
  220.     }
  221.     printf("There are %d members in queue\n",i);
  222.     return i;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement