In: Computer Science
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
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.