标题:一个电影列表正序和逆序显示电影列表。
只看楼主
saihuo092617
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2012-9-1
结帖率:25%
已结贴  问题点数:10 回复次数:2 
一个电影列表正序和逆序显示电影列表。
#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 都指向哪里?求大神指教!!!!!
搜索更多相关主题的帖子: void head include rating former 
2013-04-09 13:18
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
得分:5 
这不就是值传递么,你把 一级指针 看成一个整体

去掉一个 *后,和这个就没什么区别了

程序代码:
void fun(int n)
{  n = 5;  }

int main()
{
    int n = 3;
    fun(n);
    printf("%d\n", n);
    return 0;
}


[fly]存在即是合理[/fly]
2013-04-09 13:36
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:5 
参数里传递 ** 你可以把它看成指针的地址,
传递指针的地址你在函数内就可以改变指针的指向,让指针指向其他区域
参数传递* 仅仅不过是传递指针所指向的地址,你能改变的只有其指向区域的内容,却改变不了指针本身的指向
2013-04-09 14:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-403468-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022077 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved