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

C++-এ সিংলি লিঙ্কড লিস্টের প্রথম নোড ডিলিট করা

একটি সিংলি লিঙ্কড লিস্ট দেওয়া থাকলে, তার প্রথম নোড (হেড) ডিলিট করতে হবে।

ইনপুট ১

4 → 3 → 2 → 1

আউটপুট

3 → 2 → 1 →

ইনপুট ২

1 → 2 → 3 →

আউটপুট

2 → 3 →

সমাধানের পদ্ধতি

  • deleteAtFirst(Node* &head) ফাংশন হেডের রেফারেন্স নেবে।
  • যদি হেড NULL হয়, রিটার্ন করুন।
  • temp = head; head = head->next; delete temp; করুন।
  • লিস্ট প্রিন্ট করুন।

উদাহরণ

#include <iostream>
using namespace std;

class Node {
public:
    int data; Node* next;
    Node(int d) { data = d; next = NULL; }
};

void insertAtFirst(Node* &head, int data) {
    Node* n = new Node(data);
    n->next = head; head = n;
}

void print(Node* head) {
    while (head != NULL) {
        cout << head->data << "→";
        head = head->next;
    }
    cout << endl;
}

void deleteAtFirst(Node* &head) {
    if (head == NULL) return;
    Node* temp = head;
    head = head->next;
    delete temp;
}

int main() {
    Node* head = NULL;
    insertAtFirst(head, 1); insertAtFirst(head, 2);
    insertAtFirst(head, 3); insertAtFirst(head, 4);
    deleteAtFirst(head);
    print(head);
    return 0;
}

আউটপুট

3 → 2 → 1 →
  1. C++-এ ডাবলি লিংকড লিস্টে নির্দিষ্ট পজিশনে একটি নোড মুছে ফেলা

  2. সি++ প্রোগ্রাম: লিংকড লিস্টের প্রথম K নোডের গুণফল নির্ণয়

  3. সার্কুলার সিংলি লিঙ্কড লিস্ট বাস্তবায়নের C++ প্রোগ্রাম

  4. সিংলি লিঙ্কড লিস্ট বাস্তবায়নের C++ প্রোগ্রাম