#include <stdlib.h>
#include <stdio.h>
#include"LinkStack.h"
const int TRUE = 1;
const int FALSE = 0;
const int null = 0;
void initStack(StackLink *s) {
s->top = (StackNode *) malloc(sizeof(StackNode));
s->size = 0;
s->top->data = 0;
s->top->next = null;
}
status pop(StackLink *s, ElemType *elem) {
StackNode *p;
if (s->top->next == null) {
printf("the stack is empty\n");
return FALSE;
} else {
*elem = s->top->next->data;
p = s->top;
s->top = s->top->next;
s->size--;
free(p);
return TRUE;
}
}
void push(StackLink *s, ElemType x) {
StackNode *p;
p = (StackNode *) malloc(sizeof(StackNode));
p->data = x;
p->next = s->top->next;
s->top->next = p;
s->size++;
}
void getTop(StackLink *s, ElemType *elem) {
if (s == null)
printf("the stack is empty!\n");
*elem = s->top->next->data;
}
status isEmpty(StackLink *s) {
if (s->size == 0) {
return TRUE;
} else {
return FALSE;
}
}
void traverse(StackLink *s) {
StackNode *p;
p = s->top->next;
printf("traverse the StackNode:\n");
if (isEmpty(s))
printf("the StackNode is empty!\n");
else {
while (p) {
printf("->%d", p->data);
p = p->next;
}
}
}
void clear(StackLink *s) {
StackNode *p, *q;
p = s->top;
while (p) {
q = p; /* "q" is the prev node of node "p"*/
p = p->next;
free(q);
}
s->size = 0;
}
int getStackLength(StackLink *s) {
return s->size;
}
C链栈实现
最新推荐文章于 2023-05-19 20:23:19 发布