顺序创建链表
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat1(int n)
{
struct node *head, *tail, *p;
int i;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=1; i<=n; i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data );
p->next =NULL;
tail->next =p;
tail=p;
}
return (head);
}
void main()
{
int x;
scanf("%d", &x);
struct node *head;
struct node *q;
head = creat1(x);
q = head;
while(q->next !=NULL)
{
if(q->next->next ==NULL)
{
printf("%d\n", q->next->data );
}
else
{
printf("%d ", q->next ->data );
}
q = q->next ;
}
}

浙公网安备 33010602011771号