> 백엔드 개발 > C++ > 본문

단일 연결 리스트의 노드 곱

WBOY
풀어 주다: 2023-08-28 09:13:08
앞으로
587명이 탐색했습니다.

n개의 노드가 주어지면 작업은 단일 연결 리스트의 모든 노드의 곱을 인쇄하는 것입니다. 프로그램은 NULL이 발견되지 않을 때까지 초기 노드부터 시작하여 단방향 연결 목록의 모든 노드를 순회해야 합니다.

Example

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이 아닌 경우 set temp는 temp ->next로 이동합니다.
  • Set Product=product*(temp->data)

Algorithm
  • 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
    로그인 후 복사

    Example

    #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;
    }
    로그인 후 복사

    Output

    linked list is : 1 2 3 4 5 6
    product of nodes is : 720
    로그인 후 복사

    위 내용은 단일 연결 리스트의 노드 곱의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

  • 관련 라벨:
    원천:tutorialspoint.com
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!