diff --git a/Stack/LinkStack.c b/Stack/LinkStack.c new file mode 100644 index 0000000..52c3a0d --- /dev/null +++ b/Stack/LinkStack.c @@ -0,0 +1,42 @@ +#include +#include + +typedef struct LinkStack +{ + char data; + struct LinkStack *next; +} LinkStack; + +LinkStack *push(LinkStack *top, char a) +{ + LinkStack *line = (LinkStack *)malloc(sizeof(LinkStack)); + line->data = a; + line->next = top; + top = line; + return top; +} + +LinkStack *pop(LinkStack *top) +{ + if (top) + { + LinkStack *p = top; + top = top->next; + printf("current = %c \n", p->data); + if (top) + { + printf("top = %c\n", top->data); + } + else + { + printf("empty\n"); + } + free(p); + } + else + { + printf("Stack Empty\n"); + return top; + } + return top; +} diff --git a/Stack/SeqStack.c b/Stack/SeqStack.c index b543079..f493baa 100644 --- a/Stack/SeqStack.c +++ b/Stack/SeqStack.c @@ -1,4 +1,5 @@ #include +#include #define StackSize 100 typedef char DataType; diff --git a/main.c b/main.c index f7e84fd..cf02599 100644 --- a/main.c +++ b/main.c @@ -1,24 +1,37 @@ +#include #include "LinearList/SeqList.c" #include "LinearList/LinkList.c" -#include "Stack/SeqStack.c" +//#include "Stack/SeqStack.c" +#include "Stack/LinkStack.c" int main() { - - SeqStack seqStack; - initStack(&seqStack); - push(&seqStack, 'a'); - push(&seqStack, 'b'); - - //取栈顶元素 - printf("Top = %c\n", getTop(&seqStack)); - - printStack(&seqStack); //Stack contents: ba - pop(&seqStack); - printStack(&seqStack); //Stack contents: b - pop(&seqStack); - printStack(&seqStack); //Stack contents: - pop(&seqStack); //stack underflow + LinkStack *stack = NULL; + stack = push(stack, 'a'); + stack = push(stack, 'b'); + stack = push(stack, 'c'); + stack = push(stack, 'd'); + stack = pop(stack); + stack = pop(stack); + stack = pop(stack); + stack = pop(stack); + stack = pop(stack); + return 0; + +// SeqStack seqStack; +// initStack(&seqStack); +// push(&seqStack, 'a'); +// push(&seqStack, 'b'); +// +// //取栈顶元素 +// printf("Top = %c\n", getTop(&seqStack)); +// +// printStack(&seqStack); //Stack contents: ba +// pop(&seqStack); +// printStack(&seqStack); //Stack contents: b +// pop(&seqStack); +// printStack(&seqStack); //Stack contents: +// pop(&seqStack); //stack underflow // SeqList s; // initList(&s);