数据结构 队列

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

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


#define	SUCCESS		0
#define FAILURE		1

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

typedef struct LinkQueue{
	struct Node head;
	int size;
}STLinkQueue_def;

typedef void (*QUEUEPRINT)(void*);
//队列初始化
STLinkQueue_def* queue_init()
{
	int i = 0;
	STLinkQueue_def* queue_ptr = (STLinkQueue_def*)malloc(sizeof(STLinkQueue_def));
	queue_ptr->size = 0;
	queue_ptr->head.next = NULL;
	
	return queue_ptr;
}

//队列尾部插入元素
int queue_push(STLinkQueue_def* queue, STNode_def* data)
{
	if (queue == NULL)
	{
		printf("param is null\n");
		return FAILURE;
	}
	STNode_def* p = &(queue->head);
	while (p->next != NULL)
	{
        p = p->next;
    }
    
    p->next = data;
    data->next = NULL;
    
	queue->size++;
	return 0;
}


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

	if (queue->size == 0)
	{
		return FAILURE;
	}
    
	STNode_def* p = &(queue->head);
    queue->head.next = p->next->next;
	queue->size--;

	return 0;
}


//获取队头元素
void* queue_front(STLinkQueue_def* queue)
{
	if (queue == NULL)
	{
		printf("param is null\n");
		return NULL;
	}
    
	return queue->head.next;
}

//获取队尾元素
void* queue_back(STLinkQueue_def* queue)
{
	if (queue == NULL)
	{
		printf("param is null\n");
		return NULL;
	}
    
    STNode_def *p = &(queue->head);
    while (p->next != NULL) 
    {
        p = p->next;
    }

	return p;
}

//队列是否为空
int queue_empty(STLinkQueue_def* queue)
{
	return queue->size>0 ?0:1;
}


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

	queue->head.next = NULL;
	queue->size = 0;

	return SUCCESS;
}

//释放队列
void queue_destory(STLinkQueue_def* queue)
{
	free(queue);
}

//遍历队列
int queue_print(STLinkQueue_def* queue, void (*QUEUEPRINT)(STNode_def*))
{

	if (queue == NULL)
	{
		printf("param is null\n");
		return FAILURE;
	}
	STNode_def* p = &(queue->head);
	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()
{
	STLinkQueue_def* queue= queue_init();
	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;
}
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页