-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.c
98 lines (88 loc) · 2.07 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stackconfig.h"
#include "stack.h"
void testIntStack() {
Stack stack = {NULL};
print(&stack);
push(&stack, 3);
print(&stack);
push(&stack, 4);
print(&stack);
push(&stack, 5);
print(&stack);
pop(&stack);
print(&stack);
printf("%d\n", peek(&stack));
}
void testCharStack() {
Stack stack = {NULL};
print(&stack);
push(&stack, 3 + 'a');
print(&stack);
push(&stack, 4 + 'a');
print(&stack);
push(&stack, 5 + 'a');
print(&stack);
pop(&stack);
print(&stack);
printf("%c\n", peek(&stack));
}
void problemTwoCheckPalindrome() {
char *string = "madam";
int length = strlen(string);
Stack leftStack = {NULL};
int halfLength = length/2;
for(int i=0; i<halfLength; ++i) {
push(&leftStack, string[i]);
}
Stack rightStack = {NULL};
for(int i=0; i<halfLength; ++i) {
push(&rightStack, string[length-i-1]);
}
int palindrome = 1;
while(!isEmpty(&leftStack)) {
char left = pop(&leftStack);
char right = pop(&rightStack);
if(left != right) {
palindrome = 0;
break;
}
}
if(palindrome) {
printf("%s is a palindrome\n", string);
} else {
printf("%s is not a palindrome\n", string);
}
}
void problemThreeSort() {
int MAX = 255;
Stack counts[MAX];
for(int i=0; i<MAX; ++i) {
counts[i].top = NULL;
}
char *string = "bcfda";
int length = strlen(string);
for(int i=0; i<length; ++i) {
int ch = string[i];
push(&counts[ch], ch);
}
char sorted[length+1];
for(int i=0, j=0; i<MAX; ++i) {
while(!isEmpty(&counts[i])) {
sorted[j++] = pop(&counts[i]);
}
}
sorted[length] = 0;
printf("Input: %s\n", string);
printf("Output: %s\n", sorted);
}
int main() {
testCharStack();
// refer stackconfig.h to change to an int stack
// testIntStack();
problemTwoCheckPalindrome();
problemThreeSort();
return 0;
}