1. Queue in Array
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10000
#define INF		99999
int queue[SIZE];
int front = 0;
int rear = 0;
void push(int data) {
	if (rear >= SIZE - 1) {
		printf("Queue Overflow !!");
		return;
	}
	queue[rear++] = data;
}
int pop() {
	if (front == rear) {
		printf("Queue Underflow !!");
		return -INF;
	}
	return queue[front++];
}
void show() {
	printf("Front Of Queue \n");
	for (int i = front; i < rear; i++) {
		printf("%d \n", queue[i]);
	}
	printf("Rear Of Queue \n");
}
int main() {
	push(2);
	push(1);
	push(5);
	push(7);
	push(6);
	pop();
	push(8);
	pop();
	push(3);
	show();
	system("pause");
}

2. Queue in LinkedList
#include <stdio.h>
#include <stdlib.h>
#define INF		99999
typedef struct {
	int data;
	struct Node *next;
}Node;
typedef struct {
	struct Node *front;
	struct Node *rear;
	int count;
} Queue;
void push(Queue *queue, int data) {
	Node *node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	if (queue->count == 0) {
		queue->front = node;
	}
	else {		  
		Node *rearNode = (Node*)malloc(sizeof(Node));
		rearNode = queue->rear;
		rearNode->next = node;
	}			  
	queue->rear = node;	
	queue->count++;
}
int pop(Queue *queue) {
	if (queue->count == 0) {
		printf("Queue underflow !! \n");
		return -INF;
	}
	Node *node = (Node*)malloc(sizeof(Node));
	node = queue->front;
	int data = node->data;
	queue->front = node->next;
	free(node);
	queue->count--;
	return data;
}
void show(Queue *queue) {
	if (queue->count == 0) {
		printf("No data in Queue !! \n");
		return -INF;
	}
	Node* cur = queue->front;
	printf("Front Of Queue \n");
	while (cur != NULL) {
		printf("%d \n", cur->data);
		cur = cur->next;
	}
	printf("Rear Of Queue \n");
}
int main() {
	Queue queue;
	queue.count = 0;
	queue.front = NULL;
	queue.rear = NULL;
	push(&queue, 2);
	push(&queue, 1);
	push(&queue, 5);
	push(&queue, 7);
	push(&queue, 6);
	pop(&queue);
	push(&queue, 8);
	pop(&queue);
	push(&queue, 3);
	show(&queue);
	system("pause");
	return 0;
}
be the happy Gosu.
woojja ))*
반응형
    
    
    
  'ETC > C, C++' 카테고리의 다른 글
| [C, C++] Counting Sort (2) | 2022.02.09 | 
|---|---|
| [C, C++] Selection Sort, Insertion Sort (0) | 2021.11.04 | 
| [C, C++] Stack in Array, Lined List (0) | 2021.10.20 | 
| [C, C++] Sorted Doubly Linked List (0) | 2021.10.20 | 
