#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
int i;
void output(struct list *head);
void creat(struct list *head, int n)//头插法
{
struct list *p, *q;
head->next=NULL;
p=head;
for(i=0; i<n; i++)
{
q=(struct list*)malloc( sizeof(struct list) );
scanf("%d", &q->data);
q->next = p->next;
p->next=q;
p=q;
}
}
void creat_1(struct list *head, int n)//尾插法
{
struct list *p;
head->next=NULL;
for(i=0; i<n; i++)
{
p=(struct list*)malloc( sizeof(struct list) );
scanf("%d", &p->data);
p->next=head->next;
head->next=p;
}
}
void output(struct list *head)
{
struct list *p=head->next;
while(p)
{
printf("%d ", p->data);
p=p->next;
}
puts("\n\n");
}
void main()
{
struct list *head;
head=(struct list*)malloc( sizeof(struct list) );
creat(head, 5);
output(head);
creat_1(head, 5);
output(head);
}
C语言:链表建立 头插法 尾插法
最新推荐文章于 2024-09-11 21:35:35 发布