c - 我的循环链表不显示最后一个节点

标签 c data-structures linked-list

这是显示我创建的单个循环链表的代码。该程序获取该值,但显示插入的插入值,直到从头开始的倒数第二个节点。 插入的代码如下-

if(head==NULL)
{
    new_node=(struct list *)malloc(sizeof(struct list));
    printf("Enter the data : ");
    scanf("%d",&new_node->data);
    new_node->next=NULL;
    head=new_node;
    link=head;
    head->next=head;
}
else
{
    new_node=(struct list *)malloc(sizeof(struct list));
    printf("Enter the data : ");
    scanf("%d",&new_node->data);
    new_node->next=head;
    link->next=new_node;
    link=new_node;
}
void display()
{
    struct list *link;
    link=head;
    while(link->next!=head)
    {
        printf("%d\t",link->data);
        link=link->next;
    }

最佳答案

link = head;
do {
    printf("%d\t",link->data);
    link=link->next;
} while(link!=head);

关于c - 我的循环链表不显示最后一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46975581/

相关文章:

c - 在 C 中读取行并在达到限制时停止

java - 如果我们将 'put' 和新值赋给 Linkedhashmap 中已经存在的键会发生什么

java - 一种三维数据结构,用于保存项目之间的位置关系

c++ - 为什么在创建入栈函数时使用指向栈的指针?

c - 链表中出现无限循环

c - 当多个线程在循环中运行时索引如何受到影响

c - 仅为数组的一个元素分配内存

c - GNU 链接器和架构 i386

c - 虽然循环没有按预期工作以将各种情况作为终端的输入)

c - C 中的链表,未正确链接