首页  >  文章  >  后端开发  >  单链表的节点乘积

单链表的节点乘积

WBOY
WBOY转载
2023-08-28 09:13:08617浏览

给定 n 个节点,任务是打印单链表中所有节点的乘积。程序必须从初始节点开始遍历单向链表的所有节点,直到找不到 NULL。

示例

Input -: 1 2 3 4 5
Output -: 120

在上面的例子中,从第一个节点开始遍历所有节点,即 1, 2 3, 4, 5, 6,它们的乘积为 1*2*3*4*5*6 = 120

单链表的节点乘积

下面使用的方法如下

  • 获取一个临时指针,比如节点类型的 temp
  • 将此临时指针设置为头指针指向的第一个节点
  • 当 temp 不为 NULL 时,将 temp 移动到 temp ->next。
  • 设置 Product=product*(temp->data)

算法

  • H2>
    Start
    Step 1 -> create structure of a node and temp, next and head as pointer to a structure node
       struct node
          int data
          struct node *next, *head, *temp
       End
    Step 2 -> declare function to insert a node in a list
       void insert(int val)
          struct node* newnode = (struct node*)malloc(sizeof(struct node))
          newnode->data = val
          IF head= NULL
             set head = newnode
             set head->next = NULL
          End
          Else
             Set temp=head
             Loop While temp->next!=NULL
             Set temp=temp->next
          End
          Set newnode->next=NULL
          Set temp->next=newnode
       End
    Step 3 -> Declare a function to display list
       void display()
          IF head=NULL
             Print no node
          End
          Else
             Set temp=head
             Loop While temp!=NULL
                Print temp->data
                Set temp=temp->next
             End
          End
    Step 4 -> declare a function to find alternate nodes
       void product_nodes()
          declare int product=1
          Set temp=head
       Loop While temp!=NULL
          Set product=product * (temp->data)
          Set temp=temp->next
       End
       Print product
    Step 5 -> in main()
       Create nodes using struct node* head = NULL;
       Call function insert(10) to insert a node
       Call display() to display the list
       Call product_nodes() to find alternate nodes product
    Stop

    示例

    #include<stdio.h>
    #include<stdlib.h>
    //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("</p><p>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;
    }

    输出

    linked list is : 1 2 3 4 5 6
    product of nodes is : 720
  • 以上是单链表的节点乘积的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明:
    本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除