赞
踩
目录
- #pragma once
-
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<stdbool.h>
- #include<assert.h>
-
- #define STDateType int
-
- typedef struct Stack
- {
- STDateType* date;
- int top;
- int capacity;
- }ST;
-
- void STInit(ST* st);
- void STDestory(ST* st);
- void STPush(ST* st, STDateType x);
- void STPop(ST* st);
- STDateType STTop(ST* st);
- bool STEmpty(ST* st);
- int STSize(ST* st);
- void STInit(ST* st)
- {
- assert(st);
- st->date = NULL;
- st->capacity = st->top = 0;
- }
- void STDestory(ST* st)
- {
- assert(st);
- free(st->date);
- st->date = NULL;
- st->top = 0;
- st->capacity = 0;
- }
- void STPush(ST* st, STDateType x)
- {
- assert(st);
-
- if (st->top == st->capacity)
- {
- size_t newcapacity = (st->capacity == 0 ? 4 : 2 * st->capacity);
- STDateType* tmp = (STDateType*)realloc(st->date, sizeof(STDateType) * newcapacity);
- if (tmp != NULL)
- {
- st->date = tmp;
- st->capacity = newcapacity;
- }
- }
-
- st->date[st->top++] = x;
- }
- void STPop(ST* st)
- {
- assert(st);
- assert(st->top > 0);
- st->top--;
- }
- bool STEmpty(ST* st)
- {
- assert(st);
- return st->top == 0;
- }
- STDateType STTop(ST* st)
- {
- assert(st);
- assert(st->top > 0);
- return st->date[st->top - 1];
- }
- int STSize(ST* st)
- {
- assert(st);
- return st->top;
- }
- #pragma once
-
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<stdbool.h>
- #include<assert.h>
-
- typedef int StDateType;
-
- typedef struct StackNode
- {
- StDateType date;
- struct StackNode* next;
- }StackNode;
-
- typedef struct Stack
- {
- StackNode* top;
- size_t size;
- }Stack;
-
- void StackInit(Stack* st);//初始化
- void StackDestory(Stack* st);//销毁
- void StackPush(Stack* st, StDateType x);//压栈
- void StackPop(Stack* st);//弹栈
- StDateType StackTop(Stack* st);//读取栈顶元素
- bool StackEmpty(Stack* st);//判空
- size_t StackSize(Stack* st);//返回栈的长度
- void StackInit(Stack* st)
- {
- assert(st);
-
- st->top = NULL;
- st->size = 0;
- }
- void StackDestory(Stack* st)
- {
- assert(st);
-
- while (st->size > 0)
- StackPop(st);
- }
- void StackPush(Stack* st, StDateType x)
- {
- assert(st);
-
- StackNode* Node = (StackNode*)malloc(sizeof(StackNode));
- if (!Node)
- {
- perror("malloc mistake");
- exit(-1);
- }
- Node->date = x;
- Node->next = NULL;
-
- Node->next = st->top;
- st->top = Node;
- st->size++;
- }
- void StackPop(Stack* st)
- {
- assert(st);
- assert(st->top);
-
- StackNode* tmp = st->top;
- st->top = st->top->next;
- free(tmp);
- st->size--;
- }
- bool StackEmpty(Stack* st)
- {
- assert(st);
- return st->top == NULL;
- }
- StDateType StackTop(Stack* st)
- {
- assert(st);
- return st->top->date;
- }
- size_t StackSize(Stack* st)
- {
- assert(st);
- assert(st->top);
- return st->size;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。