728x90
반응형
#include<stdio.h>
#include<stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct LinkedNode {
int data;
struct LinkedNode* link;
} Node;
Node* front = NULL;
Node* rear = NULL;
void init_queue() { front = rear = NULL; }
int is_empty() { return front == NULL; }
int size() {
Node* p;
int count = 0;
for (p = front; p != NULL; p = p->link) count++;
return count;
}
void enqueue(int val) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
p->link = NULL;
if (is_empty()) front = rear = p;
else {
rear->link = p;
rear = p;
}
}
int dequeue() {
int e = 0;
if (!is_empty()) {
Node* p = front;
front = front->link;
if (front == NULL) rear = NULL;
e = p->data;
free(p);
}
return e;
}
int peek() {
if (!is_empty()) {
return front->data;
}
}
void destroy_queue() {
while (!is_empty()) dequeue();
}
void print_queue(char msg[]) {
Node* p;
printf("%s[%2d]= ", msg, size());
for (p = front; p != NULL; p = p->link) {
printf("%2d ", p->data);
}
printf("\n");
}
void main() {
int i;
init_queue();
for (int i = 1; i < 10; i++) {
enqueue(i);
}
print_queue("연결된큐 equeue 9회");
printf("\tdequeue() --> %d\n", dequeue());
printf("\tdequeue() --> %d\n", dequeue());
printf("\tdequeue() --> %d\n", dequeue());
print_queue("연결된큐 dqueue 3회");
destroy_queue();
print_queue("연결된큐 destroy_queue ");
}
728x90
반응형