C程序以找到链表的长度

2023年 9月 7日 54.2k 0

#include
#include
typedef struct linklist{
int data;
struct linklist *next;
}node;
int l=0;
int main(){
node *head=NULL,*temp,*temp1;
int len,choice,count=0,key;
do{
temp=(node *)malloc(sizeof(node));
if(temp!=NULL){
printf("

enter the elements in a list : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=temp;
}else{
temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp;
}
}else{
printf("

Memory is full");
}
printf("

press 1 to enter data into list: ");
scanf("%d",&choice);
}while(choice==1);
len=length(head);
printf("The list has %d no of nodes",l);
return 0;
}
//recursive function to find length
int length(node *temp){
if(temp==NULL)
return l;
else{
l=l+1;
length(temp->next);
}
}

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论