初建链表

一.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int date;
struct node *next;
}Node;
int main()
{
Node *head=NULL;
int number;
do{
scanf("%d",&number);
if(number!=0){
Node *p=(Node *)malloc(sizeof(Node));
p->date=number;
p->next=NULL;
Node *last=head;
if(last){
while(last->next){
last=last->next;
}
last->next=p;
}
else{
head=p;
}
}
}
while(number!=0);
return 0;
}
二.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
 int date;
 struct node *next;
}Node;
Node *creatlist(int v[],int n)
{
 Node *head,*p,*q;
 int i;
 if(n<=0)
  return NULL;
 q=(Node *)malloc(sizeof(Node));
 q->date=v[0];
 head=q;
 for(i=1;i<n;i++){
  p=(Node *)malloc(sizeof(Node));
  p->date=v[i];
  q->next=p;
  q=p;
 }
 q->next=NULL;
 return (head);
}
int main()
{
 int v[100];
 int n,i;
 scanf("%d",&n);
 for(i=0;i<n;i++)
  scanf("%d",&v[i]);
 printf("%d",*creatlist(v,n));
 return 0;
}

猜你喜欢

转载自blog.csdn.net/LiAiZu/article/details/85239872