C:\hw2_sol.c 2012年年年年4月月月11日月 日日 下日下下下下下下下 11:05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define L 50
struct Node {
char word[L];
struct Node *next;
};
struct Node *newNode(const char *word) {
struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
strcpy(tmp->word, word);
tmp->next = NULL;
return tmp;
}
void addWord(char *word, int *len) {
int pos = 0;
static struct Node *head = NULL;
struct Node **tmp = &head, *tmp2;
word[*len] = 0;
*len = 0;
while (*tmp!=NULL && strcmp((*tmp)->word, word)!=0) {
tmp = &(*tmp)->next;
++pos;
}
if (*tmp!=NULL) {
tmp2 = *tmp;
*tmp = (*tmp)->next;
printf("%d", pos);
} else {
tmp2 = newNode(word);
printf("%s", word);
}
tmp2->next = head;
head = tmp2;
}
int main() {
int pos = 0;
char tmp[L], get;
while (scanf("%c", &get)==1) {
if ((get>='a'&&get<='z') || (get>='A'&&get<='Z')) {
tmp[pos++] = get;
} else
-1-
C:\hw2_sol.c 2012年年年年4月月月11日月 日日 下日下下下下下下下 11:05
{
if (pos)
addWord(tmp, &pos);
printf("%c", get);
} }
if (pos)
addWord(tmp, &pos);
return 0;
}
-2-