#include <ctype.h>
#include <malloc.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int32_t Elemtype;
typedef struct
{
Elemtype *Stack_List, *Stack_listEnd;
Elemtype* top;
int maxlengh;
} linear_Stack_t, *linear_Stack_p;
linear_Stack_p CreatStack(int maxlengh)
{
if (maxlengh < 0)
return NULL;
linear_Stack_t* newStack = malloc(sizeof(linear_Stack_t));
if (!newStack)
return NULL;
newStack->Stack_List = maxlengh == 0 ? NULL : malloc(sizeof(Elemtype) * maxlengh);
if (maxlengh > 0 && !newStack->Stack_List) {
free(newStack);
return NULL;
}
memset(newStack->Stack_List, 0, sizeof(Elemtype) * maxlengh);
newStack->Stack_listEnd = newStack->Stack_List ? newStack->Stack_List + maxlengh - 1 : NULL;
newStack->top = NULL;
newStack->maxlengh = maxlengh;
return newStack;
}
typedef uint8_t S_Status;
#define OK 0
#define SNULL 1
S_Status isNULLStack(linear_Stack_p s)
{
return s == NULL ? SNULL : 0;
}
#define SEMPTY 2
S_Status isEmptyStack(linear_Stack_p s)
{
return s->top == NULL ? SEMPTY : 0;
}
#define SFULL 3
S_Status isFullStack(linear_Stack_p s)
{
return s->top == s->Stack_listEnd ? SFULL : 0;
}
S_Status (*StackStatusFuc[3])(linear_Stack_p) = { &isNULLStack, &isEmptyStack, &isFullStack };
void PrintStackStatue(linear_Stack_p s)
{
S_Status status = 0;
for (int i = 0; i < 3; i++) {
status = (*StackStatusFuc[i])(s);
printf("%d ", status);
if (status == SNULL)
break;
}
printf("\n");
}
S_Status Push(linear_Stack_p s, const Elemtype date, void CopyFuc(Elemtype*, const Elemtype*))
{
if (isNULLStack(s))
return SNULL;
if (isFullStack(s))
return SFULL;
s->top = s->top ? s->top + 1 : s->Stack_List;
CopyFuc(s->top, &date);
return 0;
}
S_Status Push_num(linear_Stack_p s, int num, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
S_Status status = 0;
while (!(status = Push(s, *date, CopyFuc)) && num--) {
++date;
}
return status;
}
S_Status Pop(linear_Stack_p s, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
if (isNULLStack(s))
return SNULL;
if (isEmptyStack(s))
return SEMPTY;
CopyFuc(date, s->top);
s->top = s->top == s->Stack_List ? NULL : s->top - 1;
return 0;
}
#define NO_LARGENUM_TO_POP SEMPTY
S_Status Pop_num(linear_Stack_p s, int num, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
S_Status status = 0;
while (!(status = Pop(s, date, CopyFuc)) && num--) {
++date;
}
return status;
}
S_Status Pop_All(linear_Stack_p s, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
S_Status status = 0;
while (!(status = Pop(s, date, CopyFuc))) {
++date;
}
return status == SEMPTY ? 0 : status;
}
void ElemtypeCopyFuc(Elemtype* dst, const Elemtype* src)
{
*dst = *src;
}
int main(void)
{
Elemtype pushdate[10] = {1,2,3,4,5,6,7,8,9,0};
Elemtype popdate[10];
linear_Stack_p s1 = CreatStack(5);
linear_Stack_p s2 = CreatStack(3);
Push_num(s1,5,pushdate,ElemtypeCopyFuc);
Push_num(s2,3,pushdate+5,ElemtypeCopyFuc);
printf("\n");
Pop_All(s1,popdate,ElemtypeCopyFuc);
Pop_All(s2,popdate + 5,ElemtypeCopyFuc);
printf("\n");
}
顺序栈的实现
于 2024-08-15 17:12:57 首次发布