কম্পিউটার টিউটোরিয়াল

সিঙ্গলি লিংকড লিস্টের সব নোডের গুণফল বের করা


n সংখ্যক নোড দেওয়া থাকলে, সিঙ্গলি লিংকড লিস্টের সব নোডের গুণফল প্রিন্ট করতে হবে। প্রোগ্রামটি প্রথম নোড থেকে NULL না পাওয়া পর্যন্ত সব নোড ট্রাভার্স করবে।

উদাহরণ

Input: 1 2 3 4 5
Output: 120

উপরের উদাহরণে, প্রথম নোড থেকে সব নোড ট্রাভার্স করা হয়েছে: 1, 2, 3, 4, 5, 6 এবং তাদের গুণফল ১×২×৩×৪×৫×৬ = ৭২০।

সিঙ্গলি লিংকড লিস্টের সব নোডের গুণফল বের করা

অ্যালগরিদম

Start
Step 1: Create structure of a node with temp, next, head pointers
Step 2: Declare function to insert a node in list
Step 3: Declare function to display list
Step 4: Declare function to find product of nodes
        void product_nodes() {
            int product = 1;
            temp = head;
            while (temp != NULL) {
                product = product * (temp->data);
                temp = temp->next;
            }
            Print product
        }
Step 5: In main()
        Create nodes, insert values, display list, call product_nodes()
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("\nproduct of nodes is: %d", product);
}

int main() {
    struct node* head = NULL;
    insert(1);
    insert(2);
    insert(3);
    insert(4);
    insert(5);
    insert(6);
    printf("linked list is: ");
    display();
    product_nodes();
    return 0;
}

আউটপুট

linked list is: 1 2 3 4 5 6
product of nodes is: 720

  1. লিংকড লিস্টের বিকল্প নোডগুলির গুণফল বের করা

  2. সি প্রোগ্রামিং-এ লিংকড লিস্টের নির্দিষ্ট ইনডেক্সের নোড প্রিন্ট করা

  3. সি++-এ সিঙ্গলি লিংকড লিস্টের প্রাইম নোডগুলোর গুণফল বের করার প্রোগ্রাম

  4. সি++-এ সার্কুলার লিংকড লিস্টের নোডগুলোর যোগফল (Sum)