SeqStack.cpp
#include "SeqStack.h"
#include<stdio.h>
#include<stdlib.h>
typedef struct PERSON{
char name[64];
int age;
}Person;
int main(void)
{
//创建栈
SeqStack *stack = Init_SeqStack();
//创建数据
Person p1, p2, p3, p4, p5;
p1 = { "aaaa", 10 };
p2 = { "bbbb", 10 };
p3 = { "cccc", 10 };
p4 = { "dddd", 10 };
p5 = { "eeee", 10 };
//入栈
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 *p = (Person*)Top_SeqStack(stack);
printf("name:%s age:%d\n",p->name,p->age);
//弹出栈顶元素
Pop_SeqStack(stack);
}
FreeSpace_SeqStack(stack);
system("pause");
return 0;
}
SeqStack.h
#ifndef SEQSTACK_H
#define SEQSTACK_H
//数组模拟栈的顺序存储
#define MAX_SIZE 1024
#define SEQSTACK_TURE 1
#define SEQSTACK_FALSE 2
typedef struct SEQSTACK{
void*data[MAX_SIZE];
int size;
}SeqStack;
//初始化栈
SeqStack *Init_SeqStack();
//入栈
void Push_SeqStack(SeqStack *stack,void *data);
//返回栈顶元素
void*Top_SeqStack(SeqStack*stack);
//出栈
void Pop_SeqStack(SeqStack*stack);
//判点是否为空
int IsEmpty(SeqStack *stack);
int Size_SeqStack(SeqStack*stack);
//清空栈
void Clear_SeqStack(SeqStack*stack);
//销毁栈
void FreeSpace_SeqStack(SeqStack*stack);
#endif
具体程序见: