Linked List কি?
Linked List হল একটি Linear Data Structure যেটা কতগুলো Node এর সমন্বয়ে গঠিত যার প্রত্যেকটা Element এক একটা অবজেক্ট হিসেবে কাজ করে।
Linked List (লিঙ্কড লিস্ট) এর প্রত্যেকটা এলিমেন্ট এক একটা Node হিসেবে কাজ করে। প্রত্যেকটা Node আবার দুইটা অংশ নিয়ে গঠিত।
- Data (ডাটা)
- Reference (রেফারেন্স)
কেন আমরা Array (এর্যে) ব্যবহার না করে Linked List ব্যবহার করব?
- Linked List হল একটি Dynamic Data Structure. যার মানে হল, আমাদের Linked list এর শুরুতে সাইজ ডিক্লিয়ার করার দরকার নেই। যত সাইজ লাগবে, সে তার প্রয়োজন মত নিয়ে নেবে।
- যেহেতু সাইজ প্রয়োজন মত নিয়ে নেবে, তাই এটি মেমুরিতে কোনো অপ্রয়োজনীয় জায়গা নিবেনা।
- যেসকল প্রোগ্রাম Unknown ডাটা নিয়ে কাজ করে ,সেসকল ক্ষেত্রে Linked list খুবই কার্যকরী।
- যেকোনো Node আমরা চাইলে ডিলেট করে দিতে পারি, কিন্তু Array’র ক্ষেত্রে আমরা চাইলেই যেকোনো জায়গা থেকে Array’র ভ্যালু ডিলেট করতে পারিনা, তাতে সবগুলো index পরিবর্তন করতে হয়।
Linked List (লিঙ্কড লিস্ট এর সমস্যা) Problems:
- Array তে যেমন আমরা যেকোনো element সরাসরি access করতে পারি, কিন্তু Linked list এর ক্ষেত্রে আমরা সরাসরি কোনো node access করতে পারিনা। এজন্য আমাদের
Linked List Initialize করাঃ
typedef struct Node { int data; struct Node *next; }node;
এখানে, Node হল একটি struct ডাটা টাইপের অবজেক্ট।
এর ভিতরে data হল ডাটা, আর *next হল reference.
Linked List ডাটা ইনসার্ট করাঃ
void insert(node *pointer, int data) { while(pointer->next!=NULL) { pointer = pointer -> next; } pointer->next = (node *)malloc(sizeof(node)); pointer = pointer->next; pointer->data = data; pointer->next = NULL; }
Linked List ডাটা সার্চ করাঃ
//search method that search if the given data is in the linked list or not int find(node *pointer, int key) { pointer = pointer -> next; while(pointer != NULL) { if(pointer->data == key) //key is found. { return 1; } pointer = pointer -> next;//Search in the next node. } /*Key is not found */ return 0; }
Linked List সব ডাটা প্রিন্ট করাঃ
void print(node *pointer) { if(pointer==NULL) { return; } printf("%d ",pointer->data); print(pointer->next); //using recursion }
Linked List Node ডিলেট করাঃ
void delete(node *pointer, int data) { while(pointer->next!=NULL && (pointer->next)->data != data) { pointer = pointer -> next; } if(pointer->next==NULL) { printf("Element %d is not present in the list\n",data); return; } node *temp; temp = pointer -> next; pointer->next = temp->next; free(temp); //this is the main thing to delete linked list any node by--> free() return; }
সম্পূর্ণ সোর্স কোডঃ
/** LinkedList Complete Code of insert, delete, search, get total item @author: Maniruzzaman Akash **/ #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; }node; void insert(node *pointer, int data) { while(pointer->next!=NULL) { pointer = pointer -> next; } pointer->next = (node *)malloc(sizeof(node)); pointer = pointer->next; pointer->data = data; pointer->next = NULL; } //search method that search if the given data is in the linked list or not int find(node *pointer, int key) { pointer = pointer -> next; while(pointer != NULL) { if(pointer->data == key) //key is found. { return 1; } pointer = pointer -> next;//Search in the next node. } /*Key is not found */ return 0; } void delete(node *pointer, int data) { while(pointer->next!=NULL && (pointer->next)->data != data) { pointer = pointer -> next; } if(pointer->next==NULL) { printf("Element %d is not present in the list\n",data); return; } node *temp; temp = pointer -> next; pointer->next = temp->next; free(temp); //this is the main thing to delete linked list any node by--> free() return; } void print(node *pointer) { if(pointer==NULL) { return; } printf("%d ",pointer->data); print(pointer->next); //using recursion } int main() { node *start,*temp; start = (node *)malloc(sizeof(node)); temp = start; temp -> next = NULL; printf("\t\tLinked List\n-------------------------------------\n"); printf(" Press 1 to Insert Data in the linked list\n Press 2 Delete data from the linked list\n Press 3 Print data of the linked list\n Press 4 Find data from the linked data from the linked list\n"); printf(" Press 0 To exit the program\n-------------------------------------\n"); while(1) { int option; printf("Enter your option: "); scanf("%d",&option); if(option==1) { int data; printf("Enter a value in the linked list : "); scanf("%d",&data); insert(start,data); printf("Value inserted successfully\n"); } else if(option==2) { int data; printf("Enter a data do you want to delete from the linked list: "); scanf("%d",&data); delete(start,data); } else if(option==3) { printf("The list is "); print(start->next); printf("\n"); } else if(option==4) { int data; scanf("%d",&data); int status = find(start,data); if(status) { printf("Element Found\n"); } else { printf("Element Not Found\n"); } }else if(option == 0){ printf("!!!..Program exited successfully..!!!"); return 0; }else{ printf("!!!..Wrong option.. chose a valid option..!!!"); } } return 0; }
First off I want to say awesome blog! I
had a quick question that I’d like to ask if you don’t mind.
I was interested to find out how you center yourself and clear your head before writing.
I have had trouble clearing my mind in getting my thoughts out there.
I do enjoy writing but it just seems like the
first 10 to 15 minutes are usually wasted simply just trying to figure out how to begin. Any suggestions or
hints? Kudos!
This is one within the keys to bolster your online presence.
Page accessories can include paw prints, outdoor backgrounds and earthy shades and textures.
Have you locate these ads on the online world before? http://casino1391.com/best-online-casino-games-platforms-to-download-right-away/
This is one within the keys to bolster your online
presence. Page accessories can include paw prints, outdoor backgrounds and
earthy shades and textures. Have you locate these ads on the online world before? http://casino1391.com/best-online-casino-games-platforms-to-download-right-away/
Hi, I do think this is a great website. I stumbledupon it ;
) I’m going to come back yet again since i have book-marked it.
Money and freedom is the greatest way to change, may you be rich and continue to help others. https://www.datafilehost.com/d/bdb8fc3c
Hi, I do think this is a great website. I stumbledupon it 😉 I’m going to come back yet again since i have
book-marked it. Money and freedom is the greatest way to change, may
you be rich and continue to help others. https://www.datafilehost.com/d/bdb8fc3c