Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a series of queue operations, please complete the queue ADT to output for respective operations. -------------------------Copy the following code, complete it and submit------------------------- #include

Given a series of queue operations, please complete the queue ADT to output for respective operations. -------------------------Copy the following code, complete it and submit-------------------------  #include  #include  #include  #define ERROR_EMPTY 0x80000000 #define ERROR_FULL 0x7fffffff typedef enum { enqueue = 1, dequeue, end, isfull, isempty } Operation; struct Queue {  char *arr;  int capacity;  int front;  int rear; }; struct Queue *Create(int capacity) {  struct Queue *Q = (struct Queue *)malloc(sizeof(struct Queue));  Q->arr = (char *)malloc(sizeof(char) * capacity);  Q->capacity = capacity;  Q->front = 0;  Q->rear = 0;  return Q; } int IsFull(struct Queue *Q) {  // write your code here } int IsEmpty(struct Queue *Q) {  // write your code here } int Enqueue(struct Queue *Q, char x) {  if (IsFull(Q))    return ERROR_FULL;   // write your code here } int Dequeue(struct Queue *Q) {  if (IsEmpty(Q))    return ERROR_EMPTY;  // write your code here } Operation Getop() { // given starter code  char str[10];  scanf("%s", str);  if (strcmp(str, "Enqueue") == 0) {    return 1;  } else if (strcmp(str, "Dequeue") == 0) {    return 2;  } else if (strcmp(str, "End") == 0) {    return 3;  } else if (strcmp(str, "Isfull") == 0) {    return 4;  } else if (strcmp(str, "Isempty") == 0) {    return 5;  } else {    return 0;  } } void printQueue(struct Queue *Q) {  int curr;  curr = Q->front;  if (!IsEmpty(Q)) {    while ((curr + 1) % Q->capacity != Q->rear) {      printf("%c ", Q->arr[curr]);      curr = (curr + 1) % Q->capacity;    }    printf("%c", Q->arr[curr]);  } else {    printf("emtpy");  } } int main(void) {  int res;  char v;  int capacity;  struct Queue *Q;  int flag = 0;   scanf("%d", &capacity);  Q = Create(capacity);  while (!flag) {    switch (Getop()) {    case enqueue:      scanf(" %c", &v);      if (Enqueue(Q, v) == ERROR_FULL) {        printf("Queue is full");      }      break;    case dequeue:      res = Dequeue(Q);      if (res == ERROR_EMPTY) {        printf("Queue is empty");      } else {        printf("%c", (char)res);      }      break;    case end:      printQueue(Q);      flag = 1;      break;    case isfull:      if (IsFull(Q)) {        printf("Queue is full");      } else {        printf("Queue is not full");      }      break;    case isempty:      if (IsEmpty(Q)) {        printf("Queue is empty");      } else {        printf("Queue is not empty");      }      break;    }  }  return 0; } -------------------------------------------End of Code------------------------------------------- Input: The first line is a positive integer N, which denotes the capacity of the queue, followed by M lines of queue operations, each chosen from one of the followings: Enqueue x(a char), Dequeue, Isfull and Isempty, except the last of which, which is End. Output: A line per Dequeue (if queue is empty), Isfull or Isempty operation, each denoting the output of the specified operation, following by a line of remaining elements in the queue from head to tail upon End. Sample Input 1: 4                  Dequeue            Enqueue a Enqueue b            Dequeue End Sample Output 1: Queue is empty ab Sample Input 2:  1                  Enqueue a          Enqueue b          Dequeue End Sample Output 2: Queue is full Queue is full Queue is empty empty

Step by Step Solution

3.27 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

Here is the completed code include include include define ERROREMPTY 0x80000000 define ERRORFULL 0x7... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Operating System questions

Question

Which 3 accounts can't be merged in the chart of accounts?

Answered: 1 week ago