• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Crazy丶
享受现在的每一天........
博客园    首页    新随笔    联系   管理    订阅  订阅

头插法建立单链表

头插法建立单链表

#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;
}

posted @ 2015-06-09 13:00  Crazy丶  阅读(3747)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3