C语言实现栈和队列(栈和队列的基本操作)

栈:

栈:栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底

特点:先进后出

stack.h

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

//
//typedef int DataType;
//
//#define N 10
//typedef struct Stack
//{
//	DataType _a[N];
//	int _top; // 栈顶
//}Stack;


typedef int DataType;
#define N 10
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(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);

void TestStack();

stack.c

#include "Stack.h"

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

void StackDestory(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	ps->_top = 0;
	ps->_capacity = 0;
	free(ps);
	ps = NULL;
}
void StackPush(Stack* ps, DataType x)
{
	DataType* cur;
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		cur = (DataType*)realloc(ps->_a, sizeof(DataType) * 2 * ps->_capacity);
		if (cur != NULL)
		{
			ps->_a = cur;
		}
		ps->_capacity = 2 * ps->_capacity;
	}
	ps->_a[ps->_top] = x;
	ps->_top++;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;

}


DataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top - 1];
}


int StackEmpty(Stack* ps)//空0 非空1
{
	assert(ps);
	return ps->_top == 0 ? 0 : 1;
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}

队列:

队列(queue)是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

特点:先进先出

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(Queue* pq);
int QueueEmpty(Queue* pq);

int QueueSize(Queue* pq);

Queue.c

#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->_front = NULL;
	pq->_back = NULL;
		
}
void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode*cur = pq->_front;
	while (cur)
	{
		QueueNode*tmp = cur->_next;
		free(cur);
		cur = tmp;
	}
	pq->_front = pq->_back = NULL;
}
QueueNode* BuyQueueNode(DataType x)
{
	QueueNode*tmp = (QueueNode*)malloc(sizeof(QueueNode));
	if (tmp == NULL)
	{
		perror("use malloc");
	}
	tmp->_next = NULL;
	tmp->_data = x;
	return tmp;

}
void QueuePush(Queue* pq, DataType x)
{
	assert(pq);
	if (pq->_front == NULL)
	{
		QueueNode*next = BuyQueueNode(x);
		pq->_front = next;
		pq->_back = next;
	}
	else
	{
		QueueNode*next = BuyQueueNode(x);
		pq->_back->_next = next;
		pq->_back = next;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	QueueNode*next = pq->_front->_next;
	free(pq->_front);
	pq->_front = next;
}
DataType QueueFront(Queue* pq)
{
	assert(pq);
	return pq->_front->_data;
}
int QueueEmpty(Queue* pq)////空返回0,非空返回1
{
	assert(pq);
	return pq->_front == NULL ? 0 : 1;

}

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


下面是栈和队列的测试函数(test.c)

#include"Stack.h"
#include"Queue.h"
void test1()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);
	while (StackEmpty(&s) != 0)
	{
		printf("%d ", StackTop(&s));
		StackPop(&s);
	}
	printf("\n");
}
void test2()
{
	Queue s;
	QueueInit(&s);
	QueuePush(&s, 1);
	QueuePush(&s, 2);
	QueuePush(&s, 3);
	QueuePush(&s, 4);
	QueuePush(&s, 5);
	while (	QueueEmpty(&s) != 0)
	{
		printf("%d ", QueueFront(&s));
		QueuePop(&s);
	}
}

int main()
{
	test1();
	test2();
	return 0;
}

运行结果如下

猜你喜欢

转载自blog.csdn.net/Damn_Yang/article/details/83928852