৭-৬ঃ Take Singly Linked List Input Recap
এবার আমরা লিঙ্কড লিস্টে ইনপুট নেওয়া রিকেপ করব।
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int val;
Node *next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};
void insert_tail(Node *&head, Node *&tail, int val)
{
Node *newNode = new Node(val); // টেল ট্র্যাক রেখে ইনসার্ট করা হচ্ছে, যার কমপ্লেক্সিটি O(1)
if (head == NULL)
{
head = newNode;
tail = newNode;
return;
}
tail->next = newNode;
tail = newNode;
}
void print_linekd_list(Node *head)
{
Node *tmp = head;
while (tmp != NULL)
{
cout << tmp->val << " ";
tmp = tmp->next;
}
cout << endl;
}
int main()
{
Node *head = NULL;
Node *tail = NULL;
int val;
while (true)
{
cin >> val; // ভেলু ইনপুট নিচ্ছি।
if (val == -1) // ভেলুটি -1 হলে ব্রেক করে দিচ্ছি।
break;
insert_tail(head, tail, val); // ইনসার্ট টেল ফাংশনকে কল করে টেলে ইনসার্ট করা হচ্ছে।
}
print_linekd_list(head);
return 0;
}Previous৭-৫ঃ Delete Head from Singly Linked List RecapNext৭-৭ঃ Printing Singly Linked List in Reverse
Last updated