您的位置:首页 > 理论基础 > 数据结构算法

数据结构之简单链表(尾部插入数据)

2011-11-30 15:59 274 查看
#include  <stdio.h>
#include  <stdlib.h>
typedef struct node
{
int key;
struct node *Next;
}Node;
void Insert(int num,Node *head)
{
Node *L = NULL;
Node *p = NULL;
L = head;
while (L->Next != NULL)
{
L = L->Next;//遍历找到尾节点
}
p = (Node *) malloc (sizeof(Node));//开辟新的节点
p->key = num;//赋值
p->Next = NULL;//设置为尾节点
L->Next = p;//挂在原来尾节点之后

}
void Print_List(Node *head)
{
Node *p = head->Next;
while(p != NULL)//遍历打印节点值
{
printf("%d->", p->key);
p = p->Next;
}
printf("\n");
}
int main()
{
int i;
Node *head = (Node *)malloc(sizeof(Node));
head->Next = NULL;//建立空表头
for (i=0; i<10; i++)
{
Insert(i, head);
}
Print_List(head);
system("PAUSE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐