C语言-栈-顺序结构(数组实现)

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

//数组去模拟栈的顺序存储
#define MAX_SIZE 1024
#define SEQSTACK_TRUE 1
#define SEQSTACK_FALSE 0

typedef struct SEQSTACK {
	void* data[MAX_SIZE];
	int size;
}SeqStack;

//初始化栈
SeqStack* Init_SeqStack() {
	SeqStack *stack = (SeqStack*)malloc(sizeof(SeqStack));
	for (int i = 0; i < MAX_SIZE; i++)
	{
		stack->data[i] = NULL;
	}
	stack->size = 0;
	return stack;
}
// 入栈
void Push_SeqStack(SeqStack* stack, void* data) {
	if (stack == NULL) {
		return;
	}
	if (stack->size == MAX_SIZE) {
		return;
	}
	if (data == NULL) {
		return;
	}
	stack->data[stack->size] = data;
	stack->size++;
}
//出栈
void* Pop_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return NULL;
	}
	if (stack->size == 0) {
		return NULL;
	}
	void *top = stack->data[stack->size - 1];
	stack->data[stack->size - 1] = NULL;
	stack->size--;
	return top;
}
//判断是否为空
int IsEmpty(SeqStack* stack) {
	if (stack == NULL) {
		return -1;
	}
	if (stack->size == 0) {
		return SEQSTACK_TRUE;
	}
	return SEQSTACK_FALSE;
}
//返回栈中元素个数
int Size_SeqStack(SeqStack* stack) {
	return stack->size;
}
//清空栈
void Clear_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return;
	}
	for (int i = 0; i < stack->size; i++) {
		stack->data[i] = NULL;
	}
	stack->size = 0;
}
//销毁
void FreeSpace_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return;
	}
	free(stack);
}

typedef struct PERSON {
	char name[64];
	int age;
}Person;

int main(void) {

	//创建栈
	SeqStack *stack = Init_SeqStack();
	//创建数据
	Person p1 = { "aaa",10 };
	Person p2 = { "bbb",20 };
	Person p3 = { "ccc",30 };
	Person p4 = { "ddd",40 };
	Person p5 = { "eee",50 };
	//入栈
	Push_SeqStack(stack, &p1);
	Push_SeqStack(stack, &p2);
	Push_SeqStack(stack, &p3);
	Push_SeqStack(stack, &p4);
	Push_SeqStack(stack, &p5);

	//输出
	while(Size_SeqStack(stack) > 0) {
		//访问栈顶
		Person* person = (Person*)Pop_SeqStack(stack);
		printf("Name:%s Age:%d\n", person->name, person->age);
	}

	//释放内存
	FreeSpace_SeqStack(stack);

	system("pause");
	return 0;
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页