Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
queue_list.c 1.94 KB
Copy Edit Raw Blame History
胡宇彪 authored 2019-09-01 12:16 . 表、树、图
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define TYPE int
typedef struct Node
{
TYPE data;
struct Node* next;
}Node;
//创建节点
Node *creat_node(TYPE data)
{
Node* node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
typedef struct Queue
{
Node *head;
Node *tail;
size_t size;
}Queue;
//创建队
Queue* creat_queue(void);
//销毁
void destory_queue(Queue* queue);
//入队
void push_queue(Queue* queue,TYPE data);
//出队
bool pop_queue(Queue* queue);
//判断队空
bool empty_queue(Queue* queue);
//查看队头
TYPE* head_queue(Queue* queue);
//查看队尾
TYPE* tail_queue(Queue* queue);
/*
int main()
{
Queue* queue = creat_queue();
for(int i = 0 ; i < 10 ; i++)
{
push_queue(queue,i);
printf("队尾:%d 队头:%d\n",*tail_queue(queue),*head_queue(queue));
}
for(int i = 0 ; i < 5 ; i++)
{
pop_queue(queue);
printf("队尾:%d 队头:%d\n",*tail_queue(queue),*head_queue(queue));
}
destory_queue(queue);
return 0;
}
*/
//创建队
Queue* creat_queue(void)
{
Queue* queue = malloc(sizeof(Queue));
queue->head = NULL;
queue->tail = NULL;
queue->size = 0;
return queue;
}
//销毁
void destory_queue(Queue* queue)
{
while(pop_queue(queue));
free(queue);
}
//入队
void push_queue(Queue* queue,TYPE data)
{
Node *node = creat_node(data);
if(0 == queue->size)
{
queue->tail = node;
queue->head = node;
}
else
{
queue->tail->next = node;
queue->tail = node;
}
queue->size++;
}
//出队
bool pop_queue(Queue* queue)
{
if(empty_queue(queue))
return false;
Node *node = queue->head;
queue->head = node->next;
free(node);
queue->size--;
return true;
}
//判断队空
bool empty_queue(Queue* queue)
{
return NULL == queue->head;
}
//查看队头
TYPE* head_queue(Queue* queue)
{
if(empty_queue(queue))
return NULL;
return (TYPE *)queue->head;
}
//查看队尾
TYPE* tail_queue(Queue* queue)
{
if(empty_queue(queue))
return NULL;
return (TYPE *)queue->tail;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化