循环链接列表在附加第三节点后不起作用

发布于 2025-01-26 19:19:18 字数 1087 浏览 6 评论 0原文

它显示了2个节点的输出 我在做什么错? 这是我编写的所有其他程序的附加方法,没有我测试过所有内容的错误。

void append(nd element)
{
    nd temp = head;
    nd temp1 = null;
    while(temp != null)
    {
        temp1 = temp;
        temp = temp.nxt;
    }
    element.nxt = head;
    temp1.nxt = element;
}

void printCLL()
{
        nd temp = head;
        
        do
        {
            System.out.print(temp.data+" --> ");
            temp = temp.nxt;
        }
        while(temp != head);
}

public static void  main(String ar[])
{
        CLList clobj = new CLList();

        nd nn1 = new nd(10);
        clobj.head = nn1;    //working
        nd nn2 = new nd(20);   
        clobj.append(nn2);     //working
        nd nn3 = new nd(30); 
        clobj.append(nn3);   //not working
        nd nn4 = new nd(40);
        clobj.append(nn4);    //not working
         
        nd nn5 = new nd(25); 
        clobj.insertAfter(nn1, nn5); //this method also working if node added 
                
        System.out.println("Printing Circular LinkedList : ");
        
        clobj.printCLL();
    
}

It's Showing output for 2 nodes but after adding 3rd node it doesn't showing any output,
what i'm doing anything wrong??
This is the append method that i have written all the other program have no errors i have tested all the things.

void append(nd element)
{
    nd temp = head;
    nd temp1 = null;
    while(temp != null)
    {
        temp1 = temp;
        temp = temp.nxt;
    }
    element.nxt = head;
    temp1.nxt = element;
}

void printCLL()
{
        nd temp = head;
        
        do
        {
            System.out.print(temp.data+" --> ");
            temp = temp.nxt;
        }
        while(temp != head);
}

public static void  main(String ar[])
{
        CLList clobj = new CLList();

        nd nn1 = new nd(10);
        clobj.head = nn1;    //working
        nd nn2 = new nd(20);   
        clobj.append(nn2);     //working
        nd nn3 = new nd(30); 
        clobj.append(nn3);   //not working
        nd nn4 = new nd(40);
        clobj.append(nn4);    //not working
         
        nd nn5 = new nd(25); 
        clobj.insertAfter(nn1, nn5); //this method also working if node added 
                
        System.out.println("Printing Circular LinkedList : ");
        
        clobj.printCLL();
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2025-02-02 19:19:18

您的循环条件temp1!= null是不正确的,因为它假定列表是不是 cyclic且结束的。但是,由于您的列表是循环的,因此循环将继续迭代。

因此,您不应与null进行比较,而应与head

void append(nd element)
{
    if (head == null) {
        head = element;
    } else {
        nd temp = head;
        // Find node that precedes head
        while (temp.nxt != head) 
        {
            temp = temp.nxt;
        }
        temp.nxt = element;
    }
    element.nxt = head;
}

Your loop condition temp1 != null is not correct, as it assumes the list is not cyclic and has an end. But as your list is cyclic, the loop will keep iterating.

So you should not compare with null but with head:

void append(nd element)
{
    if (head == null) {
        head = element;
    } else {
        nd temp = head;
        // Find node that precedes head
        while (temp.nxt != head) 
        {
            temp = temp.nxt;
        }
        temp.nxt = element;
    }
    element.nxt = head;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文