#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef struct node *Link;
void CreatLink(Link *head)
{
*head = NULL;
}
void IsMallocOk(Link new_node)
{
if (new_node == NULL)
{
printf ("No enough memory to allocate!\n");
exit(-1);
}
}
void CreatNewnode(Link *new_node)
{
*new_node = (Link)malloc(sizeof(Node));
IsMallocOk(*new_node);
}
void InsertNodeTail(Link *head, Link new_node)
{
Link temp;
temp = *head;
if (*head == NULL)
{
new_node -> next = *head; //若原链表为空表,那么新建节点置为头节点
*head = new_node;
}
else
{
while (temp -> next != NULL)
{
temp = temp -> next; //若原链表为非空,那么移动temp直到指向表尾
}
temp -> next = new_node;
new_node -> next = NULL; //让末节点的指针域指向新建节点
}
}
void DisplayNode(Link head) //打印链表节点
{
Link temp;
temp = head;
while (temp != NULL)
{
printf ("%d ", temp -> num);
temp = temp -> next;
}
printf ("\n");
}
int main()
{
int Arr[] = {1,3,2,16,5,4,65,36,7,10}; //初始化一个数组
Link head = NULL;
Link new_node = NULL;
int i = 0;
CreatLink(&head); //创建一个头节点
for (i = 0; i < 10; i++)
{
CreatNewnode(&new_node); //创建新的节点
new_node -> num = Arr[i]; //将数组的数据写入节点
InsertNodeTail(&head, new_node);
}
DisplayNode(head);
return 0;
}
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef struct node *Link;
void CreatLink(Link *head)
{
*head = NULL;
}
void IsMallocOk(Link new_node)
{
if (new_node == NULL)
{
printf ("No enough memory to allocate!\n");
exit(-1);
}
}
void CreatNewnode(Link *new_node)
{
*new_node = (Link)malloc(sizeof(Node));
IsMallocOk(*new_node);
}
void InsertNodeTail(Link *head, Link new_node)
{
Link temp;
temp = *head;
if (*head == NULL)
{
new_node -> next = *head; //若原链表为空表,那么新建节点置为头节点
*head = new_node;
}
else
{
while (temp -> next != NULL)
{
temp = temp -> next; //若原链表为非空,那么移动temp直到指向表尾
}
temp -> next = new_node;
new_node -> next = NULL; //让末节点的指针域指向新建节点
}
}
void DisplayNode(Link head) //打印链表节点
{
Link temp;
temp = head;
while (temp != NULL)
{
printf ("%d ", temp -> num);
temp = temp -> next;
}
printf ("\n");
}
int main()
{
int Arr[] = {1,3,2,16,5,4,65,36,7,10}; //初始化一个数组
Link head = NULL;
Link new_node = NULL;
int i = 0;
CreatLink(&head); //创建一个头节点
for (i = 0; i < 10; i++)
{
CreatNewnode(&new_node); //创建新的节点
new_node -> num = Arr[i]; //将数组的数据写入节点
InsertNodeTail(&head, new_node);
}
DisplayNode(head);
return 0;
}