可執行文件突然停止工作。整個程序可以正常工作,但鏈接列表反轉的部分使得exe文件可以這樣做。這段代碼有什麼錯誤?
#include<stdio.h>
#include<stdlib.h>
struct node{
int info;
struct node *link;
} *start=NULL;
main()
{
int i=1,n,data;
printf("\nEnter the number of nodes you want to enter: ");
scanf("%d",&n);
printf("\nEnter the key no. 1: ");
scanf("%d",&data);
struct node *p=start,*tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;
start=tmp;
while(i<n)
{
printf("\nEnter the key no. %d: ",(i+1));
scanf("%d",&data);
while(p!=NULL)
p=p->link;
tmp->info=data;
tmp->link=NULL;
p=tmp;
i++;
}
p=start;
printf("\nThe list is: ");
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
p=start;
printf("\nThe reversed list is: ");
while(p->link->link!=NULL)
{
p->link->link=p;
p=p->link;
}
start->link=NULL;
start=p->link;
for(p=start;p!=NULL;p=p->link)
printf("%d",p->info);
getch();
return 0; //main shourl return.
}
到底什麼你問,「讓exe文件這樣做呢?」你的問題標題提到一個錯誤代碼,但你沒有顯示錯誤。請閱讀如何發佈正確的問題。 – OldProgrammer