首頁 > 後端開發 > C++ > 主體

單鍊錶的節點乘積

WBOY
發布: 2023-08-28 09:13:08
轉載
587 人瀏覽過

給定 n 個節點,任務是列印單鍊錶中所有節點的乘積。程式必須從初始節點開始遍歷單向鍊錶的所有節點,直到找不到 NULL。

範例

Input -: 1 2 3 4 5
Output -: 120
登入後複製

在上面的範例中,從第一個節點開始遍歷所有節點,即1, 2 3, 4, 5, 6,它們的乘積為1*2* 3*4*5*6 = 120

< p>單鍊錶的節點乘積

下面所使用的方法如下

  • 取得一個暫存指針,例如節點類型的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
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!