diff --git a/CMakeLists.txt b/CMakeLists.txt index f2fe95f..e3f9162 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(untitled1 C) set(CMAKE_C_STANDARD 11) -add_executable(untitled1 main.c ) +add_executable(untitled1 main.c) diff --git a/Stack/SeqStack.c b/Stack/SeqStack.c new file mode 100644 index 0000000..b543079 --- /dev/null +++ b/Stack/SeqStack.c @@ -0,0 +1,67 @@ +#include + +#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"); + +} \ No newline at end of file diff --git a/main.c b/main.c index 139e83e..f7e84fd 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,25 @@ -#include "SeqList/SeqList.c" -#include "SeqList//LinkList.c" +#include "LinearList/SeqList.c" +#include "LinearList/LinkList.c" +#include "Stack/SeqStack.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 + // SeqList s; // initList(&s); // insertList(&s, 1, 100); @@ -13,20 +31,19 @@ int main() { // printf("%d",getElem(&s,1)); // printf("%d",locateElem(&s,400)); - ListNode *l; - l = initLink(); - display(l); - - ListNode *k; - k = init(); - display(k); - ListNode *m; - m = createList(); - display(m); - ListNode *o; -// o = GetNodei(m, 3); - o = insertLinkList(m,3,100); - display(o); +// ListNode *l; +// l = initLink(); +// display(l); +// +// ListNode *k; +// k = init(); +// display(k); +// ListNode *m; +// m = createList(); +// display(m); +// ListNode *o; +// o = insertLinkList(m,3,100); +// display(o); }