单链表是数据结构中最简单的,它的每一个节点只有一个指向后一个节点的指针,最后一个节点指向null
建立
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
node* next;
} node;
node* create()
{
int i = 0;
node* head = NULL;
node* p = NULL;
node* q = NULL;
int x = 0;
head = (node*)malloc(sizeof(node));
while(1)
{
printf("please input the data:");
scanf("%d",&x);
if(x == 0)
{
break;
}
p = (node*)malloc(sizeof(node));
p->data = x;
if(++i == 1)
{
head->next = p;
}
else
{
q->next = p;
}
q = p;
}
q->next = NULL;
return head;
}
int main()
{
node* p = NULL;
node* head = create();
p = head->next;
while(p != NULL)
{
printf("The LinkList is:%d\n",p->data);
p = p ->next;
}
return 0;
}