頭插法建立單鏈表


#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