Nhảy tới nội dung

Cài đặt hàng đợi bằng danh sách liên kết

Kiểu hàng đợi

#include <stdlib.h>

typedef int ElementType;

struct Node
{
ElementType Element;
struct Node *Next;
};

typedef struct Node *Position;

typedef struct
{
Position Front, Rear;
} Queue;

Một số phép toán

makenullQueue

thông tin

Khởi tạo một hàng đợi rỗng.

void makenullQueue(Queue *pS)
{
pS->Front = (Position)malloc(sizeof(struct Node));
pS->Front->Next = NULL;
pS->Rear = pS->Front;
}

emptyQueue

thông tin

Kiểm tra hàng đợi có rỗng hay không.

int emptyQueue(Queue Q)
{
return Q.Front == Q.Rear;
}

front

thông tin

Trả về giá trị ở đầu hàng.

ElementType front(Queue Q)
{
return Q.Front->Next->Element;
}

deQueue

thông tin

Xóa phần tử ở đầu hàng

void deQueue(Queue *pQ)
{
pQ->Front = pQ->Front->Next;
}

enQueue

thông tin

Thêm phần tử x vào cuối hàng.

void enQueue(ElementType x, Queue *pQ)
{
pQ->Rear->Next = (Position)malloc(sizeof(struct Node));
pQ->Rear = pQ->Rear->Next;
pQ->Rear->Element = x;
pQ->Rear->Next = NULL;
}

count

thông tin

Đếm số lượng phần tử trong hàng.

int count(Queue Q)
{
int c = 0;
Position P = Q.Front;
while (P != Q.Rear)
{
c++;
P = P->Next;
}
return c;
}