栈和队列C

Stack.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int top;
	int capacity;//动态增长类型
}ST;
//初始化
void StackInit(ST* ps);
//摧毁
void StackDestroy(ST* ps);
//压栈
void StackPush(ST* ps, STDatatype x);
//出栈
void StackPop(ST* ps);
//栈是否为空
bool StackEmpty(ST* ps);
//栈的元素个数
int StackSize(ST* ps);
//栈顶
STDatatype StackTop(ST* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
//初始化
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;//也可以设为-1
	ps->capacity = 0;
}
//销毁
void StackDestroy(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
//插入
void StackPush(ST* ps, STDatatype x)
{
	//先判断是否增容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDatatype* tmp = realloc(ps->a, sizeof(STDatatype) * newcapacity);
		if (NULL == tmp)
		{
			printf("ralloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
	}
	ps->a[ps->top] = x;
	ps->top++;

}


//删除
void StackPop(ST* ps)
{
	assert(ps);
	//判断是否为空
	assert(!StackEmpty(ps));
	//顺序表实现的栈,直接让top--即可
	ps->top--;
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

Quene.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;
//用链表实现队列
typedef struct QueneNode
{
	struct QueneNode* next;
	QDataType data;
}QueneNode;
//队列只需要记录头和尾
typedef struct Quene
{
	QueneNode* phead;
	QueneNode* ptail;
}Quene;
//初始化
void QueneInit(Quene* pq);
//销毁
void QueneDestory(Quene* pq);
//入队
void QuenePush(Quene* pq, QDataType x);  
//出队
void QuenePop(Quene* pq);               
//队的大小
int QueneSize(Quene* pq);
//队头
QDataType QueneFront(Quene* pq);
//队尾
QDataType QueneBack(Quene* pq);
//队是否为空
bool QueneEmpty(Quene* pq);

Quene.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Quene.h"
void QueneInit(Quene* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
}
void QueneDestory(Quene* pq)
{
	assert(pq);
	//先摧毁链表
	QueneNode* cur = pq->phead;
	while (cur)
	{
		QueneNode* next = cur->next;
		free(cur);
		
		cur = next;
	}
	//摧毁队列
	pq->phead = pq->ptail = NULL;
}
void QuenePush(Quene* pq, QDataType x)
{
	assert(pq);
	QueneNode* newnode = (QueneNode*)malloc(sizeof(QueneNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->ptail == NULL)
		pq->phead = pq->ptail = newnode;
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
}
void QuenePop(Quene* pq)
{
	assert(pq);
	assert(!QueneEmpty(pq));
	if (pq->phead->next = NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QueneNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
}
int QueneSize(Quene* pq)
{
	assert(pq);
	int n = 0;
	QueneNode* cur = pq->phead;
	while (cur)
	{
		cur = cur->next;
		n++;
	}
	return n;
}
QDataType QueneFront(Quene* pq)
{
	assert(pq);
	assert(!QueneEmpty(pq));
	return pq->phead->data;
}
QDataType QueneBack(Quene* pq)
{
	assert(pq);
	assert(!QueneEmpty(pq));
	return pq->ptail->data;
}
bool QueneEmpty(Quene* pq)
{
	assert(pq);
	return pq->ptail == NULL && pq->phead == NULL;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值