【问题标题】:how to move first element to last of a linked list in c language?如何在c语言中将第一个元素移动到链表的最后一个?
【发布时间】:2015-02-20 22:41:10
【问题描述】:

我正在尝试获取链表的第一个元素并将其移动到最后一个位置。我是用这段代码做到的:

            pend=pstart;
            while(pend->next != NULL) // go to the last element
            {
                pend = pend->next;
            }
            pend->next=pstart;
            pend=pstart;
            pstart=pstart->next;
            pend->next=NULL;

但似乎我可能遗漏了一些东西,因为我没有得到我想要的结果。所以,我的问题是:这段代码正确吗?如果没有,请帮我修复它。谢谢。

【问题讨论】:

  • 您的代码在我看来是正确的。您能否详细说明“我没有得到我想要的结果”?
  • 另外,需要更多的上下文来理解这个问题,最好是Minimal, Complete, and Verifiable example
  • 对我来说看起来是正确的。

标签: c list linked-list


【解决方案1】:

您的代码是正确的。你在其他地方搞砸了。

解决此类问题的最佳方法是使用框(节点)和箭头(链接)绘制链表。

【讨论】:

    【解决方案2】:
            pend=pstart;
            while(pend->next != NULL) // go to the last element
            {
                pend = pend->next;
            }
            pend->next=pstart;
            pstart=pstart->next;
            pend->next->next= null;
    

    【讨论】:

    • 这看起来与 OP 的代码完全相同。
    • 虽然这个代码块可能会回答这个问题。请尝试在代码中添加相关解释,以改善您的答案。见stackoverflow.com/help/how-to-answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2023-03-16
    相关资源
    最近更新 更多