给予无限循环请帮助 [英] giving infinite loop please help

查看:61
本文介绍了给予无限循环请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>

struct node{
int data;
struct node* next;
};
struct node* head; //GLOBAL DECLARATION//
void insert(int x);
void print();
int main()
{    int i,n,x;
    head=NULL; //EMPTY LIST//
    printf("how many number?");
     scanf("%d",&n);
    for(i=0;i<n;i++){>
    printf("ENTER THE NUMBER:\n");
    scanf("%d",&x);
    insert(x);
    print();
    } getch();
}
void insert(int x)
{
   struct node* temp=(struct node*)malloc(sizeof(struct node));
    temp->data=x;
    temp->next=head ;
    head=temp;
   }
void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
	printf("%d",temp->data);
	if(temp->next->next==NULL)
	printf("%d",temp->next->data);
	print(temp->next);
    }
    print("\n");

}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

嗯。

您确定要从内部打电话打印吗?

Um.
Are you sure you want to call print from within itself?
void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    print(temp->next);   <<<<<--- What does this call?
    }
    print("\n");  <<<<<--- What does this call?

}

试试这个:

Try this:

void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    printf(temp->next);   
    }
    printf("\n");
}

试试这个:

你的打印功能仍然无效(它需要一个循环)但至少它不会给你堆栈溢出......:笑:

Try this:
Your print function still doesn't work (it needs a loop) but at least it wonlt give you stack overflow... :laugh:


我在这里可以看到两个问题:

1.你在 print()函数应该是 printf

2.你在递归循环中运行print()但是temp总是初始化为head in你从一开始就开始递归,而不是从最后一次印刷开始...

试试这个:

There are two problems I can see here:
1. Your last print call inside the print() function should have been printf
2. You are running print() in a recursive loop but temp always initialized to head so in ever recursion you start from the beginning and not from the last already printed...
Try this:
int main()
{
    int i,n,x;
    head=NULL; //EMPTY LIST//
    printf("how many number?");
    scanf("%d",&n);
    for(i=0;i<n;i++){>
    printf("ENTER THE NUMBER:\n");
    scanf("%d",&x);
    insert(x);
    print(head); // !!! change here !!!
    } getch();
}




void print(node* root)
{
    struct node* temp = root; //TEMP IS LOCAL VARIABLE// // !!! change here !!!
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    print(temp->next);
    }
    printf("\n"); // !!! change here !!!
}


#include

#include



struct node {

int data;

struct node * next;

};

struct node * head; //全局声明//

void insert(int x);

void print();

int main()

{int i,n,x;

head = NULL; //清空名单//

printf(多少个数字?);

scanf(%d,&n);

for(i = 0; i

printf(输入数字:\ n);

scanf(%d,&x);

insert(x);

print();

} getch();

}

void insert(int x)

{

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

temp-> data = x;

temp-> next = head;

head = temp;

}

void print()

{

struct node * temp = head; // TEMP IS LOCAL VARIABLE //

printf( 列表是:);

while(temp!= NULL)

{printf(%d,temp-> data);

temp = temp-> next;

}

printf(\ n);

}
#include
#include

struct node{
int data;
struct node* next;
};
struct node* head; //GLOBAL DECLARATION//
void insert(int x);
void print();
int main()
{ int i,n,x;
head=NULL; //EMPTY LIST//
printf("how many number?");
scanf("%d",&n);
for(i=0;i
printf("ENTER THE NUMBER:\n");
scanf("%d",&x);
insert(x);
print();
} getch();
}
void insert(int x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=head ;
head=temp;
}
void print()
{
struct node* temp=head; //TEMP IS LOCAL VARIABLE//
printf("LIST IS:");
while(temp!=NULL)
{ printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}


这篇关于给予无限循环请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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