Linux学习——数据结构之链栈

目录

test.c

linkqueue.c

linkqueue.h


test.c

#include <stdio.h>
#include "linkqueue.h"

int main(int argc, const char *argv[])
{
	linkqueue *lq;

	lq = queue_create();
	if (lq == NULL) 
		return -1;

	enqueue(lq, 10);
	enqueue(lq, 20);
	enqueue(lq, 30);
	enqueue(lq, 40);

	//while (!queue_empty(lq)) {
		//printf("dequeue:%d\n", dequeue(lq));
	//}
	queue_clear(lq);

	lq = queue_free(lq);
	enqueue(lq, 50);

	return 0;
}

linkqueue.c

#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"
linkqueue * queue_create()
{
	linkqueue *lq;
	if ((lq = (linkqueue*)malloc(sizeof(linkqueue))) == NULL)
	{
		printf("malloc linkqueue is default");
		return NULL;
	}
	if ((lq->front = lq->rear = (linklist)malloc(sizeof(listnode))) == NULL)
	{
		printf("malloc lq->rear&lq->front is default");
		return NULL;
	}
	lq->front->next = NULL;
	lq->front->data = 0;
	//lq->rear = lq->front; 
	return lq;
}
int enqueue(linkqueue *lq, datatype x)
{
	linklist p;
	//p->fr = (linkqueue*)malloc(sizeof(linkqueue));
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
		printf("malloc node failed\n");
		return -1;
	}
	p->data = x;
	p->next = NULL;	
	lq->rear->next = p;
	lq->rear = p;
	//lq->rear->data = x;
	return 0;
}
datatype dequeue(linkqueue *lq)
{
	linklist p;
	datatype x;
	if(lq == NULL)
	{
		printf("lq is NULL");
		return -1;
	}
	p = lq->front;
	
	lq->front = lq->front->next;
	free(p);
	p = NULL;
	x = lq->front->data;
	return x;
}
int queue_empty(linkqueue *lq)
{
	if(lq == NULL)
	{
		printf("lq is NULL");
		return -1;
	}
	return (lq->front == lq->rear ? 1 : 0);
}
#if 1
int queue_clear(linkqueue *lq)
{
	linklist p;
	if(lq == NULL)
	{
		printf("lq is NULL");
		return -1;
	}
	for(;lq->front != lq->rear;)
	{
		p = lq->front;
		lq->front = lq->front->next;
		printf("clear free:%d\n", p->data);
		free(p);
		p = NULL;
	}
	return 0;	
}
#endif
#if 0
int queue_clear(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	while (lq->front->next) {
		p = lq->front;
		lq->front = p->next;
		printf("clear free:%d\n", p->data);
		free(p);
		p = NULL;
	}
	return 0;
}
#endif
#if 0
linkqueue * queue_free(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return NULL;
	}

	while (lq->front) {
		p = lq->front;
		lq->front = p->next;
		printf("free:%d\n", p->data);
		free(p);
	}

	free(lq);
	lq = NULL;

	return NULL;
}
#endif
#if 1
linkqueue * queue_free(linkqueue *lq)
{
	linklist p;
	if(lq == NULL)
	{
		printf("lq is NULL");
		return NULL;
	}
	for(;lq->front != NULL;)
	{
		p = lq->front;
		lq->front = lq->front->next;
		printf("free:%d\n", p->data);
		free(p);
		p = NULL;
	}
	free(lq);
	lq = NULL;
	return NULL;
}






#endif





linkqueue.h

typedef struct node {
	datatype data;
	struct node *next;
}listnode , *linklist;

typedef struct {
	linklist front;
	linklist rear;
}linkqueue;

linkqueue * queue_create();
int enqueue(linkqueue *lq, datatype x);
datatype dequeue(linkqueue *lq);
int queue_empty(linkqueue *lq);
int queue_clear(linkqueue *lq);
linkqueue * queue_free(linkqueue *lq);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇努力学习

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值