单链表的节点乘积

2023年 8月 29日 74.5k 0

#include
#include
//structure of a node
struct node{
int data;
struct node *next;
}*head,*temp;
//function for inserting nodes into a list
void insert(int val){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
if(head == NULL){
head = newnode;
temp = head;
} else {
temp->next=newnode;
temp=temp->next;
}
}
//function for displaying a list
void display(){
if(head==NULL)
printf("no node ");
else{
temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}
}
//function for finding product
void product_nodes(){
int product=1;
temp=head;
while(temp!=NULL){
product=product * (temp->data);
temp=temp->next;
}
printf("

product of nodes is : %d" ,product);
}
int main(){
//creating list
struct node* head = NULL;
//inserting elements into a list
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
//displaying the list
printf("linked list is : ");
display();
//calling function for finding prodouct
Product_nodes();
return 0;
}

相关文章

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

发布评论