add LinkStack.c

main
lwy 2 years ago
parent 9a48d5cadc
commit c3368fc6b2

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#define StackSize 100 #define StackSize 100
typedef char DataType; typedef char DataType;

@ -1,24 +1,37 @@
#include <stdio.h>
#include "LinearList/SeqList.c" #include "LinearList/SeqList.c"
#include "LinearList/LinkList.c" #include "LinearList/LinkList.c"
#include "Stack/SeqStack.c" //#include "Stack/SeqStack.c"
#include "Stack/LinkStack.c"
int main() { int main() {
LinkStack *stack = NULL;
SeqStack seqStack; stack = push(stack, 'a');
initStack(&seqStack); stack = push(stack, 'b');
push(&seqStack, 'a'); stack = push(stack, 'c');
push(&seqStack, 'b'); stack = push(stack, 'd');
stack = pop(stack);
//取栈顶元素 stack = pop(stack);
printf("Top = %c\n", getTop(&seqStack)); stack = pop(stack);
stack = pop(stack);
printStack(&seqStack); //Stack contents: ba stack = pop(stack);
pop(&seqStack); return 0;
printStack(&seqStack); //Stack contents: b
pop(&seqStack); // SeqStack seqStack;
printStack(&seqStack); //Stack contents: // initStack(&seqStack);
pop(&seqStack); //stack underflow // 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; // SeqList s;
// initList(&s); // initList(&s);

Loading…
Cancel
Save