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

লিংকড লিস্টের দৈর্ঘ্য বের করার C প্রোগ্রাম


লিংকড লিস্ট ডাইনামিক মেমরি অ্যালোকেশন ব্যবহার করে অর্থাৎ সেগুলো সেই অনুযায়ী বড় ও ছোট হয়। এগুলো নোডের একটি সংগ্রহ হিসেবে সংজ্ঞায়িত হয়। এখানে, নোডের দুটি অংশ রয়েছে, যেগুলো হলো ডেটা এবং লিংক। ডেটা, লিংক এবং লিংকড লিস্টের রিপ্রেজেন্টেশন নিচে দেওয়া হলো:

লিংকড লিস্টের দৈর্ঘ্য বের করার C প্রোগ্রাম

লিংকড লিস্টের প্রকার

লিংকড লিস্টের চারটি প্রকার রয়েছে, যেগুলো হলো:

  • সিঙ্গলি লিংকড লিস্ট
  • ডাবলি লিংকড লিস্ট
  • সার্কুলার সিঙ্গল লিংকড লিস্ট
  • সার্কুলার ডাবল লিংকড লিস্ট

রিকার্শন মেথড ব্যবহার করে লিংকড লিস্টের দৈর্ঘ্য বের করার লজিক নিম্নরূপ:

int length(node *temp){    if(temp==NULL)       return l;    else{       l=l+1;       length(temp->next);    } }

প্রোগ্রাম

লিংকড লিস্টের দৈর্ঘ্য বের করার C প্রোগ্রাম:

#include <stdio.h> #include <stdlib.h> typedef struct linklist{    int data;    struct linklist *next; }node; int l=0; int main(){    node *head=NULL,*temp,*temp1;    int len,choice,count=0,key;    do{       temp=(node *)malloc(sizeof(node));       if(temp!=NULL){          printf("\nenter the elements in a list : ");          scanf("%d",&temp->data);          temp->next=NULL;          if(head==NULL){             head=temp;           }else{             temp1=head;             while(temp1->next!=NULL){                temp1=temp1->next;             }             temp1->next=temp;           }       }else{          printf("\nMemory is full");       }       printf("\npress 1 to enter data into list: ");       scanf("%d",&choice);    }while(choice==1);    len=length(head);    printf("The list has %d no of nodes",l);    return 0; } //recursive function to find length int length(node *temp){    if(temp==NULL)       return l;    else{       l=l+1;       length(temp->next);    } }

আউটপুট

উপরোক্ত প্রোগ্রামটি এক্সিকিউট করলে নিচের ফলাফলটি দেখায়:

Run 1: enter the elements in a list: 3 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 0 The list has 3 no of nodes Run 2: enter the elements in a list: 12 press 1 to enter data into list: 1 enter the elements in a list: 45 press 1 to enter data into list: 0 The list has 2 no of nodes

  1. সি প্রোগ্রামিং-এ লিংকড লিস্টের শেষ থেকে n-তম নোড বের করা

  2. C++-এ লিংকড লিস্টে লুপের দৈর্ঘ্য বের করুন

  3. C++-এ ডাবলি লিংকড লিস্টের আকার (Size) বের করার প্রোগ্রাম

  4. পাইথন প্রোগ্রাম লিস্টে সবচেয়ে ছোট সংখ্যা বের করা