add SeqStack.c

main
lwy 2 years ago
parent 375e0264af
commit 9a48d5cadc

@ -0,0 +1,67 @@
#include <stdio.h>
#define StackSize 100
typedef char DataType;
typedef struct {
DataType data[StackSize];
int top;
} SeqStack;
//置空栈
void initStack(SeqStack *S) {
S->top = -1;
}
//判栈空
int stackEmpty(SeqStack *S) {
return S->top == -1;
}
//判栈满
int stackFull(SeqStack *S) {
return S->top == StackSize - 1;
}
//进栈(入栈)
void push(SeqStack *S, DataType x) {
if (stackFull(S)) {
} else {
S->top = S->top + 1;
S->data[S->top] = x;
}
}
//退栈(出栈)
DataType pop(SeqStack *S) {
if (stackEmpty(S)) {
printf("stack underflow");
exit(0);
} else {
return S->data[S->top--];
}
}
//取栈顶元素
DataType getTop(SeqStack *S) {
if (stackEmpty(S)) {
printf("stack empty");
exit(0);
} else {
return S->data[S->top];
}
}
// 遍历并输出栈中的所有元素
void printStack(SeqStack *S) {
int i;
printf("top = %d\n",S->top);
printf("Stack contents: ");
for (i = S->top; i >= 0; i--) {
printf("%c", S->data[i]);
}
printf("\n");
}

@ -1,7 +1,25 @@
#include "SeqList/SeqList.c" #include "LinearList/SeqList.c"
#include "SeqList//LinkList.c" #include "LinearList/LinkList.c"
#include "Stack/SeqStack.c"
int main() { 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
// SeqList s; // SeqList s;
// initList(&s); // initList(&s);
// insertList(&s, 1, 100); // insertList(&s, 1, 100);
@ -13,20 +31,19 @@ int main() {
// printf("%d",getElem(&s,1)); // printf("%d",getElem(&s,1));
// printf("%d",locateElem(&s,400)); // printf("%d",locateElem(&s,400));
ListNode *l; // ListNode *l;
l = initLink(); // l = initLink();
display(l); // display(l);
//
ListNode *k; // ListNode *k;
k = init(); // k = init();
display(k); // display(k);
ListNode *m; // ListNode *m;
m = createList(); // m = createList();
display(m); // display(m);
ListNode *o; // ListNode *o;
// o = GetNodei(m, 3); // o = insertLinkList(m,3,100);
o = insertLinkList(m,3,100); // display(o);
display(o);
} }

Loading…
Cancel
Save