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 <stdlib.h>
#define StackSize 100
typedef char DataType;

@ -1,24 +1,37 @@
#include <stdio.h>
#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);

Loading…
Cancel
Save