-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
157 lines (138 loc) · 4.01 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "main.h"
#include "SeqList/SeqList.h" // SeqList
#include "LinkList/LinkList.h" // Linked List
#include "Stack/Stack.h" // Stack
#include "Queue/Queue.h" // Queue
#include "String/String.h" // String
int main() {
int choice;
do {
main_menu();
choice = judge_int();
system("cls");
switch (choice) {
case 0: // exit
break;
case 1: // array
seqlist_menu();
break;
case 2: // linked list
linklist_menu();
break;
case 3: // stack
stack_menu();
break;
case 4: // queue
queue_menu();
break;
case 5: // string
string_menu();
break;
case 6: // tree
break;
case 7: // map
break;
default:
printf("Wrong input, please re-enter!\n");
break;
}
} while (choice != 0);
return 0;
}
void visit(ElemType e) {
if (sizeof(e) == sizeof(char)) {
printf("%c -> ", e);
}
else if (sizeof(e) == sizeof(int)) {
printf("%d -> ", e);
}
}
void print_string(char *ch, int len) {
printf("\"");
for (int i = 1; i < len; i++) {
printf("%c", ch[i]);
}
printf("\"");
printf("\n");
}
void get_input_element(ElemType *e) {
printf("Please input the data: ");
if (sizeof((*e)) == sizeof(char)) {
scanf("%c", e);
}
else if (sizeof((*e)) == sizeof(int)) {
*e = judge_int();
}
}
void get_order_position(int *i) {
printf("Please input the position: ");
*i = judge_int();
}
int judge_int(void) {
int len, num = 0, arg = 1;
char word[10] = {0};
int m, j = 1, k;
while (j) {
scanf("%s", word);
len = strlen(word);
for (m = 0; m < len; m++) {
if (word[m] < '0' || word[m] > '9') // Checks for garbled input of other characters
{
printf("Please enter an integer: ");
break;
}
else {
if (m == len - 1)
j = 0;
}
}
}
j = len - 1;
for (m = 0; m < len; m++) // Converts characters back to numbers
{
for (k = 0; k < j; k++)
arg *= 10;
num += (word[m] - '0') * arg;
arg = 1;
j--;
}
return num;
}
char get_choice(void) {
int len;
char word[10] = {0};
while (1) {
scanf("%s", word);
len = strlen(word);
if ((len == 1) && (word[0] == 'Y' || word[0] == 'y' || word[0] == 'N' || word[0] == 'n')) {
break;
}
else {
printf("Please enter Y/y or N/n: ");
}
}
return word[0];
}
void main_menu(void) {
printf("\n");
printf("\t**********************************\n");
printf("\t* Data Structure *\n");
printf("\t*--------------------------------*\n");
printf("\t* 1 | Sequential List *\n");
printf("\t*--------------------------------*\n");
printf("\t* 2 | Linked List *\n");
printf("\t*--------------------------------*\n");
printf("\t* 3 | Stack *\n");
printf("\t*--------------------------------*\n");
printf("\t* 4 | Queue *\n");
printf("\t*--------------------------------*\n");
printf("\t* 5 | String *\n");
printf("\t*--------------------------------*\n");
printf("\t* 6 | Tree *\n");
printf("\t*--------------------------------*\n");
printf("\t* 7 | Map *\n");
printf("\t*--------------------------------*\n");
printf("\t* 0 | Exit *\n");
printf("\t**********************************\n");
printf("\nPlease enter the corresponding number(0-7): ");
}