//线性顺序栈
#ifndef _MY_SEQSTACK_H_
#define _MY_SEQSTACK_H_
typedef void SeqStack;
//创建栈
SeqStack* SeqStack_Create(int capacity);
//销毁栈
int SeqStack_Destroy(SeqStack* stack);
//清空栈
int SeqStack_Clear(SeqStack* stack);
//压栈
int SeqStack_Push(SeqStack* stack, void* item);
//出栈
void* SeqStack_Pop(SeqStack* stack);
//获取栈顶元素
void* SeqStack_Top(SeqStack* stack);
//获取栈长度
int SeqStack_Size(SeqStack* stack);
//获取栈的原始容量
int SeqStack_Capacity(SeqStack* stack);
#endif //_MY_SEQSTACK_H_
//线性顺序栈代码实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"SeqList.h"//引用线性顺序表dll
#include"seqstack.h"
/*
顺序线性表模拟栈
入栈应该从尾部插入元素,从开头插入元素,需要向后移动所有元素
*/
//创建栈
SeqStack* SeqStack_Create(int capacity){
return (SeqStack*)SeqList_Create(capacity);
}
//销毁栈
int SeqStack_Destroy(SeqStack* stack){
int ERRO_MSG = 0;
if (stack==NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return ERRO_MSG;
}
ERRO_MSG=SeqList_Destroy(&stack);
return ERRO_MSG;
}
//清空栈
int SeqStack_Clear(SeqStack* stack){
int ERRO_MSG = 0;
if (stack == NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return ERRO_MSG;
}
ERRO_MSG=SeqList_Clear(stack);
return ERRO_MSG;
}
//压栈
int SeqStack_Push(SeqStack* stack, void* item){
int ERRO_MSG = 0;
if (stack == NULL || item==NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return ERRO_MSG;
}
ERRO_MSG = SeqList_Insert(stack, item, SeqList_Length(stack));
return ERRO_MSG;
}
//出栈
void* SeqStack_Pop(SeqStack* stack){
int ERRO_MSG = 0;
if (stack == NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return NULL;
}
return SeqList_Delete(stack, SeqList_Length(stack)-1);
}
//获取栈顶元素
void* SeqStack_Top(SeqStack* stack){
int ERRO_MSG = 0;
if (stack == NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return NULL;
}
return SeqList_Get(stack, SeqList_Length(stack)-1);
}
//获取栈长度
int SeqStack_Size(SeqStack* stack){
int ERRO_MSG = 0;
if (stack == NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return ERRO_MSG;
}
return SeqList_Length(stack);
}
//获取栈的原始容量
int SeqStack_Capacity(SeqStack* stack){
int ERRO_MSG = 0;
if (stack == NULL)
{
ERRO_MSG = -1;
printf("传入参数不可以为空!erro msg:%d \n", ERRO_MSG);
return ERRO_MSG;
}
return SeqList_Capacity(stack);
}
//线性顺序栈测试程序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"seqstack.h"
typedef struct _Student{
char name[30];
int age;
}Student;
void Test(){
Student s1, s2, s3, s4, s5;
int numx = 0, i = 0, ret = 0;
strcpy(s1.name, "小米");
s1.age = 11;
strcpy(s2.name, "小刚");
s2.age = 12;
strcpy(s3.name, "小红");
s3.age = 10;
strcpy(s4.name, "啸天");
s4.age = 13;
strcpy(s5.name, "莲华");
s5.age = 12;
SeqStack *stack = NULL;
//创建栈
stack = SeqStack_Create(10);
//压栈
ret = SeqStack_Push(stack, &s1);
if (ret!=0)
{
printf("入栈失败!\n");
return;
}
ret = SeqStack_Push(stack, &s2);
if (ret != 0)
{
printf("入栈失败!\n");
return;
}
ret = SeqStack_Push(stack, &s3);
if (ret != 0)
{
printf("入栈失败!\n");
return;
}
ret = SeqStack_Push(stack, &s4);
if (ret != 0)
{
printf("入栈失败!\n");
return;
}
ret = SeqStack_Push(stack, &s5);
if (ret != 0)
{
printf("入栈失败!\n");
return;
}
numx = SeqStack_Size(stack);
//出栈
for (i = 0; i < numx; i++)
{
Student *temp = (Student *)SeqStack_Pop(stack);
printf("我的名字是%s;我的年龄是%d\n", temp->name, temp->age);
}
//销毁链表
ret=SeqStack_Capacity(stack);
if (ret!=0)
{
printf("销毁链表失败!\n");
return;
}
}
void main(){
Test();
system("pause");
}