队列链表实现:
思路:用front 指向链表的头,用rear 指向链表的尾;
构建一个空的Node节点,用front指向Node节点,rear也指向节点;空节点指向NULL
front->Next=NULL;队列就为空;
插入节点rear->next 指向新创建的节点;rear等于新节点
删除节点:
Q = front->next;
front->next = Q->next;
free(Q);
typedef struct Node* node;
typedef struct QNode* Queue;
struct Node
{
int data;
node next;
};
struct QNode
{
node front;
node rear;
};
Queue creatQueue()
{
Queue ptr = (Queue)malloc(sizeof(struct QNode));
node Q = (node)malloc(sizeof(struct Node));
Q->next = NULL;
ptr->front = Q;
ptr->rear = Q;
return ptr;
}
bool IsEmpty(Queue ptr)//判断是否为空
{
if (ptr->front->next == NULL)
{
return true;
}
return false;
}
void AddQ(Queue ptr, int item)
{
node Q = (node)malloc(sizeof(struct Node));
Q->data = item;
Q->next = ptr->rear->next;
ptr->rear->next = Q;
}
int DelQ(Queue ptr)
{
if (IsEmpty(ptr))
{
printf("队列为空\n");
return -1;
}
int item = ptr->front->next->data;
node Q = ptr->front->next;
ptr->front->next = Q->next;
free(Q);
return item;
}