SqStack.h
#ifndef _SQSTACK_H
#define _SQSTACK_H
typedef int dataType;
typedef struct
{
dataType *data;
int capacity;
int stackTop;
} SqStack, *pSqStack;
pSqStack StackCreate(int capacity);
dataType StackPop(pSqStack stack);
int StackPush(pSqStack stack, dataType val);
int StackEmpty(pSqStack stack);
int StackFull(pSqStack stack);
dataType StackTop(pSqStack stack);
int StackClear(pSqStack stack);
int StackFree(pSqStack stack);
int StackShow(pSqStack stack);
#endif
SqStack.c
#include "SqStack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
pSqStack StackCreate(int capacity)
{
pSqStack s = (pSqStack)malloc(sizeof(SqStack));
if (s == NULL)
{
printf("StackCreate:pSqStack space allocation failed\n");
return NULL;
}
s->data = (dataType *)malloc(capacity * sizeof(dataType));
if (s->data == NULL)
{
printf("StackCreate:s->data space allocation failed\n");
free(s);
s = NULL;
return NULL;
}
memset(s->data, 0, sizeof(dataType) * capacity);
s->capacity = capacity;
s->stackTop = -1;
return s;
}
dataType StackPop(pSqStack stack)
{
if (stack == NULL)
{
printf("StackPop:invalid parameter.\n");
return -1;
}
stack->stackTop--;
return stack->data[stack->stackTop + 1];
}
int StackPush(pSqStack stack, dataType val)
{
if (stack == NULL)
{
printf("StackPush:invalid parameter.\n");
return -1;
}
if (stack->stackTop == stack->capacity - 1)
{
printf("The stack is full.\n");
return -1;
}
stack->stackTop++;
stack->data[stack->stackTop] = val;
return 0;
}
int StackEmpty(pSqStack stack)
{
if (stack == NULL)
{
printf("StackEmpty:invalid parameter.\n");
return -1;
}
return (stack->stackTop == -1 ? 1 : 0);
}
int StackFull(pSqStack stack)
{
if (stack == NULL)
{
printf("StackFull:invalid parameter.\n");
return -1;
}
return (stack->stackTop == stack->capacity - 1 ? 1 : 0);
}
dataType StackTop(pSqStack stack)
{
if (stack == NULL)
{
printf("StackTop:invalid parameter.\n");
return -1;
}
return stack->data[stack->stackTop];
}
int StackClear(pSqStack stack)
{
if (stack == NULL)
{
printf("StackClear:invalid parameter.\n");
return -1;
}
stack->stackTop = -1;
return 0;
}
int StackFree(pSqStack stack)
{
if (stack == NULL)
{
printf("StackFree:invalid parameter.\n");
return -1;
}
if (stack->data != NULL)
{
free(stack->data);
}
free(stack);
return 0;
}
int StackShow(pSqStack stack)
{
if (stack == NULL)
{
printf("StackShow:invalid parameter.\n");
return -1;
}
for (int i = stack->stackTop; i != -1; i--)
{
printf("%d ", stack->data[i]);
}
printf("\n");
return 0;
}
main.c
#include <stdio.h>
#include "SqStack.h"
void test_StackPush();
void test_StackPop();
void test_StackClear();
int main(int argc, char const *argv[])
{
// test_StackPush();
// test_StackPop();
test_StackClear();
return 0;
}
void test_StackPush()
{
pSqStack s = StackCreate(5);
StackPush(s, 1);
StackPush(s, 2);
StackPush(s, 3);
StackPush(s, 4);
StackPush(s, 5);
StackShow(s);
}
void test_StackPop()
{
pSqStack s = StackCreate(5);
StackPush(s, 1);
StackPush(s, 2);
StackPush(s, 3);
StackPush(s, 4);
StackPush(s, 5);
StackShow(s);
StackPop(s);
StackShow(s);
}
void test_StackClear()
{
pSqStack s = StackCreate(5);
StackPush(s, 1);
StackPush(s, 2);
StackPush(s, 3);
StackPush(s, 4);
StackPush(s, 5);
StackShow(s);
StackClear(s);
StackShow(s);
}
标签:03,顺序,return,int,StackPush,pSqStack,NULL,stack
From: https://www.cnblogs.com/goldenFantome/p/17615566.html