码迷,mamicode.com
首页 > 其他好文 > 详细

头插法建立单链表

时间:2015-06-09 13:38:06      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
int data;
struct node *next;
}LNode,*LinkList;

LinkList creat_list()
{
LinkList head=NULL;
head=(LinkList)malloc(sizeof(LNode));
if(head==NULL)
{
printf("memory out of use\n");
return NULL;
}
head->next=NULL;
head->data=0;
return head;
}

int insert(LinkList head,int n)
{
for(int i=0;i<n;i++)
{

LinkList head_t=head->next;
LinkList new_node=NULL;


new_node=(LinkList)malloc(sizeof(LNode));
if(new_node==NULL)
{
printf("memory out of use\n");
return -1;
}
printf("please input the value:\n");
scanf("%d",&new_node->data);
new_node->next=head_t;
head->next=new_node;
}

return 0;
}
int show_list(LinkList head)
{
LinkList tem;
tem=head->next;
while(tem)
{
printf("%3d",tem->data);
tem=tem->next;
}
return 0;
}

int main()
{
LinkList head;
int n;
printf("please input the size which you want to insert\n");
scanf("%d",&n);
head=creat_list();
insert(head,n);
show_list(head);
return 0;
}

头插法建立单链表

标签:

原文地址:http://www.cnblogs.com/loveyan/p/4563160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!