简体   繁体   English

如何颠倒链表的顺序?

[英]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. 我试图以与输入相反的顺序打印出链表的结果。该程序需要3个输入,即乐曲名称,乐曲长度(以秒为单位)和版权。 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) 歌曲1-全明星(歌曲名称),237(秒),0(无版权)

song 2 - Crab Rave, 193, 0 歌曲2-Crab Rave,193,0

song 3 - 7 rings, 185, 1(copyrights) 歌曲3-7响,185,1(版权)

The output should be: 输出应为:

7 rings, 185, 1 7环185 1

Crab Rave, 193, 0 螃蟹狂欢,193,0

All Star, 237, 0 全明星,237,0

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; 通过这种方式,程序流程将建立一个display_recursive-函数调用的“堆栈”,其中第一个节点,然后是第二个节点,直到到达最后一个节点为止。 by then, recursion stops, and the print part of display_recursive is handled, starting with the last node backwards. 到那时,递归停止,从最后一个节点向后开始处理display_recursive的打印部分。

Hope this explanation helps; 希望这种解释有所帮助; Try it out in the debugger to see what happens. 在调试器中尝试一下,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM