Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_QUEUE_SZ 100
- #define MALLOC(p,s) \
- if(!((p) = malloc(s))) {\
- fprintf(stderr, "Insufficient memory\n"); \
- exit(EXIT_FAILURE);\
- }
- typedef int bool;
- enum{false,true};
- /*
- Complex number system : x+iy
- Features:
- User-friendly
- Menu-driven
- Use linked list to implement a queue
- Functions:
- a. Read a sequence of complex numbers from input
- b. Determine if a queue is empty
- c. Insert a complex number at the end of a list
- d. Search for a complex number
- e. Delete a complex number
- f. Print out a queue
- g. Count the number of points in a queue
- */
- // Declare a new type
- typedef struct listNode* listPtr;
- typedef struct listNode
- {
- int Re;
- int Im;
- listPtr link;
- }listNode;
- listPtr front=NULL,rear=NULL;
- // Declare functions
- void Attach(void);
- bool QEmpty(void);
- void Insertion(int x, int y);
- int SearchQ(int x, int y);
- void Delete(void);
- void PrintQ(void);
- int Traverse(void);
- void Menu(void);
- int main(int argc, const char * argv[])
- {
- //char input;
- int input2;
- int insert1,insert2;
- Menu();
- for(;;)
- {
- printf("Your input: ");
- //input = getchar();
- scanf("%d",&input2);
- switch (input2)
- {
- case 1:
- Attach();
- break;
- case 2:
- if(QEmpty()) printf("This is a empty queue\n");
- else printf("This queue have numbers\n");
- break;
- case 3:
- printf("Please enter the Real part: ");
- scanf("%d",&insert1);
- printf("Please enter the Imagine part: ");
- scanf("%d",&insert2);
- Insertion(insert1, insert2);
- break;
- case 4:
- printf("Which complex number you want to search ?\n");
- printf("Re :");
- scanf("%d",&insert1);
- printf("Im :");
- scanf("%d",&insert2);
- SearchQ(insert1, insert2);
- break;
- case 5:
- Delete();
- break;
- case 6:
- PrintQ();
- break;
- case 7:
- Traverse();
- break;
- case 0:
- printf("Now program terminated...\n");
- return 0;
- default:
- printf("Please enter 0-9 to choose function!\n");
- break;
- }
- }
- return 0;
- }
- // Functions implementation
- void Menu(void)
- {
- printf("Please enter a characater to implement a function\n");
- printf("===================================================\n");
- printf("1. Read a sequence of complex numbers from input\n");
- printf("2. Determine if a queue is empty\n");
- printf("3. Insert a complex number at the end of a list\n");
- printf("4. Search for a complex number\n");
- printf("5. Delete a complex number\n");
- printf("6. Print out a queue\n");
- printf("7. Count the number of points in a queue\n");
- printf("0. To terminate the program\n");
- printf("===================================================\n");
- }
- void Attach(void)
- {
- int n;
- printf("Please enter n numbers you want to insert, 0<n<100\n");
- scanf("%d",&n);
- if(n>MAX_QUEUE_SZ && n<0)
- {
- printf("Error !");
- exit(EXIT_FAILURE);
- }
- int re,im;
- printf("Start insertion...\n\n");
- for (int i=0; i<n; i++)
- {
- printf("Please enter Re part of %d number : ",i);
- scanf("%d",&re);
- printf("Please enter Im part of %d number : ",i);
- scanf("%d",&im);
- Insertion(re, im);
- }
- printf("Insertion done...\n");
- }
- bool QEmpty(void)
- {
- if(front == NULL ) return true;
- else return false;
- }
- void Insertion(int x, int y)
- {
- listPtr temp;
- MALLOC(temp, sizeof(*temp));
- temp->Re = x;
- temp->Im = y;
- temp->link = NULL;
- if(QEmpty())
- {
- front = temp;
- rear = temp;
- }
- else
- {
- rear->link = temp;
- rear = temp;
- }
- }
- int SearchQ(int x,int y)
- {
- listPtr temp = front;
- int i=1;
- while (temp && !QEmpty())
- {
- if( (temp->Re == x) && (temp->Im == y))
- {
- printf("Target located at %d member of queue\n",i);
- return i;
- }
- temp = temp->link;
- i++;
- }
- i=0;
- printf("Target doesn't exist!\n");
- return i;
- }
- void Delete(void)
- {
- listPtr now;
- if (QEmpty())
- {
- printf("Empty Queue...\n");
- exit(EXIT_FAILURE);
- }
- printf("The number you delete is : %d + i%d\n",front->Re,front->Im);
- now = front;
- front = front->link;
- if(front == NULL) rear = NULL; //Clear pointer
- free(now);
- }
- void PrintQ(void)
- {
- listPtr ptr = front;
- while (ptr)
- {
- printf("Real part: %d, Imagine part: %d\n",ptr->Re,ptr->Im);
- ptr = ptr->link;
- }
- }
- int Traverse(void)
- {
- listPtr ptr = front;
- int i =0;
- while (ptr)
- {
- ptr = ptr->link;
- i++;
- }
- printf("There are %d members in queue\n",i);
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement