要我说,和单链表的头插入法差不多呵呵
#include<cstdio> #include<cstring> #include<cstdlib> typedef struct StackNode{ int data; struct StackNode *next; }StackNode,*LinkStackPtr; typedef struct LinkStack{ LinkStackPtr top; int count; }LinkStack; int Push(LinkStack *S,int e){ LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode)); s->data = e; s->next = S->top; //类似头插入 S->top = s; //更新栈top S->count++; return 1; } int StackEmpty(LinkStack *S){ if(S->count == 0) return 1; return 0; } int Pop(LinkStack *S,int *e){ LinkStackPtr p; if(!(StackEmpty(*S))) return 0; *e = S->top->data; p = S->top; S->top = S->top->next; free(p); S->count--; return 1; } int main(){ return 0; }
时间: 2024-11-10 08:54:02