栈和队列的实现

一.栈的实现:

1.头文件代码实现:

#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>

typedef int DataType;
typedef struct Stack
{
	DataType* a;
	int top;
	int capacity;
}ST;
void STInit(ST* st);//初始化
void STDestroy(ST* st);//销毁
void STPush(ST* st, DataType x);//入栈
void STPop(ST* st);//出栈
DataType gettop(ST* st);//得到栈顶元素
int STSize(ST* st);//个数
bool STEmpty(ST* st);//判空

2.各接口函数函数的实现:

(1):初始化和销毁

void STInit(ST* st)//初始化
{
	assert(st);
	st->a = NULL;
	st->top = st->capacity = 0;
}
void STDestroy(ST* st)//销毁
{
	assert(st);
	free(st->a);
	st->a = NULL;
	st->top = st->capacity = 0;
}
(2):入栈和出栈
void STPush(ST* st, DataType x)//入栈
{
	assert(st);
	if (st->top == st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
		DataType* tmp = (DataType*)realloc(st->a, sizeof(DataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("");
			return;
		}
		st->a = tmp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	st->top++;
}
void STPop(ST* st)//出栈
{
	assert(st);
	assert(st->top > 0);
	st->top--;
}
(3).得到栈顶元素:
DataType gettop(ST* st)//得到栈顶元素
{
	assert(st && st->top > 0);
	return st->a[st->top - 1];
}
(4).得到元素个数:
int STSize(ST* st)//个数
{
	assert(st);
	return st->top;
}
(5)判空
bool STEmpty(ST* st)//判空
{
	assert(st);
	return st->top == 0;
}
(6)遍历栈
void STPrint(ST* ps)//打印
{
	while (STEmpty(ps) == 0)
	{
		printf("%d ", STTop(ps));
		STPop(ps);
	}
	printf("\n");
}

二.队列的实现

1.头文件形式:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int DataType;
typedef struct QueueNode
{
	DataType val;
	struct QueueNode* next;
}QN;
typedef struct Queue
{
	QN* phead;
	QN* ptail;
	int size;
}Queue;

void QNInit(Queue* pq);//初始化
void QNDestroy(Queue* pq);//销毁
void QNPush(Queue* pq, DataType x);//入队
void QNPop(Queue* pq);//出队
DataType QNFront(Queue* pq);//队首元素
DataType QNBack(Queue* pq);//队尾元素
int QNSize(Queue* pq);//个数
bool QNEmpty(Queue* pq);//判空
2.各接口函数的实现:
(1)初始化和销毁
void QNInit(Queue* pq)//初始化
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
void QNDestroy(Queue* pq)//销毁
{
	assert(pq);
	QN* cur = pq->phead;
	while (cur)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
(2)入队和出队
void QNPush(Queue* pq, DataType x)//入队
{
	assert(pq);
	QN* node = (QN*)malloc(sizeof(QN));
	if (node == NULL)
	{
		perror("");
		return;
	}
	node->next = NULL;
	node->val = x;
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = node;
	}
	else
	{
		pq->ptail->next = node;
		pq->ptail = node;
	}
	pq->size++;
}
void QNPop(Queue* pq)//出队
{
	assert(pq);
	assert(pq->phead);
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else {
		QN* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}
(3)得到队头或队尾:
DataType QNFront(Queue* pq)//队首元素
{
	assert(pq && pq->phead);
	return pq->phead->val;
}
DataType QNBack(Queue* pq)//队尾元素
{
	assert(pq && pq->ptail);
	return pq->ptail->val;
}
(4)得到队员个数
int QNSize(Queue* pq)//个数
{
	assert(pq);
	return pq->size;
}
(5)判空
bool QNEmpty(Queue* pq)//判空
{
	assert(pq);
	return pq->size == 0;
}
(6)遍历队列
void print(Queue* pq)
{
	assert(pq);
	while (QNEmpty(pq) == 0)
	{
		printf("%d ", QNFront(pq));
		QNPop(pq);
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值