Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 23, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/803c57856f9d15208dcf7c1cff003c39 to your computer and use it in GitHub Desktop.
Save maehrm/803c57856f9d15208dcf7c1cff003c39 to your computer and use it in GitHub Desktop.
平成20年度春期試験_基本情報午後問4
#include <stdio.h>
#include <string.h>
typedef struct {
int next;
char value[4];
} LIST;
LIST list[100];
int listsize = 0, first = -1;
void makeList(const char []);
int organizingSearch(const char []);
void printList(void);
void makeList(const char mvalue[]) {
if (listsize != 0) {
list[listsize - 1].next = listsize;
}
else {
first = 0;
}
strcpy(list[listsize].value, mvalue);
list[listsize++].next = -1;
}
int organizingSearch(const char svalue[]) {
int current;
int temp;
current = first;
while (current != -1) {
if (strcmp(list[current].value, svalue) == 0) {
if (current != first) {
list[temp].next = list[current].next;
list[current].next = first;
first = current;
}
break;
}
else {
temp = current; /* α */
current = list[current].next;
}
}
return current;
}
void printList(void) {
int current;
current = first;
while (current != -1) {
printf("%s", list[current].value);
current = list[current].next;
if (current != -1) printf(" -> ");
}
printf("\n");
}
int main(void) {
char *str[] = {"usb", "cgi", "cpu", "dos", "fat"};
int i, size = sizeof(str) / sizeof(str[0]);
for (i = 0; i < size; i++) {
makeList(str[i]);
}
printList();
printf("%d\n", organizingSearch("cgi"));
printList();
printf("%d\n", organizingSearch("cpu"));
printList();
printf("%d\n", organizingSearch("dos"));
printList();
return 0;
}
@maehrm
Copy link
Author

maehrm commented Mar 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment