Question

In: Computer Science

C Programme Time limit: 5000ms Memory limit: 256mb Description: Given a series of queue operations, please...

C Programme 
Time limit: 5000ms
Memory limit: 256mb

Description:
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 <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {enqueue = 1, dequeue, end, isfull, isempty} Operation;

struct Queue {
    int * arr;
    int capacity;
    int front;
    int rear;
};

struct Queue * Create(int capacity) {
    struct Queue * queue = (struct Queue *)malloc(sizeof(struct Queue));
    queue->arr = (int*)malloc(sizeof(int) * capacity);
    queue->capacity = capacity;
    queue->front = 0;
    queue->rear = 0;
    return queue;
}

int IsFull(struct Queue * queue) {
    // write your code here
}

int IsEmpty(struct Queue * queue) {
    // write your code here
}

int Enqueue(struct Queue * queue, int x) {
    // write your code here
}

int Dequeue(struct Queue * queue) {
    // write your code here
}

Operation Getop() {
    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 printArray(struct Queue * queue) {
    int curr;
    curr = queue->front;
    if (!IsEmpty(queue)){
        while((curr+1)%queue->capacity != queue->rear){
            printf("%d ", queue->arr[curr]);
            curr = (curr + 1)%queue->capacity;
        }
        printf("%d\n", queue->arr[curr]);
    }
}

int main(void) {
    int X;
    int capacity;
    struct Queue * queue;
    int flag = 0;

    scanf("%d", &capacity);
    queue = Create(capacity);
    while (!flag) {
        switch(Getop()) {
            case enqueue:
                scanf("%d", &X);
                if (Enqueue(queue, X) == 1e5) {
                    printf("Queue is full\n");
                }
                break;
            case dequeue:
                X = Dequeue(queue);
                if (X == 1e5) {
                    printf("Queue is empty\n");
                }
                break;
            case end:
                printArray(queue);
                flag = 1;
                break;
            case isfull:
                if (IsFull(queue)){
                    printf("Queue is full\n");
                }
                else{
                    printf("Queue is not full\n");
                }
                break;
            case isempty:
                if (IsEmpty(queue)){
                    printf("Queue is empty\n");
                }
                else{
                    printf("Queue is not empty\n"); 
                }
                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(an integer), 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 1
Enqueue 2
Dequeue
End

Sample Output 1:
Queue is empty
2

Sample Input 2:
2
Isempty
Enqueue 1
Isfull
Enqueue 2
End

Sample Output 2:
Queue is empty
Queue is full
Queue is full
1

Solutions

Expert Solution

Code

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef enum { enqueue = 1, dequeue, end, isfull, isempty } Operation;

struct Queue {

  int * arr;

  int capacity;

  int front;

  int rear;

};

struct Queue * Create(int capacity) {

  struct Queue * queue = (struct Queue *)malloc(sizeof(struct Queue));

  queue->arr = (int*)malloc(sizeof(int) * capacity);

  queue->capacity = capacity;

  queue->front = 0;

  queue->rear = 0;

  return queue;

}

int IsFull(struct Queue * queue) {

  if (queue->rear == queue->capacity-1)

    return 1;

  

  return 0;

}

int IsEmpty(struct Queue * queue) {

  if (queue->rear == 0)

    return 1;

  return 0;

}

int Enqueue(struct Queue * queue, int x) {

  if (IsFull(queue))

    return 100000;

  else

  {

    queue->arr[queue->rear] = x;

    queue->rear++;

  }

return 1;

}

int Dequeue(struct Queue * queue) {

  int val;

  if (IsEmpty(queue))

    return 1e5;

  else

  {

    val = queue->arr[queue->front];

    queue->front++;

  }

  return val;

}

Operation Getop() {

  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 printArray(struct Queue * queue) {

  int curr;

  curr = queue->front;

  if (!IsEmpty(queue)) {

    while ((curr + 1) % queue->capacity != queue->rear) {

      printf("%d ", queue->arr[curr]);

      curr = (curr + 1) % queue->capacity;

    }

    printf("%d\n", queue->arr[curr]);

  }

}

int main(void) {

  int X;

  int capacity;

  struct Queue * queue;

  int flag = 0;

  scanf("%d", &capacity);

  queue = Create(capacity);

  while (!flag) {

    switch (Getop()) {

    case enqueue:

      scanf("%d", &X);

      if (Enqueue(queue, X) == 1e5) {

        printf("Queue is full\n");

      }

      break;

    case dequeue:

      X = Dequeue(queue);

      if (X == 1e5) {

        printf("Queue is empty\n");

      }

      break;

    case end:

      printArray(queue);

      flag = 1;

      break;

    case isfull:

      if (IsFull(queue)) {

        printf("Queue is full\n");

      }

      else {

        printf("Queue is not full\n");

      }

      break;

    case isempty:

      if (IsEmpty(queue)) {

        printf("Queue is empty\n");

      }

      else {

        printf("Queue is not empty\n");

      }

      break;

    }

  }

  return 0;

}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Time limit: 5000ms Memory limit: 256mb Description: Given a series of stack operations, please complete the...
Time limit: 5000ms Memory limit: 256mb Description: Given a series of stack operations, please complete the stack ADT to output for respective operations. -------------------------Copy the following code, complete it and submit------------------------- #include <stdio.h> #include <stdlib.h> typedef enum {push = 1, pop, end} Operation; typedef struct Node * PtrToNode; struct Node {     int element;     PtrToNode next;     PtrToNode prev; }; typedef struct ListRecord * List; struct ListRecord {     PtrToNode head;     PtrToNode tail; }; List Create() {    ...
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue...
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue using a linked list as a backing data structure Requirements Write the following struct struct JobNode { string name; JobNode * next; }; Create a class called Queue. In this class, create a private variable JobNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used to dynamically create new nodes...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming Exercise from the Book section. If you get a logical or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Describe the following terms a) stationary time series b) seasonal time series c) box test (...
Describe the following terms a) stationary time series b) seasonal time series c) box test ( clearly define your notation and the null and alternative hypotheses)
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
Refer to the gasoline sales time series data in the given table.
Refer to the gasoline sales time series data in the given table. Week Sales (1000s of gallons) 1 17 2 21 3 19 4 24 5 19 6 15 7 21 8 19 9 23 10 19 11 15 12 22 Compute four-week and five-week moving averages for the time series. Round your answers to two decimal places. Week Sales 4-WeekMoving Average 5-WeekMoving Average 1 17     2 21     3 19     4 24    ...
Refer to the gasoline sales time series data in the given table.
Refer to the gasoline sales time series data in the given table. a. Compute four-week and five-week moving averages for the time series. Round your answers to two decimal places.b. Compute the MSE for the four-week and five-week moving average forecasts. Round your intermediate calculations and final answers to two decimal places. MSE for four-week moving average = MSE for five-week moving average = c. What appears to be the best number of weeks of past data (three, four, or five) to use in...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return empty queue // enqueue and dequeue each return an int error code int enqueue(int, Queue *) int dequeue(Queue *, int *) int getQsize(Queue *) // returns # of items in queue void freeQueue(Queue *) // Free *all* space Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time. You will need to document each of your functions to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT