#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
int create_link(Link * head)
{
*head = (Link) malloc (sizeof(Node));
if(!(*head))
{
printf("error!\n");
return 0;
}
(*head) -> next = NULL;
return 1;
}
void insert_node_head(Link * head,int n, int a[])
{
Link tmp;
int i;
*head = (Link) malloc (sizeof(Node));
if(!(head))
{
printf("error!\n");
exit(-1);
}
(*head) -> next = NULL;
for(i = 0;i < n;i++)
{
tmp = (Link) malloc (sizeof(Node));
tmp -> num = a[i];
tmp -> next = (*head) -> next;
(*head) -> next = tmp;
}
}
void display_node(Link head)
{
Link tmp = head -> next;
while(tmp != NULL)
{
printf("%5d",tmp -> num);
tmp = tmp -> next;
}
printf("\n");
}
int main()
{
Link head = NULL;
int a[10];
int i;
printf("输入一个10个数的数组:\n");
for(i = 0;i < 10;i++)
{
scanf("%d",&a[i]);
}
create_link(&head);
insert_node_head(&head, 10, a);
display_node(head);
return 0;
}
#include <stdlib.h>
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
int create_link(Link * head)
{
*head = (Link) malloc (sizeof(Node));
if(!(*head))
{
printf("error!\n");
return 0;
}
(*head) -> next = NULL;
return 1;
}
void insert_node_head(Link * head,int n, int a[])
{
Link tmp;
int i;
*head = (Link) malloc (sizeof(Node));
if(!(head))
{
printf("error!\n");
exit(-1);
}
(*head) -> next = NULL;
for(i = 0;i < n;i++)
{
tmp = (Link) malloc (sizeof(Node));
tmp -> num = a[i];
tmp -> next = (*head) -> next;
(*head) -> next = tmp;
}
}
void display_node(Link head)
{
Link tmp = head -> next;
while(tmp != NULL)
{
printf("%5d",tmp -> num);
tmp = tmp -> next;
}
printf("\n");
}
int main()
{
Link head = NULL;
int a[10];
int i;
printf("输入一个10个数的数组:\n");
for(i = 0;i < 10;i++)
{
scanf("%d",&a[i]);
}
create_link(&head);
insert_node_head(&head, 10, a);
display_node(head);
return 0;
}