我的循环链表代码只打印第一个元素。为什么? [英] My Circular linked list code only prints first element.Why?

查看:105
本文介绍了我的循环链表代码只打印第一个元素。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct node

{

int data;

struct node * next;

} node;



node * addinto(int n,node * start)

{

node * temp =(node *)malloc(sizeof(node));

node * k = start;

temp-> data = n;

temp- > next = start;

start = temp;

node * t2 = start;

while((t2-> next!= k))

{

t2 = t2-> next;

}

t2-> next =开始;

返回开始;

}

无效显示(节点*开始)

{

printf(\ n列表中的元素是:\ n);

node * temp = start;

printf(% d,temp->数据);

temp = temp-> next;

while(temp!= start)

{

printf(%d,temp->数据);

temp = temp-> next;

}

}

int m ain()

{

int n;

node * start = NULL;

do

{

printf(\ n 1.Insert);

printf(\ n 2.Print);

printf(\ n 3.Exit);

printf(\ n输入您的选择:);

scanf(%d) ,&n);

开关(n)

{

案例1:

{

char ch;

do

{

int dn;

printf(\ n Enter元素:);

scanf(%d,&dn);

start = addinto(dn,start);

printf( \ n按y输入更多。 );

scanf(%c,&ch);

} while(ch =='y');

break;

}

案例2:

{

显示(开始);

getch();

休息;

}

案例3:

{

printf(\ n程序现在将终止);

getch();

退出(0);

}

默认值:

{

printf(\ n无效输入!!再试一次!);

}



}

} while(n!= 3);



返回0;

}



我为循环链表制作了以下代码。元素从头开始输入,我写了这样的代码每次输入节点时,最后一个元素获取节点的第一个元素的地址。但由于某种原因,输出只显示第一个元素,并且不会打印更多元素。请帮帮我.ASAP。在此先感谢。

typedef struct node
{
int data;
struct node* next;
}node;

node* addinto(int n,node* start)
{
node* temp=(node*)malloc(sizeof(node));
node* k=start;
temp->data=n;
temp->next=start;
start=temp;
node *t2=start;
while ((t2->next!=k))
{
t2=t2->next;
}
t2->next=start;
return start;
}
void display(node* start)
{
printf("\n The elements in the list are: \n");
node* temp=start;
printf("%d ",temp->data);
temp=temp->next;
while(temp!=start)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int main()
{
int n;
node *start=NULL;
do
{
printf("\n 1.Insert");
printf("\n 2.Print");
printf("\n 3.Exit");
printf("\n Enter your choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
{
char ch;
do
{
int dn;
printf("\n Enter element: ");
scanf("%d",&dn);
start=addinto(dn,start);
printf("\n Press y to enter more. ");
scanf(" %c",&ch);
}while (ch=='y');
break;
}
case 2:
{
display(start);
getch();
break;
}
case 3:
{
printf("\n Program will now terminate");
getch();
exit(0);
}
default:
{
printf("\n Invalid Input!!Try Again!!");
}

}
}while(n!=3);

return 0;
}

I have made the following code for a circular linked list. The elements are entered from the beginning, and i have written the code such that every time a node is entered the last element gets the address of the first element of the node. But for some reason the output only shows the first element and doesnt print further elements. Help me please.ASAP. Thanks in advance.

推荐答案

从调试器开始,并按照您的代码。

仔细查看创建总计时发生的情况两个节点,并使用调试器自己跟踪此列表。

我认为这个问题非常明显,但这是设计的一个重要部分,最好是在这样的简单代码上学习,而不是试着学习一点点代码!
Start with the debugger, and follow your code.
Look closely at what happens when you create a total of two nodes, and follow this list yourself with the debugger.
I think it's pretty obvious what the problem is, but this is an important part of design, and it's best to learn it on a simple bit of code like this rather than try to learn on a huge bit of code!


这篇关于我的循环链表代码只打印第一个元素。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆