我这个单链表的创建和遍历哪里有错 总是奔溃
#include <stdio.h>#include <stdlib.h>
#define Error 0
typedef struct Node
{
int data;
struct Node * Next;
}Node;
//链表的创建
void create_list(Node * p)
{
int data;
Node * tail;//尾指针 永远指向最后一个节点;
Node * node;
p = (Node*)malloc(sizeof(Node));
//tail = (Node*)malloc(sizeof(Node));
if( p == NULL )
{
printf("内存创建失败!");
return Error;
}
p->Next = tail;
tail->Next = NULL;
//p->Next = NULL;
//tail = p;
while(1)
{
printf("输入数据: 输入0结束\n");
scanf("%d",&data);
if(data != 0)
{
node = (Node*)malloc(sizeof(Node));
if( node == NULL )
{
printf("内存创建失败!");
return Error;
}
node->data = data;
tail->Next = node;
node->Next = NULL;
tail = node;
}
else
{
return 1;
}
}
}
//链表的遍历
void traverse_list(Node * head)
{
Node * p;
p = head;
while(1)
{
if(p->Next == NULL)
{
printf("为空表");
return 0;
}
printf("数据为:%d\n",p->data);
p = p->Next;
}
printf("OK");
}
int main()
{
Node * p;
create_list(p);
traverse_list(p);
return 0;
}
[此贴子已经被作者于2017-2-1 19:14编辑过]