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

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


n সংখ্যক নোড দেওয়া থাকলে, লিংকড লিস্টের শেষ থেকে n-তম নোড প্রিন্ট করতে হবে। প্রোগ্রামটি নোডের ক্রম বদলে না, শুধু শেষ থেকে n-তম নোডটি প্রিন্ট করবে।

উদাহরণ

Input: 10 20 30 40 50 60, N = 3
Output: 40

উপরের উদাহরণে, প্রথম নোড থেকে (count - n) নোড ট্রাভার্স করা হয়েছে, অর্থাৎ 10, 20, 30, 40 — সুতরাং শেষ থেকে ৩-য় নোড হলো ৪০।

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

পুরো লিস্ট ট্রাভার্স না করে এই দক্ষ পদ্ধতিটি অনুসরণ করা যায়:

  • একটি টেম্পোরারি পয়েন্টার (temp) নিন, যা নোড টাইপের হবে
  • এই temp পয়েন্টারকে হেড পয়েন্টার দ্বারা নির্দেশিত প্রথম নোডে সেট করুন
  • কাউন্টারকে লিস্টের নোড সংখ্যায় সেট করুন
  • temp কে temp → next এ সরিয়ে যান (count - n) বার
  • temp → data ডিসপ্লে করুন

এই পদ্ধতিতে যদি কাউন্ট ৫ হয়, তবে প্রোগ্রাম লুপটি (5-3) অর্থাৎ ২ বার ইটারেট করবে। ১০ (০-এ INDX), ২০ (১-ST INDX), ৩০ (২-ND INDX) — যা ফলাফল। এই পদ্ধতিতে পুরো লিস্ট ট্রাভার্স করতে হয় না, যা স্পেস এবং মেমরি বাঁচায়।

অ্যালগরিদম

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 nth node from last
        void last(int n) {
            temp = head;
            for(i = 0; i < count - n; i++)
                temp = temp->next;
            Print temp->data
        }
Step 5: In main()
        Create nodes, insert values, display list, call last(n)
Stop

সি প্রোগ্রাম

#include <stdio.h>
#include <stdlib.h>

// Structure of a node
struct node {
    int data;
    struct node *next;
} *head, *temp;

int count = 0;

// 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;
        count++;
    } else {
        temp->next = newnode;
        temp = temp->next;
        count++;
    }
}

// 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 nth node from the last of a linked list
void last(int n) {
    int i;
    temp = head;
    for (i = 0; i < count - n; i++) {
        temp = temp->next;
    }
    printf("\n%drd node from the end of linked list is: %d", n, temp->data);
}

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

আউটপুট

linked list is: 1 2 3 4 5 6
3rd node from the end of linked list is: 4

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

  2. C ভাষায় লিংকড লিস্টের ধারণা ব্যাখ্যা

  3. C প্রোগ্রামিং-এ এন্ডে পৌঁছানোর জন্য মিনিমাম জাম্প (Minimum Number of Jumps to Reach End)

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