2017-05-01
#include <stdio.h>
#include <malloc.h>
typedef struct Link{
int data;
struct Link *next;
}Node;
Node *createLinkList(int n)
{
int i, d;
Node *head=NULL, *p=NULL, *last=NULL;
for(i=0; i<n; i++)
{
printf("数据:");
scanf("%d", &d);
p = (Node *)malloc(sizeof(Node));
p->data=d;
p->next=NULL;
if(head==NULL)
head = p;
else{
last->next=p;
}
last=p;
}
return head;
}
void traverse(Node *head)
{
Node *p=NULL;
p=head;
do{
printf("%-5d", p->data);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void main()
{
int n;
Node *head=NULL;
printf("链表长度:");
scanf("%d", &n);
head = createLinkList(n);
traverse(head);
}