#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node* next;
}Node,*Lnode;
void insert(Lnode head) //插入数据
{
int i;
Lnode q; //定义全局变量q,用q来辅助构造链表
for(i=0;i<7;i++)
{
Lnode p=(Lnode)malloc(sizeof(Node));
printf("please input the num: ");
scanf("%d",&(p->data));
if(head->next==NULL)
{
head->next=p;
p->next=NULL;
q=head->next;
}
else
{
q->next=p;
p->next=NULL;
q=p;
}
}
}
void print(Lnode head)
{
int i;
Lnode p=head->next;
for(i=0;i<7;i++)
{
printf("%d ",p->data);
p=p->next;
}
}
void main()
{
Lnode head;
head=(Lnode)malloc(sizeof(Node));
head->next=NULL;
insert(head);
print(head);
}
自己想到一个算法,可以正向构建链表,跟一般的反向不一样,拿出来晒晒,呵呵