# 数据结构 队列

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

//链式栈：使用企业式链表存储元素

#define	SUCCESS		0
#define FAILURE		1

typedef struct Node{
struct Node *next;
}STNode_def;

int size;

typedef void (*QUEUEPRINT)(void*);
//队列初始化
{
int i = 0;
queue_ptr->size = 0;

return queue_ptr;
}

//队列尾部插入元素
{
if (queue == NULL)
{
printf("param is null\n");
return FAILURE;
}
while (p->next != NULL)
{
p = p->next;
}

p->next = data;
data->next = NULL;

queue->size++;
return 0;
}

//队列头部删除元素
{
if (queue == NULL)
{
printf("param is null\n");
return FAILURE;
}

if (queue->size == 0)
{
return FAILURE;
}

queue->size--;

return 0;
}

//获取队头元素
{
if (queue == NULL)
{
printf("param is null\n");
return NULL;
}

}

//获取队尾元素
{
if (queue == NULL)
{
printf("param is null\n");
return NULL;
}

while (p->next != NULL)
{
p = p->next;
}

return p;
}

//队列是否为空
{
return queue->size>0 ?0:1;
}

//清空队列
{
if (queue == NULL)
{
printf("param is null\n");
return FAILURE;
}

queue->size = 0;

return SUCCESS;
}

//释放队列
{
free(queue);
}

//遍历队列
{

if (queue == NULL)
{
printf("param is null\n");
return FAILURE;
}
while (p->next != NULL)
{
p = p->next;
QUEUEPRINT(p);
}

return SUCCESS;
}

typedef struct Student{
STNode_def node;
char name[64];
int age;
}STStudent_def;

void student_print(STStudent_def* info)
{
printf("name:%s, age:%d\n", info->name, info->age);
}

int main()
{
STStudent_def st1;
STStudent_def st2;
STStudent_def st3;
STStudent_def st4;
STStudent_def st5;
STStudent_def* st6 = NULL;
STStudent_def* st7 = NULL;

strcpy(st1.name, "刘备");
st1.age = 40;

strcpy(st2.name, "关羽");
st2.age = 35;

strcpy(st3.name, "张飞");
st3.age = 30;

strcpy(st4.name, "赵云");
st4.age = 25;

strcpy(st5.name, "诸葛亮");
st5.age = 45;

printf("empty:%d\n", queue_empty(queue));

queue_push(queue, &st1);
queue_push(queue, &st2);
queue_push(queue, &st3);
queue_push(queue, &st4);
queue_push(queue, &st5);

printf("empty:%d\n", queue_empty(queue));

st6 = (STStudent_def*)queue_back(queue);
printf("st6.name=%s, st6.age=%d\n", st6->name, st6->age);

st7 = (STStudent_def*)queue_front(queue);
printf("st7.name=%s, st7.age=%d\n", st7->name, st7->age);

queue_print(queue, student_print);
printf("empty:%d\n", queue_empty(queue));

queue_clear(queue);
printf("empty:%d\n", queue_empty(queue));

queue_destory(queue);
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

最后冰吻free

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
03-19 2万+

08-12 1万+
03-12 427
05-04 1360
01-29