কম্পিউটার

লিঙ্ক করা তালিকার বিকল্প নোডের পণ্য


n নোডের সাথে দেওয়া টাস্ক হল একটি লিঙ্ক করা তালিকায় বিকল্প নোডের পণ্য মুদ্রণ করা। প্রোগ্রামটিকে অবশ্যই নোডের অবস্থান পরিবর্তন না করে শুধুমাত্র বিকল্প নোডের পণ্য মুদ্রণ করতে হবে।

উদাহরণ

Input -: 10 20 30 40 50 60
Output -: 15000

উপরের উদাহরণে, প্রথম নোড থেকে শুরু করে 10টি বিকল্প নোড হল 10, 30, 50 এবং তাদের গুণফল হল 10*30*50 =15000।

লিঙ্ক করা তালিকার বিকল্প নোডের পণ্য

উপরের চিত্রে, নীল রঙের নোডগুলি হল বিকল্প নোড, যদি আমরা প্রথম নোড থেকে শুরু করি এবং লাল রঙের নোডগুলি উল্লেখযোগ্য নোড নয়৷

নিচে ব্যবহৃত পদ্ধতিটি নিম্নরূপ

  • একটি অস্থায়ী পয়েন্টার নিন, ধরা যাক, টেম্প অফ নোড

  • এই টেম্প পয়েন্টারটিকে প্রথম নোডে সেট করুন যা হেড পয়েন্টার দ্বারা নির্দেশিত হয়

  • টেম্প-এ টেম্প ->পরবর্তী -> পরবর্তীতে সরান যখন পরিস্থিতি (temp->পরবর্তী!=NULL &&temp!=NULL &&temp->next->next!=NULL) সত্য ধরে রাখে

  • পণ্য=পণ্য*(টেম্প->ডেটা)

    সেট করুন

অ্যালগরিদম

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 alternate()
      declare int product
      Set temp=head
      Set product=head->data
      Loop While(temp->next!=NULL && temp!=NULL && temp->next-
         >next!=NULL)
         Set temp=temp->next->next
         Set product=product * (temp->data)
      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 alternate() 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;
   if(head == NULL) {
      head = newnode;
      head->next = NULL;
      } else {
      temp=head;
      while(temp->next!=NULL) {
         temp=temp->next;
      }
      newnode->next=NULL;
      temp->next=newnode;
   }
}
//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 alternate elements
void alternate() {
   int product;
   temp=head;
   product=head->data;
   while(temp->next!=NULL && temp!=NULL && temp->next->next!=NULL) {
      temp=temp->next->next;
      product=product * (temp->data);
   }
   printf("\nproduct of alternate nodes is %d : " ,product);
}
int main() {
   //creating list
   struct node* head = NULL;
   //inserting elements into a list
   insert(10);
   insert(20);
   insert(30);
   insert(40);
   insert(50);
   insert(60);
   //displaying the list
   printf("linked list is : ");
   display();

   //calling alternate function for finding product
   alternate();
   return 0;
}

আউটপুট

linked list is : 10 20 30 40 50 60
product of alternate nodes is : 15000

  1. C ভাষায় প্রদত্ত সূচীতে লিঙ্কযুক্ত তালিকার নোডগুলি মুদ্রণ করুন

  2. লিংকড লিস্টের বিকল্প নোড (পুনরাবৃত্ত পদ্ধতি) সি ভাষায় প্রিন্ট করুন

  3. C++ এ সার্কুলার লিঙ্কড লিস্টের নোডের সমষ্টি

  4. C++ এ লিঙ্ক করা তালিকার বিকল্প নোডের যোগফল