链表学习
细细理解
1 #include <stdio.h> 2 #include <iostream> 3 #include <string> 4 #include <string.h> 5 #include <math.h> 6 #include <algorithm> 7 #include <stdlib.h> 8 using namespace std; 9 struct film{ 10 char title[45]; 11 int rating; 12 struct film * next; 13 }; 14 int main() 15 { 16 struct film * head = NULL; 17 struct film * prev, * current; 18 char input[45]; 19 20 puts("enter first movie title: "); 21 while(gets(input) != NULL && input[0] != '\0'){ 22 current = (struct film *)malloc(sizeof(struct film)); 23 if(head == NULL) 24 head = current; 25 else 26 prev->next = current; 27 current->next = NULL; 28 strcpy(current->title,input); 29 puts("rating : "); 30 scanf("%d",¤t->rating); 31 while(getchar() != '\n') 32 continue; 33 puts("enter next movie title : "); 34 prev = current; 35 } 36 if(head == NULL) 37 printf("NO data entered."); 38 else 39 printf("Here is the movie list: \n"); 40 current = head; 41 while (current != NULL) 42 { 43 printf("Movie: %s Rsting: %d\n",current->title,current->rating); 44 current = current->next; 45 } 46 current = head; 47 while (current != NULL) 48 { 49 free(current); 50 current = current->next; 51 } 52 printf("Bye!\n"); 53 return 0; 54 }