#include <stdio.h>
#include <stdlib.h>
//节点的类型
struct node
{
int data; //数据域
struct node *next; //指针域 保存下一个节点的地址
};
struct node* create_list()
{
struct node *head = NULL, *pnew = NULL, *tail=NULL;
int x;
scanf("%d", &x);
while(x)
{
//1、创建一个节点,并给每个成员赋值
pnew = (struct node*)malloc(sizeof(struct node));
if(NULL == pnew)
{
printf("malloc error! %s,%d\n",__FILE__, __LINE__);
break;
}
pnew->data = x;
pnew->next = NULL;
//2、加入链表
if(NULL == head)//第一个节点 head保存第一个节点的地址
{
head = pnew;
tail = pnew;
}
else
{
tail->next = pnew;
tail = pnew;
}
scanf("%d", &x);
}
return head;
}
void show_list(struct node *head)
{
if(NULL == head)
{
printf("empty list!\n");
return ;
}
struct node *p = head;
printf("%p-->", head);
while(p != NULL)
{
printf("[%d|%p]-->", p->data,p->next);
p = p->next;
}
printf("\n");
}
int main()
{
struct node *head = NULL;
head = create_list();
show_list(head);
return 0;
}
#include <stdlib.h>
//节点的类型
struct node
{
int data; //数据域
struct node *next; //指针域 保存下一个节点的地址
};
struct node* create_list()
{
struct node *head = NULL, *pnew = NULL, *tail=NULL;
int x;
scanf("%d", &x);
while(x)
{
//1、创建一个节点,并给每个成员赋值
pnew = (struct node*)malloc(sizeof(struct node));
if(NULL == pnew)
{
printf("malloc error! %s,%d\n",__FILE__, __LINE__);
break;
}
pnew->data = x;
pnew->next = NULL;
//2、加入链表
if(NULL == head)//第一个节点 head保存第一个节点的地址
{
head = pnew;
tail = pnew;
}
else
{
tail->next = pnew;
tail = pnew;
}
scanf("%d", &x);
}
return head;
}
void show_list(struct node *head)
{
if(NULL == head)
{
printf("empty list!\n");
return ;
}
struct node *p = head;
printf("%p-->", head);
while(p != NULL)
{
printf("[%d|%p]-->", p->data,p->next);
p = p->next;
}
printf("\n");
}
int main()
{
struct node *head = NULL;
head = create_list();
show_list(head);
return 0;
}