Created
July 23, 2020 13:34
-
-
Save uhmseohun/ef3473ec4b892b3423cb4efe54a8800a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define QUEUE_SIZE 10 | |
typedef char element; | |
typedef struct { | |
element queue[QUEUE_SIZE]; | |
int front, rear; | |
} QueueType; | |
QueueType* createQueue() { | |
QueueType* Q; | |
Q = (QueueType*)malloc(sizeof(QueueType)); | |
Q -> front = -1; | |
Q -> rear = -1; | |
return Q; | |
} | |
int isEmpty(QueueType* Q) { | |
return Q -> front == Q -> rear; | |
} | |
int isFull(QueueType* Q) { | |
return Q -> rear == QUEUE_SIZE - 1; | |
} | |
void enQueue(QueueType* Q, element item) { | |
if(isFull(Q)) exit(0); | |
Q -> rear++; | |
Q -> queue[Q -> rear] = item; | |
} | |
element deQueue(QueueType* Q) { | |
if(isEmpty(Q)) exit(0); | |
Q -> front++; | |
return Q -> queue[Q -> front]; | |
} | |
element peek(QueueType* Q) { | |
if(isEmpty(Q)) exit(0); | |
return Q -> queue[Q -> front + 1]; | |
} | |
void print(QueueType* Q) { | |
printf("Queue: {"); | |
int i = Q -> front + 1; | |
for(; i <= Q -> rear; ++i) { | |
printf("%3c", Q -> queue[i]); | |
} | |
printf(" }\n"); | |
} | |
int main() { | |
QueueType* Q = createQueue(); | |
printf("A -> EnQueue\n"); | |
enQueue(Q, 'A'); | |
print(Q); | |
printf("B -> EnQueue\n"); | |
enQueue(Q, 'B'); | |
print(Q); | |
printf("C -> EnQueue\n"); | |
enQueue(Q, 'C'); | |
print(Q); | |
printf("Peek -> %c\n", peek(Q)); | |
printf("DeQueue -> %c\n", deQueue(Q)); | |
print(Q); | |
printf("DeQueue -> %c\n", deQueue(Q)); | |
print(Q); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment