diff options
Diffstat (limited to 'c/dataStructure/栈和队列/栈/链栈')
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/ack.c | 20 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/conversion.c | 29 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/main.c | 21 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/p_match.c | 41 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/palindrome.c | 64 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/stack.c | 56 | ||||
-rwxr-xr-x | c/dataStructure/栈和队列/栈/链栈/stack.h | 25 |
7 files changed, 256 insertions, 0 deletions
diff --git a/c/dataStructure/栈和队列/栈/链栈/ack.c b/c/dataStructure/栈和队列/栈/链栈/ack.c new file mode 100755 index 0000000..9d12544 --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/ack.c @@ -0,0 +1,20 @@ +#include "stack.h" + +int ack (int m, int n) +{ + if (m == 0) + return n+1; + else if (n == 0) + return ack(m-1,1); + else + return ack(m-1,ack(m,n-1)); +} + +int main(void) +{ + int m = 2, n = 1; + + printf("%d\n",ack(m,n)); + + return 0; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/conversion.c b/c/dataStructure/栈和队列/栈/链栈/conversion.c new file mode 100755 index 0000000..ebc2161 --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/conversion.c @@ -0,0 +1,29 @@ +#include "stack.h" + +int main(void) +{ + Stack s; + + int number, ans; + + InitStack(&s); + + printf("Please enter a number: "); + scanf("%d", &number); + + while (number) + { + Push(&s, number%2); + number /= 2; + } + + while (!StackIsEmpty(&s)) + { + printf("%d",GetTop(&s)); + Pop(&s, &ans); + } + + putchar('\n'); + + return 0; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/main.c b/c/dataStructure/栈和队列/栈/链栈/main.c new file mode 100755 index 0000000..f211ae4 --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/main.c @@ -0,0 +1,21 @@ +#include "stack.h" + +int main(void) { + Stack s; + int p; + + InitStack(&s); + + for (int i = 0; i < 3; i++) + Push(&s, i); + + for (int i = 0; i < 3; i++) { + printf("Get top: %d\n",GetTop(&s)); + Pop(&s,&p); + printf("Pop: %d\n",p); + } + + DestroyStack(&s); + + return 0; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/p_match.c b/c/dataStructure/栈和队列/栈/链栈/p_match.c new file mode 100755 index 0000000..44efecd --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/p_match.c @@ -0,0 +1,41 @@ +#include "stack.h" + +int main(void) +{ + ElemType ch; + Stack s; + ElemType pop, top; + bool flag = true; + + InitStack(&s); + + while ((ch = getchar()) != '\n' && flag) + { + switch (ch) + { + case '(': Push(&s,ch); + break; + case '[': Push(&s,ch); + break; + case ')': if (StackIsEmpty(&s) || (top = GetTop(&s)) != '(') + flag = false; + else if (top == '(') + Pop(&s,&pop); + break; + case ']': if (StackIsEmpty(&s) || (top = GetTop(&s)) != '[') + flag = false; + else if (top == '[') + Pop(&s,&pop); + break; + } + } + + if (StackIsEmpty(&s) && flag) + printf("Matching successful!\n\n"); + else + { + fprintf(stderr,"Matching failed!\n\n"); + DestroyStack(&s); + } + return 0; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/palindrome.c b/c/dataStructure/栈和队列/栈/链栈/palindrome.c new file mode 100755 index 0000000..c8a9a7f --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/palindrome.c @@ -0,0 +1,64 @@ +#include<ctype.h> +#include "stack.h" + +int main(void) +{ + ElemType ch,a1,a2; + Stack s,p; + int size = 0; + bool flag = false; + + InitStack(&s); + InitStack(&p); + + while ((ch = getchar()) != '\n') + { + Push(&s,tolower(ch)); + size++; + } + + if (size == 0) + return 0; + if (size % 2 == 0) + flag = true; + size /= 2; + + if (!flag) + size++; + + for (int i = 0; i < size; i++) + { + if (flag) + { + Pop(&s,&ch); + Push(&p,ch); + } + else + { + if (i == size - 1) + ch = GetTop(&s); + else + Pop(&s,&ch); + Push(&p,ch); + } + } + + while (s) + { + Pop(&s,&a1); + Pop(&p,&a2); + if (a1 != a2) + break; + } + + if (s) + { + printf("Not palindrome word!\n"); + DestroyStack(&s); + DestroyStack(&p); + } + else + printf("Is palindrome word!\n"); + + return 0; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/stack.c b/c/dataStructure/栈和队列/栈/链栈/stack.c new file mode 100755 index 0000000..e47dd81 --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/stack.c @@ -0,0 +1,56 @@ +#include "stack.h" + +bool InitStack(Stack * s) { + *s = NULL; + return true; +} + +bool DestroyStack(Stack * s) { + Stack p; + + while (*s) { + p = *s; + *s = (*s)->next; + free (p); + } + return true; +} + +bool Push(Stack * s, ElemType n) { + Stack p = (stack *) malloc (sizeof(stack)); + if (!p) { + fprintf(stderr,"No enough memory\n"); + return false; + } + p->data = n; + p->next = *s; + + *s = p; + + return true; +} + +bool Pop(Stack * s, ElemType * n) { + if (!*s) { + fprintf(stderr,"No element in stack!\n"); + return false; + } + *n = (*s)->data; + Stack p = *s; + *s = (*s)->next; + free(p); + + return true; +} + +ElemType GetTop(Stack * s) { + if (*s) + return (*s)->data; + return -1; +} + +bool StackIsEmpty (Stack * s) { + if (!*s) + return true; + return false; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/栈/链栈/stack.h b/c/dataStructure/栈和队列/栈/链栈/stack.h new file mode 100755 index 0000000..33e9ffe --- /dev/null +++ b/c/dataStructure/栈和队列/栈/链栈/stack.h @@ -0,0 +1,25 @@ +#ifndef _STACK_H +#define _STACK_H + +#include<stdio.h> +#include<stdlib.h> +#include<stdbool.h> +#include"tree.h" + +typedef Node * ElemType; + +typedef struct stack{ + ElemType data; + struct stack * next; +} stack; + +typedef stack * Stack; + +bool InitStack(Stack * s); +bool DestroyStack(Stack * s); +bool Push(Stack * s, ElemType n); +bool Pop(Stack * s, ElemType * n); +ElemType GetTop(Stack * s); +bool StackIsEmpty (Stack * s); + +#endif |