Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct _Item {
- char* value;
- struct _Item *next;
- } Item;
- typedef struct {
- Item *first;
- Item *last;
- } list;
- void initList(list *l ) {
- l->first = NULL;
- l->last = NULL;
- }
- void pushFront( list *l, char *value) {
- Item *newItem = (Item*) malloc( sizeof (Item));
- newItem->value = value;
- newItem->next = l->first;
- l->first = newItem;
- if (l->last == NULL) {
- l->last = newItem;
- }
- }
- void pushBack( list *l, char *value) {
- Item *newItem = (Item*) malloc( sizeof (Item));
- newItem->value = value;
- if (l->first == NULL) {
- l->first = newItem;
- } else {
- l->last->next = newItem;
- }
- l->last = newItem;
- newItem->next = NULL;
- }
- void printList(const list *l) {
- const Item *current = l->first;
- while(current) {
- printf(current->next ? "%s -> " : "%s", current->value);
- current = current->next;
- }
- }
- char *readStream(FILE *f) {
- int ending = 0;
- int size = 16;
- char *buffer = (char*)malloc((size_t) size);
- char letter;
- while ( '\n' != ( letter = (char) getc( f )) ) {
- buffer[ ending++ ] = letter;
- if ( ending == size) {
- size += 16;
- char *tmp = realloc( buffer, (size_t) size);
- if ( !tmp ) {
- free( buffer );
- return NULL;
- }
- buffer = tmp;
- }
- }
- buffer[ending] = '\0';
- return buffer;
- }
- int main()
- {
- list list;
- initList( &list);
- char *value;
- do {
- value = readStream(stdin);
- if (strcmp(value, "quit") == 0) {
- break;
- }
- pushFront(&list, value);
- } while (1);
- printf("\n");
- printList(&list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment