1. 佇列模型
2. 佇列陣列實現
- 陣列實現在建立時候需要比連結串列多傳入一個 max_size ,即需要指定陣列的長度。
- 陣列存放佇列內容,size - 佇列長度, front - 隊頭,rear - 隊尾,同時都與陣列下標對應。
enqueue
值存入陣列rear + 1
位置,同時rear + 1
,size + 1
,如果超過右邊界,則從 0 開始。dequeue
返回front + 1
位置的值,同時front + 1
,size - 1
如果超過右邊界,則從 0 開始。
結構體定義typedef int element_type; typedef struct queue_record { int capacity; //陣列長度,佇列最大長度 int size; //佇列長度 int front; //隊頭 int rear; //隊尾 element_type *arr; } *queue;
函式定義
queue create_queue(int max_size); void make_empty(queue q); int is_full(queue q); int is_empty(queue q); void enqueue(element_type x, queue q); void dequeue(queue q); element_type front(queue q); element_type front_and_dequeue(queue q); void dispose_queue(queue *q);
完整程式碼:
/**
* 佇列ADT-陣列實現
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE (5)
#define error(str) fatal_error(str)
#define fatal_error(str) fprintf(stderr, "%s\n", str),exit(1)
typedef int element_type;
typedef struct queue_record
{
int capacity;
int size; //佇列長度
int front; //隊頭
int rear; //隊尾
element_type *arr;
} *queue;
queue create_queue(int max_size);
void make_empty(queue q);
int is_full(queue q);
int is_empty(queue q);
void enqueue(element_type x, queue q);
void dequeue(queue q);
element_type front(queue q);
element_type front_and_dequeue(queue q);
void dispose_queue(queue *q);
void print_queue(queue q);
void test();
queue create_queue(int max_size)
{
queue q;
if (max_size < MAX_QUEUE_SIZE)
error("max_len is too small.");
q = (queue)malloc(sizeof(struct queue_record));
if (NULL == q)
fatal_error("out of space.");
q->capacity = max_size;
q->arr = (element_type*)malloc(sizeof(element_type) * max_size);
make_empty(q);
return q;
}
void make_empty(queue q)
{
q->size = 0;
q->front = 1;
q->rear = 0;
}
int is_full(queue q)
{
return q->size == q->capacity;
}
int is_empty(queue q)
{
return q->size == 0;
}
void enqueue(element_type x, queue q)
{
if (NULL == q)
fatal_error("Must create queue");
if (is_full(q)){
error("Full queue");
} else {
q->size++;
if (++q->rear == q->capacity)
q->rear = 0;
q->arr[q->rear] = x;
}
}
void dequeue(queue q)
{
if (NULL == q)
fatal_error("Must create queue");
if (is_empty(q)){
error("Empty queue");
} else {
q->size--;
if (++q->front == q->capacity)
q->front = 0;
}
}
element_type front(queue q)
{
if (!is_empty(q))
return q->arr[q->front];
error("empty queue");
return 0;
}
element_type front_and_dequeue(queue q)
{
if (!is_empty(q)) {
element_type ret = q->arr[q->front];
dequeue(q);
return ret;
}
error("empty queue");
return 0;
}
void print_queue(queue q)
{
int i;
for (i = q->front; i <= q->size; i++) {
printf("%d\t", q->arr[i]);
}
printf("\n");
}
void dispose_queue(queue *q)
{
if ( NULL != (*q) ) {
free((*q)->arr);
free((*q));
(*q) = NULL;
}
}
void test()
{
queue q;
int act;
while (1) {
printf("\n\tplease input a intger and choose a option.\n");
printf("\t\t1.create a empty queue.\n");
printf("\t\t2.enqueue a element to queue.\n");
printf("\t\t3.dequeue a element from queue.\n");
printf("\t\t4.get the front element of queue.\n");
printf("\t\t5.print queue.\n");
printf("\t\t6.dispose queue.\n");
printf("\t\t0.exit\n");
scanf("%d", &act);
if (act == 0)
break;
switch(act) {
case 1:{
int max_len;
printf("please input max_len of stack.\n");
scanf("%d", &max_len);
q = create_queue(max_len);
printf("create queue success\n");
break;
}
case 2: {
int x;
printf("please input a intger.\n");
scanf("%d", &x);
enqueue(x, q);
printf("enqueue success\n");
print_queue(q);
break;
}
case 3:
dequeue(q);
printf("dequeue a element success\n");
break;
case 4:
printf("queue front:%d\n", front(q));
break;
case 5:
print_queue(q);
break;
case 6:
dispose_queue(&q);
printf("dispose queue success\n");
break;
}
}
}
int main(int argc, char const *argv[])
{
test();
return 0;
}
3. 佇列連結串列實現
- 連結串列每個節點表示佇列的一個元素
- 入隊
enqueue
新節點插入到末尾。 - 出隊
dequeue
返回開頭的元素,即 header->next。
結構定義
typedef int element_type;
typedef struct node
{
element_type element;
struct node *next;
} *queue;
全部程式碼實現:
/**
* 佇列 ADT-連結串列實現
*/
#include <stdio.h>
#include <stdlib.h>
#define error(str) fatal_error(str)
#define fatal_error(str) fprintf(stderr, "%s\n", str),exit(1)
typedef int element_type;
typedef struct node *ptr_to_node;
typedef ptr_to_node queue;
struct node
{
element_type element;
ptr_to_node next;
};
queue create_queue();
void make_empty(queue q);
int is_empty(queue q);
void enqueue(element_type x, queue q);
void dequeue(queue q);
element_type front(queue q);
element_type front_and_dequeue(queue q);
void dispose_queue(queue *q);
void test();
int main(void)
{
test();
return 0;
}
queue create_queue()
{
queue q;
q = (queue)malloc(sizeof(struct node));
if (NULL == q)
fatal_error("Out of space");
q->next = NULL;
make_empty(q);
return q;
}
void make_empty(queue q)
{
if (NULL == q)
error("must create queue");
else
while (!is_empty(q)) {
dequeue(q);
}
}
int is_empty(queue q)
{
return q->next == NULL;
}
void enqueue(element_type x, queue q)
{
if (NULL == q)
fatal_error("must create a queue");
queue temp_cell, p;
temp_cell = (queue)malloc(sizeof(struct node));
if (NULL == temp_cell)
fatal_error("out of space");
temp_cell->element = x;
temp_cell->next = NULL;
p = q;
while (NULL != p->next)
p = p->next;
p->next = temp_cell;
}
void dequeue(queue q)
{
if (NULL == q)
fatal_error("must create a queue");
queue temp;
temp = q->next;
q->next = temp->next;
free(temp);
temp = NULL;
}
element_type front(queue q)
{
if ( !is_empty(q) )
return q->next->element;
error("empty queue");
return 0;
}
element_type front_and_dequeue(queue q)
{
if ( !is_empty(q) ){
element_type ret = q->next->element;
dequeue(q);
return ret;
}
error("empty queue");
return 0;
}
void dispose_queue(queue *q)
{
make_empty((*q));
free((*q));
(*q) = NULL;
}
void print_queue(queue q)
{
if (NULL == q)
fatal_error("must be a queue");
queue p;
p = q->next;
printf("隊頭\t");
while(p) {
printf("\t%d", p->element);
p = p->next;
}
printf("\t隊尾\n");
}
void test()
{
queue q;
int act;
while (1) {
printf("\n\tplease input a intger and choose a option.\n");
printf("\t\t1.create a empty queue.\n");
printf("\t\t2.enqueue a element to queue.\n");
printf("\t\t3.dequeue a element from queue.\n");
printf("\t\t4.get the front element of queue.\n");
printf("\t\t5.print queue.\n");
printf("\t\t6.dispose queue.\n");
printf("\t\t0.exit\n");
scanf("%d", &act);
if (act == 0)
break;
switch(act) {
case 1:
q = create_queue();
printf("create queue success\n");
break;
case 2: {
int x;
printf("please input a intger.\n");
scanf("%d", &x);
enqueue(x, q);
printf("enqueue success\n");
print_queue(q);
break;
}
case 3:
dequeue(q);
printf("dequeue a element success\n");
break;
case 4:
printf("queue front:%d\n", front(q));
break;
case 5:
print_queue(q);
break;
case 6:
dispose_queue(&q);
printf("dispose queue success\n");
break;
}
}
}