循环链接列表在附加第三节点后不起作用
它显示了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的循环条件
temp1!= null
是不正确的,因为它假定列表是不是 cyclic且结束的。但是,由于您的列表是循环的,因此循环将继续迭代。因此,您不应与
null
进行比较,而应与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 withhead
: