简单链表程序运行停止问题
# include <stdio.h># include <stdlib.h>
# include <conio.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
int main(void)
{
link ptr, head;
int num, i;
ptr = (link)malloc(sizeof(node));
ptr = head;
printf("please enter 5 numbers:\n");
for(i=0; i<=4; i++)
{
scanf("%d", &num);
ptr->data = num;
ptr->next = (link)malloc(sizeof(node));
if(i == 4)
ptr->next = NULL;
else
ptr = ptr->next;
}
ptr = head;
while(ptr != NULL)
{
printf("The value is %d\n", ptr->data);
ptr = ptr->next;
}
ptr = head;
while(ptr != NULL)
{
ptr = head;
head = ptr->next;
free(ptr);
}
getch();
return 0;
}
这是程序,编译没问题,但是运行后,将输入的链表显示之后,程序出现停止运行,并返回return 3221225477
如图:
想了好久,没看出来哪里的问题。希望大家帮忙看看,不胜感激!