栈和队列相关接口实现

 Queue.h

#pragma once

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

typedef int DataType;

typedef struct QueueNode
{
	struct QueueNode* _next;
	DataType _data;
}QueueNode;

typedef struct Queue
{
	QueueNode* _front; // 队头
	QueueNode* _back;  // 队尾
}Queue;

void QueueInit(Queue* pq);//初始化队列
void QueueDestory(Queue* pq);//销毁队列
QueueNode* BuyQueueNode(DataType x);//开辟新结点
void QueuePush(Queue* pq, DataType x);//队尾加入新结点
void QueuePop(Queue* pq);//队头弹出元素
DataType QueueFront(const Queue* pq);//返回队头元素
int QueueEmpty(const Queue* pq);//判断队列是否为空
int QueueSize( const Queue* pq);//返回队列的大小
void TestQueue();//测试函数

Queue.c

#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->_back = NULL;
	pq->_front = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_front;
	QueueNode* del = NULL;
	while (cur){
		del =cur->_next;
		free(cur);
		cur = del;
	}
	//QueueNode* del = pq->_front;
	//QueueNode* cur = NULL;
	//while (del){
	//	cur = del->_next;
	//	free(del);
	//	del = cur;
	//}
}

QueueNode* BuyQueueNode(DataType x)
{
	QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
		perror("BuyQueueNode::malloc");
		return NULL;
	}
	newNode->_data = x;
	newNode->_next = NULL;
	return newNode;
}

void QueuePush(Queue* pq, DataType x)
{
	QueueNode* newNode = NULL;
	assert(pq);
	newNode = BuyQueueNode(x);
	if (pq->_front == NULL)
	{
		pq->_front = newNode;
		pq->_back = newNode;
	}
	else
	{
		pq->_back->_next = newNode;
		pq->_back = pq->_back->_next;
	}
}

void QueuePop(Queue* pq)
{
	QueueNode* cur = NULL;
	assert(pq);
	if (pq->_front == NULL)
		return;
	cur = pq->_front->_next;
	free(pq->_front);
	pq->_front = cur;
}

DataType QueueFront(const Queue* pq)
{
	assert(pq);
	assert(pq->_front);
	return pq->_front->_data;
}

//空0
//非空1
int QueueEmpty(const Queue* pq)
{
	return pq->_front == NULL ? 0 : 1;
}

int QueueSize(const Queue* pq)
{
	int count = 0;
	QueueNode* cur = pq->_front;
	assert(pq);
	while (cur)
	{
		count++;
		cur = cur->_next;
	}
	return count;
}

void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 33);
	QueuePush(&q, 44);
	//QueuePop(&q);
	printf("FrontData is %d\n", QueueFront(&q));
	printf("CountData is %d\n", QueueSize(&q));
	QueueDestory(&q);
}

Stack.h

#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>

typedef int DataType;

typedef struct Stack
{
	DataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

void StackInit(Stack* ps);//初始化栈
void StackDestory(Stack* ps);//销毁栈
void StackPush(Stack* ps, DataType x);//放入元素
void StackPop(Stack* ps);//弹出元素
DataType StackTop(const Stack* ps);//查找栈顶的元素(即将被弹出的元素)
int StackEmpty(const Stack* ps);//判断栈是否为空
int StackSize(const Stack* ps);//返回栈中有多少个元素

Stack.c

#include "Stack.h"

//typedef struct Stack
//{
//	DataType* _a;
//	int _top;		// 栈顶
//	int _capacity;  // 容量 
//}Stack;

void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = (DataType*)malloc(sizeof(DataType)* 3);
	ps->_capacity = 3;
	ps->_top = 0;
}

void StackDestory(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

void StackPush(Stack* ps, DataType x)
{
	//断言
	assert(ps);
	//判断栈是否已满
	if (ps->_top == ps->_capacity)
	{
		ps->_a = realloc(ps->_a, sizeof(DataType)*(ps->_capacity + 3));
		assert(ps->_a);//重新开辟内存失败会返回空指针
		ps->_capacity += 3;
	}
	//放入
	ps->_a[ps->_top] = x;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	//断言
	assert(ps);
	//判空
	if (ps->_top == 0)
	{
		return;
	}
	//删除
	ps->_top--;
}

DataType StackTop(const Stack* ps)
{
	//断言
	assert(ps);
	//判空
	assert(ps->_top);
	//返回栈顶元素
	return ps->_a[ps->_top - 1];
}

int StackEmpty(const Stack* ps)
{
	//断言
	assert(ps);
	//判空
	if (ps->_top == 0)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

int StackSize(const Stack* ps)
{
	//断言
	assert(ps);
	//返回栈中有多少元素
	return ps->_top;
}

test.c

#include "Stack.h"
#include "Queue.h"

int main()
{
	//DataType tmp = 0;
	//Stack s;
	//StackInit(&s); 
	//StackPush(&s, 1); 
	//StackPush(&s, 2);
	//StackPush(&s, 3); 
	//StackPush(&s, 5);
	//StackPush(&s, 3);
	////StackDestory(&s);
	////tmp = StackTop(&s);
	////printf("栈顶的元素是:%d", tmp);
	//if (!StackEmpty(&s))
	//{
	//	printf("栈为空\n");
	//}
	//else
	//{
	//	printf("栈非空\n");
	//}
	//tmp = StackSize(&s);
	//printf("栈中元素个数:%d", tmp);
	TestQueue();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/LiLiLiLaLa/article/details/81639246