#include
#include
typedef struct _node
{
int data;
struct _node* next;
}Node;
void CreatList(Node* head)
{
Node* p;
int x;
scanf("%d",&x);
while(x)
{
p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = head;
head = p;
scanf("%d",&x);
}
}
void PrintList(Node* head)
{
Node* current = NULL;
current = head;
while(current != NULL)
{
printf("%d",current->data);
current = current->next;
}
}
void FreeList(Node* head)
{
Node* current = head;
while(current != NULL)
{
head = current->next;
free(current);
current = head;
}
}
int main()
{
struct _node* head = NULL;
CreatList(head);
PrintList(head);
FreeList(head);
return 0;
}
因为函数中是不能实现指针参数自身地址的修改的
修改为:
#include<stdio.h>
#include<stdlib.h>
typedef struct _node
{
int data;
struct _node* next;
}Node;
Node* CreatList( )
{
Node* p,*head = NULL;
int x;
scanf("%d",&x);
while(x)
{
p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = head;
head = p;
scanf("%d",&x);
}
return head;
}
void PrintList(Node* head)
{
Node* current = NULL;
current = head;
while(current != NULL)
{
printf("%d",current->data);
current = current->next;
}
}
void FreeList(Node* head)
{
Node* current = head;
while(current != NULL)
{
head = current->next;
free(current);
current = head;
}
}
int main()
{
Node* head = CreatList();
PrintList(head);
FreeList(head);
return 0;
}
指针作为参数,在函数中修改这个指针的指向,需要传递这个指针的地址。
void CreatList(Node** head),
*head = p;
CreatList(&head);