栈、队列和数组(总)

顺序栈(固定容量)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//创建	销毁
//判空
// 入栈 出栈
// 读栈顶元素
//
#define MaxSize 50
typedef int ElemType;
typedef struct {
	ElemType data[MaxSize];
	int top;
}SqStack;

//创、初始化
void InitStack(SqStack *S){
	(*S).top = -1;
}

//销毁(数组不需要free)
void DestroyStack(SqStack* S) {
	(*S).top = -1;
}

//判空
bool StackEmpty(SqStack S){
	if (S.top == -1)
		return true;
	return false;
}

//入栈
bool Push(SqStack* S, ElemType x) {

	if ((*S).top == MaxSize-1)
		return false;
	(*S).top = (*S).top + 1;
	(*S).data[(*S).top] = x;
	return true;
}

//出栈
bool Pop(SqStack* S, ElemType* x) {
	if (StackEmpty(*S))
		return false;
	(*x) = (*S).data[(*S).top];
	(*S).top = (*S).top - 1;
	return true;
}

//读栈顶
bool GetTop(SqStack S, ElemType* x) {
	if (StackEmpty(S))
		return false;
	(*x) = S.data[S.top];
	return true;
}

void main() {
	SqStack S;
	InitStack(&S);
	int x = 10;
	int y = 0;

	Push(&S, x);
	GetTop(S, &y);
	printf("栈顶:%d 。",y );
	printf("栈是否为空:%d\n", StackEmpty(S));

	Pop(&S, &y);
	printf("出栈:%d 。", y);
	printf("栈是否为空:%d\n", StackEmpty(S));

}
链栈(无头结点 单链表存储)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//无头结点 单链表版本
//创建 销毁
// 判空
// 入栈 出栈
// 读栈顶
//

typedef int ElemType;
typedef struct Linknode{
	ElemType data;
	struct Linknode* next;
}*LiStack, Linknode;

//初始化
bool InitLiStack(LiStack* S) {
	(*S)= NULL;
	return true;
}

//判空
bool EmptyStack(LiStack S) {
	if (S == NULL)
		return true;
	return false;
}



//入栈
bool Push(LiStack* S,ElemType x) {
	Linknode* n;
	n =(Linknode*)malloc(sizeof(Linknode));
	(*n).next = (*S);
	(*n).data = x;
	(*S) = n;
	return true;
}

//出栈
bool Pop(LiStack* S,ElemType *x) {
	Linknode* d;
	d = (*S);
	(*x) = (*(*S)).data;
	(*S) = (*(*S)).next;
	free(d);
	return true;
}

//销毁
bool DestroyStack(LiStack* S) {
	ElemType x=0;
	while (!EmptyStack((*S))) {
		Push(S, x);
	}
	return true;
}

//读栈顶
bool GetTop(LiStack S, ElemType* x) {
	if (!EmptyStack(S)) {
		(*x) = (*S).data;
		return true;
	}
	return false;

}

void main() {
	LiStack S;
	InitLiStack(&S);

	int x = 10;
	int y = 0;

	scanf("%d", &x);

	while (x != 99) {
		Push(&S, x);
		GetTop(S, &y);
		printf("栈顶:%d 。\n", y);
		scanf("%d", &x);
	}
	printf("栈是否为空:%d\n", EmptyStack(S));
	printf("over\n\n");

	while (!EmptyStack(S)) {
		Pop(&S, &y);
		printf("出栈:%d 。\n", y);
	}
	printf("栈是否为空:%d\n", EmptyStack(S));

}
队列
顺序存储队列
  1. 队列 顺序表存储 固定容量

  2. 初始化 -front(最开始的;rear(下一个插入位

  3. 判空 -tag(判断最近操作类型

  4. 入队 出队

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxSize 50
//队列 顺序表存储 固定容量
// 初始化 -front(最开始的;rear(下一个插入位
// 判空	-tag(判断最近操作类型
// 入队 出队
//

typedef int ElemType;
typedef struct {
	ElemType data[MaxSize];
	int front, rear;
	int tag;	//判空3:入队-队满;出队-队空
}SqQueue;

//初始化
void InitQueue(SqQueue* Q) {
	(*Q).rear = (*Q).front = 0;
	(*Q).tag = 1;	//1-出队-队空
}

//判空
bool isEmpty(SqQueue Q) {
	if( Q.rear==Q.front && Q.tag == 1)
		return true;
	return false;
}

//入队
bool EnQueue(SqQueue* Q, ElemType x) {
	if ((*Q).front == (*Q).rear && (*Q).tag == 0)
		return false;
	(*Q).data[(*Q).rear] = x;
	(*Q).rear = ((*Q).rear + 1)%MaxSize;	//取余实现自主循环
	(*Q).tag = 0;
	return true;
}

//出队
bool DeQueue(SqQueue* Q,ElemType *x) {
	if (isEmpty(*Q))
		return false;
	(*x) = (*Q).data[(*Q).front];
	(*Q).front = ((*Q).front + 1) % MaxSize;
	(*Q).tag = 1;
	return true;
}

void main() {
	SqQueue Q;
	InitQueue(&Q);

	int x = 0, y = 0;
	scanf("%d", &x);
	while (x != 99) {
		EnQueue(&Q, x);
		printf("最新入队:%d 。\n", x);
		scanf("%d", &x);
	}
	printf("队列是否为空:%d\n", isEmpty(Q));
	printf("over\n\n");

	while (!isEmpty(Q)) {
		DeQueue(&Q, &y);
		printf("最新出队:%d 。\n", y);
	}
	printf("队列是否为空:%d\n", isEmpty(Q));
	printf("over\n\n");
}
链式存储队列
单链表(有头结点)
  1. 链式存储-有头结点
  2. 初始化
  3. 判空
  4. 入队 出队
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//队列 链式存储-有头结点
// 初始化
// 判空
// 入队 出队
//
typedef int ElemType;
typedef struct {
	ElemType data;
	struct LinkNode* next;
}LinkNode;

typedef struct {
	LinkNode* front, * rear;
}LinkQueue;

//初始化-有头结点
void InitQueue(LinkQueue *Q) {
	(*Q).front = (*Q).rear = (LinkNode*)malloc(sizeof(LinkNode));
	(*(*Q).front).next = NULL;
}

bool isEmpty(LinkQueue Q) {
	if (Q.front == Q.rear)
		return true;
	return false;
}

bool EnQueue(LinkQueue* Q,ElemType x) {
	LinkNode* n;
	n = (LinkNode*)malloc(sizeof(LinkNode));
	if (n == NULL)
		return false;
	(*n).data = x;
	(*n).next = NULL;
	(*(*Q).rear).next = n;
	(*Q).rear = n;
	return true;
}

//出队
bool DeQueue(LinkQueue* Q, ElemType* x) {
	if (isEmpty(*Q))
		return false;
	LinkNode* d;
	d= (*(*Q).front).next;
	(*x) = (*d).data;
	(*(*Q).front).next = (*d).next;
	if ((*Q).rear == d)
		(*Q).rear = (*Q).front;
	free(d);
	return true;
}

void main() {
	LinkQueue Q;
	InitQueue(&Q);

	ElemType x, y;
	scanf("%d", &x);
	while (x != 99) {
		EnQueue(&Q, x);
		printf("入队:%d 。\n", x);
		scanf("%d", &x);
	}
	printf("此时队列是否为空:%d\nover\n\n", isEmpty(Q));

	while (!isEmpty(Q)) {
		DeQueue(&Q, &y);
		printf("出队:%d 。\n", y);
	}
	printf("此时队列是否为空:%d\n", isEmpty(Q));
}
单链表(无头结点)
//队列 链式存储-无头结点
// 初始化-front指向第一个,rear指向最后一个
// 判空
// 入队 出队
//
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;
typedef struct {
	ElemType data;
	struct LinkNode* next;
}LinkNode;

typedef struct {
	LinkNode* front, * rear;
}LinkQueue;

//初始化
void InitQueue(LinkQueue* Q) {

	(*Q).front = (*Q).rear = NULL;
}

//判空
bool isEmpty(LinkQueue Q) {
	if (Q.front == NULL)
		return true;
	return false;
}

//入队-主rear往后(注意rear为空情况-同front
bool EnQueue(LinkQueue* Q, ElemType x) {
	LinkNode* n = (LinkNode*)malloc(sizeof(LinkNode));
	if (n == NULL)
		return false;
	
	(*n).data = x;
	(*n).next = NULL;
	
	
	if ((*Q).front == NULL) {
		(*Q).front = n;
		
	}
	else {
		(*(*Q).rear).next = n;
	}
	(*Q).rear = n;
		
	return true;
}

//出队-主front往后
bool DeQueue(LinkQueue* Q, ElemType* x) {
	if (isEmpty(*Q))
		return false;
	LinkNode* d = (*Q).front;
	(*Q).front = (*d).next;
	(*x) = (*d).data;
	if ((*Q).rear == d)
		(*Q).rear = (*Q).front;
	free(d);
	return true;

}

void main() {
	LinkQueue Q;
	InitQueue(&Q);

	ElemType x, y;
	scanf("%d", &x);
	while (x != 99) {
		EnQueue(&Q, x);
		printf("入队:%d 。\n", x);
		scanf("%d", &x);
	}
	printf("此时队列是否为空:%d\nover\n\n", isEmpty(Q));

	while (!isEmpty(Q)) {
		DeQueue(&Q, &y);
		printf("出队:%d 。\n", y);
	}
	printf("此时队列是否为空:%d\n", isEmpty(Q));
}
应用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值