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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #define TSIZE 45 struct film { char title[TSIZE]; int rating; struct film *next; };
int main(void) { struct film *head = NULL; struct film *prev, *current; char input[TSIZE];
puts("Enter first movie title: "); while(gets(input) != NULL && input[0] != '\0') { current = (struct film *) malloc(sizeof(struct film)); if(head == NULL) { head = current; } else { prev->next = current; } current->next = NULL; strcpy(current->title, input); puts("Enter your rating <0-10>: "); scanf("%d", ¤t->rating); while(getchar() != '\n') { continue; } puts("Enter next movie title (empty line to stop)"); prev = current; }
if(head == NULL) { printf("No data entered. "); } else { printf("Here is the movie list :\n"); }
current = head; while(current != NULL) { printf("Movie: %s Rating: %d\n",current->title,current->rating); current = current->next; } current = head; while(current != NULL) { free(current); current = current->next; } printf("Bye!\n"); }
|