一个电影列表正序和逆序显示电影列表。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define TSIZE 45
struct movie{
char name[TSIZE];
int rating;
struct movie * next;
struct movie * former;
};
void CreateMovies(struct movie **phead, struct movie **pend);
void DisplayOriginal(struct movie * head, struct movie * end);
void DisplayReverse(struct movie * head, struct movie * end);
void FreeMoives(struct movie * head, struct movie * end);
int main (void)
{
struct movie * head = NULL, * end = NULL;
CreateMovies(&head, &end);
DisplayOriginal(head, end);
DisplayReverse(head, end);
FreeMoives(head, end);
return 0;
}
void CreateMovies(struct movie **phead, struct movie **pend)
{
char input[TSIZE];
puts("Enter first movie title: ");
while(gets(input) != NULL && input[0] != '\0')
{
if((*phead) == NULL)
{
(*phead) = malloc(sizeof(struct movie));
(*pend) = (*phead);
}
else
{
(*pend)->next = malloc(sizeof(struct movie));
(*pend)->next->former = (*pend);
(*pend) = (*pend)->next;
}
strcpy((*pend)->name, input);
puts("Enter your rating: ");
scanf("%d",&(*pend)->rating);
getchar();
puts("Enter next movie title (empty line to stop): ");
}
}
void DisplayOriginal(struct movie * head, struct movie * end)
{
if(head == NULL)
{
printf("No movies in the list\n");
return ;
}
printf("display in the original order:\n");
while(head != end)
{
printf("%s\t\t%d\n",head->name, head->rating);
head = head->next;
}
printf("%s\t\t%d\n",head->name, head->rating);
}
void DisplayReverse(struct movie * head, struct movie * end)
{
if(end == NULL)
{
printf("No novies in the list.\n");
return ;
}
printf("display in the reverse order:\n");
while(end != head)
{
printf("%s\t\t%d\n",end->name, end->rating);
end = end->former;
}
printf("%s\t\t%d\n",head->name, head->rating);
}
void FreeMoives(struct movie * head, struct movie * end)
{
struct movie * previous;
if(head == NULL) return ;
while(head != end)
{
previous = head;
head = head->next;
free(previous);
}
free(head);
}
void CreateMovies(struct movie **phead, struct movie **pend)这个函数为什么要用指向指针的指针,用void CreateMovies(struct movie * phead, struct movie * pend)不行么,有什么不一样?
在这几个函数中 end 都指向哪里?求大神指教!!!!!