0

I am working on a program to have a linked list print out in reverse using recursion. My recursion code only seems to print out the last movie, then it stops. "gets" does not work in Microsoft Visual Studio, so I am using compile online's c ide and do not have access to a debugger. Expected output is below. The code is also posted below.

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

Actual Output:

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;
}
4
  • But there's no recursion in your program, as you claim. (That would require a function that calls itself.) Commented Dec 6, 2016 at 14:56
  • ..and you even don't need recursion at all for this problem. Commented Dec 6, 2016 at 15:01
  • @MichaelWalz how do I do it without recursion? Commented Dec 6, 2016 at 15:05
  • Take a piece of paper and a pencil and you'll find out. It's not difficult at all. Hint: google is your friend. Commented Dec 6, 2016 at 15:06

2 Answers 2

1

You're not filling current->prev field in struct here:

    if (head == NULL)       /* first structure       */
    {
        head = current;
        current->prev = NULL;
    }
    else                    /* subsequent structures */
    {
        current->prev = prev;
        prev->next = current;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help @koper89.
0

You are not setting the data member prev of the inserted node. You should write for example

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

Take into account that using the variable prev is redundant in this loop.

Also the function gets is unsafe and is not supported by the C Standard any more. Instead use the function fgets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.