C语言栈和队列

栈:

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

特点:先进后出

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

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();

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];
}
 
//空0 非空1
int StackEmpty(Stack* ps) {
	assert(ps);
	return ps -> _top == 0 ? 0 : 1;
}
 
int StackSize(Stack* ps) {
	assert(ps);
	return ps -> _top;
}
队列:

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

特点:先进先出

#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);

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;
}

//空返回0,非空返回1
int QueueEmpty(Queue* pq) {
	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;
}

以下为测试用代码:

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;
}
运行结果为:
5 4 3 2 1
1 2 3 4 5
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值