能运行,但结果错误!!
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node
{
int data;
struct node *next;
}*LinkList, ListNode;
static void InitList(LinkList *head);
static void CreateList(LinkList *head);
static void VisitList(LinkList head);
int main(void)
{
LinkList newhead;
InitList(&newhead);
CreateList(&newhead);
VisitList(newhead);
return 0;
getch();
}
static void InitList(LinkList *head)
{
if (*head != NULL)
*head = NULL;
}
static void CreateList(LinkList *head)
{
int data;
ListNode *p;
printf("Enter the number:\n");
scanf("%d", &data);
while (data != 0)
{
p = (LinkList)malloc(sizeof(ListNode));
if (p == NULL) exit(1);
p -> data = data;
p -> next = *head;
*head = p;
printf("Enter the number:\n");
scanf("%d", &data);
}
}
static void VisitList(LinkList head)
{
ListNode *p;
p = head;
while (p)
{
p = p -> next;
printf("%d ", p -> data);
}
}