栈与队列的实现

栈:一种特殊的线性表,只允许在固定的一端插入或删除元素。进行数据的插入或删除的一端称为栈顶,另一端称为栈低。栈中元素遵循先进后出、后入先出原则(像弹夹一样)

 

//头文件模块
//Stack.h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
struct Stack
{
	int* a;//数组来实现
	int top;//栈顶
	int capacity;//检测空间,不够增容
};
typedef struct Stack ST;
void StackInit(ST* ps);//初始化
void StackDestory(ST* ps);//销毁
void StackPush(ST* ps, int x);//插入,进栈
void StackPop(ST* ps);//删除,出栈
int  StackTop(ST* ps);//取栈里的数据
int  StackSiez(ST* ps);//求栈里的数据个数
bool  StackEmpty(ST* ps);//检测是否为空
//实现模块
//Stack.c
#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = (int*)malloc(sizeof(int) * 4);//开辟4个整形字节
	ps->capacity = 4;//记录4个整形字节
	ps->top = 0;//栈顶为0
}
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
void StackPush(ST* ps, int x)
{
	assert(ps);
	if (ps->top == ps->capacity)//满了扩容,扩2倍
	{
		int* tmp = (int*)realloc(ps->a, ps->capacity * 2 * sizeof(int));
		if (tmp == NULL)
		{
			return 0;
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)//出栈
{
	assert(ps);
	assert(ps->top > 0);//栈为空,直接终止
	ps->top--;
}
int  StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//栈为空时报错
	return ps->a[ps->top - 1];
}
int  StackSiez(ST* ps)//求栈里的数据个数
{
	assert(ps);
	return ps->top;
}
bool  StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//主函数模块
//main.c
#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
int main()
{
	ST s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);
	while (!StackEmpty(&s))//如果栈不为空
	{
		printf("%d ",StackTop(&s));//取出数据
		StackPop(&s);//取出数据后删除
	}
	printf("\n");
	StackDestory(&s);
}

队列:只允许在固定的一端插入或删除数据,遵循先进先出原则

入队列:队尾

出队列:队头

//头文件模块
//Queue.h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
struct Queue
{
	int data;
	struct Queue* next;
};
typedef Queue Qnode;
struct Queue
{
	Queue* head;//头删
	Queue* tail;//尾插
};
typedef Queue Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);//释放
void QueuePush(Queue* pq, int x);//队尾入
void QueuePop(Queue* pq);//对头出
int QueueFront(Queue* pq);//取出对头的数据
int QueueBack(Queue* pq);//取出对尾的数据
int QueueSize(Queue* pq);//计算数据的个数
bool QueueEmpty(Queue* pq);//判断队是否为空

 

//实现模块
//Queue.c
#define  _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}
void QueueDestory(Queue* pq)
{
	assert(pq);
	Queue* cut = pq->head;
	while (cut)
	{
		Queue* next = cut->next;//删掉前先保存下一个
		free(cut);
		cut = next;
	}
	pq->haed = pq->tail = NULL;
}
void QueuePush(Queue* pq, int x)//入队列
{
	assert(pq);
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode != NULL)
	{
		return 0;
	}
	//初始化一下
	newnode->date = x;
	newnode->next = NULL;
	if (pq->tail == NULL)//如果没有头节点
	{
		pq->tail = pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);//出队列要保证不为空
	if (pq->head->tail == NULL)//只剩最后一个结点,防止tail为野指针
	{
		free(pq->head);
		pq->head = pq->tail;
	}
	else
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;//变成新的头
	}
	
}
int QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->adata;
}
int QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->tail->adata;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	Queue* cut = pq->head;
	while (cut)
	{
		size++;
		cut = cut->next;
	}
	return size;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值