带头结点的链队列实现(C语言)

点击打开链接

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0


typedef struct queue_node
{  
    int data;  
    struct queue_node *next;  
}qnode;  
 
typedef struct link_queue  
{  
    qnode *head;  
    qnode *tail;  
}lqueue;  

/* 
函数功能:   链队列初始化 
*/  
int init_link_queue(lqueue *Q)  
{  
    qnode *tmp_node = (qnode *)malloc(sizeof(qnode));  
    if(!tmp_node)  
    {  
        return ERROR;  
    }  
  
    tmp_node->next = NULL;  
    Q->head = tmp_node;  
    Q->tail = tmp_node;  
  
    return OK;  
}  
  
/* 
函数功能:   队列是否为空 
返 回 值:  1 空; 0 非空 
*/  
int is_queue_empty(lqueue Q)  
{  
    return ((Q.head == Q.tail) ? TRUE : FALSE);  
}  
  
/* 
函数功能:   元素num入队 
*/  
int enqueue(lqueue *Q, int num)  
{  
    qnode *tmp_node = (qnode *)malloc(sizeof(qnode));  
    if(!tmp_node)  
    {  
        return ERROR;  
    }  
  
    tmp_node->next = NULL;  
    tmp_node->data = num;  
    Q->tail->next = tmp_node;  
    Q->tail = tmp_node;  
  
    return OK;  
}  
 
/* 
函数功能:   出队,队头元素存入num 
*/  
int dequeue(lqueue *Q, int *num)  
{  
    qnode *tmp_node = NULL;  
    if(is_queue_empty(*Q))  
    {  
        return ERROR;  
    }  
 
    tmp_node = Q->head->next;  
    Q->head->next = tmp_node->next;  //队头出队之后,结点减少一个,更新队头位置  
    *num = tmp_node->data;  
  
    if(Q->head->next == NULL)  
    {  
        Q->tail = Q->head;  
    }  
  
    free(tmp_node);

    return OK;  
}  
  
/* 
函数功能:   获取队头元素,存入num 
*/  
int get_elem(lqueue Q, int *num)  
{  
    if(is_queue_empty(Q))  
    {  
        return ERROR;  
    }  
  
    *num = Q.head->next->data;  
  
    return OK;  
}  
  
/* 
函数功能:   打印队列元素 
*/  
void print_queue(lqueue Q)  
{  
    lqueue tmpQ = Q;  
    tmpQ.head = (qnode *)malloc(sizeof(qnode));  
    tmpQ.head = Q.head->next;  
    printf("Head->*->");  
  
    while(tmpQ.head != NULL)  
    {  
        printf("%d->", tmpQ.head->data);  
        tmpQ.head = tmpQ.head->next;  
    }  

    free(tmpQ.head);  
    printf("TAIL\n");  
}  
  
/* 
函数功能:   销毁队列 
*/  
void destory_queue(lqueue *Q)  
{  
    qnode *tmp_node = NULL;  
    while(Q->head->next != NULL)  
    {  
        tmp_node = Q->head->next;  
        Q->head->next = tmp_node->next;  
  
        free(tmp_node);  
    }  
}  
  
/* 
函数功能:   获得队列长度 
返 回 值:  长度 
*/  
int get_queue_length(lqueue Q)  
{  
    int length = 0;  
    lqueue tmpQ;  
    tmpQ.head = (qnode *)malloc(sizeof(qnode));  
    tmpQ.head = Q.head->next;  
  
    if(is_queue_empty(Q))  
    {  
        return length;  
    }  
  
    do   
    {  
        length++;  
        tmpQ.head = tmpQ.head->next;  
    }while(tmpQ.head != NULL);  
 
    free(tmpQ.head);  
  
    return length;  
}  

int main(int argc, char *argv[])  
{  
    int num = 0;  
    lqueue queue;  
  
    init_link_queue(&queue);

    enqueue(&queue, 7);  
    enqueue(&queue, 8);  
    enqueue(&queue, 9);  
    enqueue(&queue, 6);  
    printf("length = %d\n", get_queue_length(queue));  
    print_queue(queue);  
  
    dequeue(&queue, &num);    
    printf("length = %d, dequeue num = %d\n", get_queue_length(queue), num);  
    print_queue(queue);  
  
    destory_queue(&queue);  
    return 0;  
}  

运行结果:


头结点链队列是一种链式存储结构,它通过一个头结点来标识队列的头和尾。下面是头结点链队列的基本操作: 1.初始化操作 头结点链队列的初始化操作就是创建一个头结点,并让头结点的 next 指针指向 NULL。代码实现如下: ``` typedef struct Node { int data; struct Node* next; } Node; typedef struct { Node* front; // 队头指针 Node* rear; // 队尾指针 } LinkQueue; void InitQueue(LinkQueue* Q) { Q->front = Q->rear = (Node*)malloc(sizeof(Node)); Q->front->next = NULL; } ``` 2.入队操作 在头结点链队列中,新元素需要插入到队尾,因此需要修改队尾指针的指向。代码实现如下: ``` void EnQueue(LinkQueue* Q, int x) { Node* s = (Node*)malloc(sizeof(Node)); s->data = x; s->next = NULL; Q->rear->next = s; Q->rear = s; } ``` 3.出队操作 出队操作需要删除队头元素,并修改队头指针的指向。代码实现如下: ``` int DeQueue(LinkQueue* Q) { if (Q->front == Q->rear) return -1; // 队列为空 Node* p = Q->front->next; int x = p->data; Q->front->next = p->next; if (Q->rear == p) Q->rear = Q->front; // 如果队列只有一个元素,删除后队列为空 free(p); return x; } ``` 4.获取队头元素 获取队头元素需要先判断队列是否为空,如果不为空就返回队头元素的值。代码实现如下: ``` int GetHead(LinkQueue* Q) { if (Q->front == Q->rear) return -1; // 队列为空 Node* p = Q->front->next; return p->data; } ``` 5.判断队列是否为空 判断队列是否为空只需要判断头结点的 next 指针是否为 NULL。代码实现如下: ``` int IsEmpty(LinkQueue* Q) { return Q->front == Q->rear; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值