cpointerslinked-listreversing

How can I reverse the order of a linked list?


I am trying to print out the results of the linked list in the reverse order that they were entered in. The program takes 3 inputs, Song name, song length (in seconds) and copyright. The program should take the list of songs and print the in the reverse order that they were entered in.

I am not too familiar with linked list. This is my first time using it as sort of a database.

#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)

//defining struct
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
}node;

//defining prototypes
node *create(int n);
void display(node *head);


int main()
{
    int n = 0;

    node *head = NULL;

    printf("How many entries?\n");
    scanf("%d", &n);

    //call to create list
    head = create(n);

    printf("\nThe linked list in order is:\n");
    display(head);

return 0;
}

node *create(int n)
{
node *head = NULL;
node *temp = NULL;
node *p = NULL;

for (int i = 0; i < n; i++)
{
    temp = (node*)malloc(sizeof(node));
    printf("What is the name of song %d\n", i + 1);
    //fgets(temp->songName, 20, stdin);
    scanf("%s", &temp->songName);

    printf("What is the length of song %d (in seconds)?\n", i + 1);
    scanf("%d", &temp->songLength);

    printf("Is song %d copyrighted?(1 = YES, 0 = NO)\n", i + 1);
    scanf("%d", &temp->copyright);

    temp->next = NULL;

    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        // if not empty, attach new node at the end
        p = head;

        while (p->next != NULL)
        {
            p = p->next;
        }
        p->next = temp;
    }
}
return head;
}

void display(node *head)
{
    node *p = NULL;

    if (head == NULL)
    {
        printf("List is empty\n");
    }
    else
    {
            p = head;
        while (p != NULL)
        {
        printf("Song: %s, ", p->songName);
        printf("%d minutes, ", p->songLength);
        if (p->copyright == 1)
        {
            printf("Copyrighted\n");
        }
        else if (p->copyright == 0)
        {
            printf("No copyright\n");
        }
            p = p->next;
    }
}
}

So if Input the following:

Song 1 - All Star (song name), 237 (seconds), 0 (no copyrights)

song 2 - Crab Rave, 193, 0

song 3 - 7 rings, 185, 1(copyrights)

The output should be:

7 rings, 185, 1

Crab Rave, 193, 0

All Star, 237, 0


Solution

  • If you have a single (forward) linked list, the probably easiest way to print it in reverse order is using recursion:

    void display_recursive(node *n) {
        if (!n) {
          return;
        }
        display_recursive(n->next);
        printf("Song: %s, ", n->songName);
        ...
    }
    

    Recursion means that a function is calling itself (until some end-condition, the anchor, is reached). By that way, program flow will build up a "stack" of display_recursive- function calls, with the first node, then the second node, ..., until it reaches the last node; by then, recursion stops, and the print part of display_recursive is handled, starting with the last node backwards.

    Hope this explanation helps; Try it out in the debugger to see what happens.