| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 14871 | Queue Operations |
|
Description
Test By Yourself
OJ Cannot judge this question
Test cases are uploaded to eecalss
Implement a Queue object. A Queue is also called a queueing structure, just like lining up at a convenience store checkout.
A queue has a front and a rear, and we can only insert at the rear of the queue and remove from the front of the queue.
Therefore, a queue has the characteristic of first-in-first-out (FIFO).
The header file of Queue is shown below.
Header File: (queue.h) Queue Header File
int data;
struct listnode *next;
};
typedef struct listnode ListNode;
struct queue {
ListNode *front;
ListNode *rear;
};
typedef struct queue Queue;
void init(Queue *s);
int empty(Queue *s);
void enqueue(Queue *s, int data);
int dequeue(Queue *s);
Functions related to Queue are as follows:
-
init initializes the queue.
-
empty checks whether the queue is empty.
-
enqueue inserts a piece of data at the rear of the queue.
-
dequeue removes a piece of data from the front of the queue.
The main program and input/output example are shown below.
Please implement all the Queue functions.
Example Program: (queue-main.c) Testing the Queue
#include "queue.h"
int main(void)
{
Queue queue;
Queue *q = &queue;
init(q);
if (empty(q))
printf("initially queue is empty\n");
enqueue(q, 2);
enqueue(q, 3);
printf("%d\n", dequeue(q));
enqueue(q, 4);
printf("%d\n", dequeue(q));
printf("%d\n", dequeue(q));
enqueue(q, 5);
enqueue(q, 6);
printf("%d\n", dequeue(q));
printf("%d\n", dequeue(q));
return 0;
}
Input
A set of queue operation instructions (enqueue / dequeue)
Ex. (No input)
Output
Results after correct simulation.
Ex. initially queue is empty
2
3
4
5
6