#include #include struct ahel { char *str; struct ahel *next; } ; void addtoahel(struct ahel**); void delfromahel(struct ahel**); void findfromahel(struct ahel*); char *getstring(void); int main() { struct ahel *loend = NULL; int exxit = 0; char input[81]; while(!exxit) { printf("\n\t(L)isa string\n\t(E)emalda stringn\n\t(O)tsi stringi\n\t(S)ure maha\n\nValik: "); fgets(input, 81, stdin); if(input[0] > 90) input[0] -= 32; if(input[0] == 'L') addtoahel(&loend); else if(input[0] == 'E') delfromahel(&loend); else if(input[0] == 'O') findfromahel(loend); else if(input[0] == 'S') exxit = 1; } return 0; } // buffer - tehakse malloc. hoiduda leketest. void addtoahel(struct ahel **loend) { struct ahel *abi; if(*loend == NULL) { *loend = malloc(sizeof(struct ahel*)); abi = *loend; } else { abi = *loend; while(abi->next != NULL) abi = abi->next; abi = abi->next = malloc(sizeof (struct ahel*)); } printf("Sisestage string: "); abi->str = getstring(); abi->next = NULL; return; } void delfromahel(struct ahel **loend) { int i, d; struct ahel *abi, *abii; if(*loend == NULL) { printf("Ahelloend on tyhi!\n"); return; } abi = *loend; printf("Stringi number: "); scanf("%d", &d); if(d==1) { *loend = (*loend)->next; } else { for(i=0; inext->next == NULL) { printf("Selline string puudub!\n"); return; } abi = abi->next; } abii = abi; abi = abi->next; abii->next = abii->next->next; } free(abi->str); free(abi); return; } void findfromahel(struct ahel *loend) { int i; char *buffer; printf("Otsitav string: "); buffer = getstring(); for(i=1; loend != NULL; i++) { if(strcmp(buffer, loend->str) == 0) { printf("Leitud positsioonilt %i.\n", i); return; } loend = loend->next; } printf("Stringi ei leidunud.\n"); return; } char *getstring(void) { char input[81], *buffer; input[0] = ' '; input[1] = 0; buffer = malloc(1); *buffer = 0; while(input[strlen(input)-1] != '\n') { fgets(input, 81, stdin); buffer = realloc(buffer, strlen(buffer) + strlen(input) + 1); strcat(buffer, input); } return buffer; }