头插法建立单链表


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM