c - 链表、递归

标签 c recursion linked-list

我正在开发一个程序,使用递归反向打印链接列表。我的递归代码似乎只打印出最后一部电影,然后就停止了。 “gets”在 Microsoft Visual Studio 中不起作用,因此我使用在线编译的 c ide 并且无法访问调试器。预期输出如下。代码也贴在下面。

Enter first movie title:
Titanic

Enter your rating<0-10>:
5

Enter next movie title:
Inception

Enter your rating<0-10>:
8

Here is the movie list:                                                                                                                                         
Movie: Titanic  Rating: 8                                                                                                                                       
Movie: Inception  Rating: 9                                                                                                                                     
Display list from tail to head:                                                                                                                                 
Here is the movie list:                                                                                                                                         
Movie: Inception Rating : 9
Movie: Titanic   Rating : 8

实际输出:

Here is the movie list:                                                                                                                                         
Movie: Titanic  Rating: 8                                                                                                                                       
Movie: Inception  Rating: 9                                                                                                                                     
Display list from tail to head:                                                                                                                                 
Here is the movie list:                                                                                                                                         
Movie: Inception Rating : 9


#include <stdio.h>
#include <stdlib.h>      /* has the malloc prototype      */
#include <string.h>      /* has the strcpy prototype      */
#define TSIZE    45      /* size of array to hold title   */

struct film 
{
    char title[TSIZE];
    int rating;
    struct film* next;    /* points to next struct in list */
    struct film* prev;   
};

int main(void)
{
    struct film *head = NULL, *prev, *current, *temp;
    struct film *tail = NULL;
    char input[TSIZE];

    /* Gather  and store information          */
    puts("Enter first movie title:");
    while (gets(input) != NULL && input[0] != '\0')
    {
        current = (struct film *) malloc(sizeof(struct film));  //request a new film struct
        if (head == NULL)       /* first structure       */
            head = current;
        else                    /* subsequent structures */
            prev->next = current;
        current->next = NULL;
        strcpy(current->title, input);
        puts("Enter your rating <0-10>:");
        scanf("%d", &current->rating);
        while (getchar() != '\n')
            continue;
        puts("Enter next movie title (empty line to stop):");
        prev = current;
        tail = current;
    }
    /* Show list of movies                    */
    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;
    }

    puts("Display list from tail to head:");
    if (tail == NULL)
        puts("No data entered.");
    else
        puts("Here is the movie list: ");
    current = tail;
    while (current != NULL)
    {
        printf("Movie: %s Rating : %d\n", current->title, current->rating);
        current = current->prev;
    }

    /* Program done, so free allocated memory */
    current = head;
    while (current != NULL)
    {
        temp = current->next;  
        free(current);
        current = temp;
    }

    return 0;
}

最佳答案

您没有在此处填写结构中的 current->prev 字段:

    if (head == NULL)       /* first structure       */
    {
        head = current;
        current->prev = NULL;
    }
    else                    /* subsequent structures */
    {
        current->prev = prev;
        prev->next = current;
    }

关于c - 链表、递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998190/

相关文章:

c - 为什么下面的代码在字符串末尾执行意外的字母?

c - 打印参数的 Oneliner C

c - 使用第一个元素作为枢轴的 C 中的快速排序实现

javascript - 使用 Underscore/Lodash 在 javascript 中执行非变异反尾的最佳方法

c++ - 用智能指针实现一个简单的单向链表

c - 如何在内核中为 tty 驱动程序编写 read() 和 poll() 功能?

java - 递归列出Java中的文件

c# - 避免递归

c++ - 带有标题节点的循环双重链接列表

java - 为什么这会从我的 LinkedList 中删除所有元素?