लिंक्ड सूची संचालन: ट्रैवर्स, इंसर्ट और डिलीट

इस ट्यूटोरियल में, आप एक लिंक की गई सूची पर अलग-अलग ऑपरेशन सीखेंगे। इसके अलावा, आप सी / सी ++, पायथन और जावा में लिंक किए गए सूची संचालन का कार्यान्वयन पाएंगे।

अब जब आपको लिंक की गई सूची और उनके प्रकारों के पीछे की मूल अवधारणाओं की समझ मिल गई है, तो यह सामान्य परिचालन में गोता लगाने का समय है जिसे निष्पादित किया जा सकता है।

याद रखने के लिए दो महत्वपूर्ण बिंदु:

  • लिंक की गई सूची के पहले नोड के प्रमुख बिंदु
  • अंतिम नोड का अगला पॉइंटर NULL है, इसलिए यदि अगला वर्तमान नोड NULL है, तो हम लिंक की गई सूची के अंत में पहुंच गए हैं।

सभी उदाहरणों में, हम मानेंगे कि लिंक की गई सूची में 1 --->2 --->3नोड संरचना के साथ तीन नोड हैं:

 struct node ( int data; struct node *next; );

लिंक की गई लिस्ट को कैसे ट्रेस करें

लिंक की गई सूची की सामग्री को प्रदर्शित करना बहुत सरल है। हम अस्थायी नोड को अगले एक पर ले जाते हैं और इसकी सामग्री प्रदर्शित करते हैं।

जब अस्थायी खाली है, तो हम जानते हैं कि हम लिंक की गई सूची के अंत तक पहुँच चुके हैं, इसलिए हम लूप से बाहर निकलते हैं।

 struct node *temp = head; printf("List elements are - "); while(temp != NULL) ( printf("%d --->",temp->data); temp = temp->next; )

इस कार्यक्रम का आउटपुट होगा:

 सूची तत्व हैं - 1 ---> 2 ---> 3 --->

लिंक्ड लिस्ट में एलिमेंट्स कैसे जोड़ें

आप लिंक की गई सूची के आरंभ, मध्य या अंत में तत्व जोड़ सकते हैं।

शुरुआत में जोड़ें

  • नए नोड के लिए मेमोरी आवंटित करें
  • डेटा की दुकान
  • सिर पर इंगित करने के लिए नए नोड के बगल में बदलें
  • हाल ही में बनाए गए नोड को इंगित करने के लिए सिर बदलें
 struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; newNode->next = head; head = newNode;

अंत में जोड़ें

  • नए नोड के लिए मेमोरी आवंटित करें
  • डेटा की दुकान
  • पिछले नोड के लिए पार
  • अंतिम नोड के बगल में हाल ही में बनाए गए नोड में बदलें
 struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; newNode->next = NULL; struct node *temp = head; while(temp->next != NULL)( temp = temp->next; ) temp->next = newNode;

मध्य में जोड़ें

  • नई नोड के लिए मेमोरी और स्टोर डेटा आवंटित करें
  • नए नोड की आवश्यक स्थिति से ठीक पहले नोड को पार करना
  • अगले नोड को बीच में नया नोड शामिल करने के लिए बदलें
 struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp = head; for(int i=2; i next != NULL) ( temp = temp->next; ) ) newNode->next = temp->next; temp->next = newNode;

लिंक्ड लिस्ट से डिलीट कैसे करें

आप शुरुआत, अंत या किसी विशेष स्थिति से हटा सकते हैं।

शुरुआत से हटाएं

  • दूसरी नोड के लिए बिंदु सिर
 head = head->next;

अंत से हटाएं

  • दूसरे अंतिम तत्व के लिए पार
  • इसके अगले पॉइंटर को नल में बदलें
 struct node* temp = head; while(temp->next->next!=NULL)( temp = temp->next; ) temp->next = NULL;

बीच से हटाओ

  • हटाए जाने वाले तत्व से पहले तत्व का पारगमन
  • चेन से नोड को बाहर करने के लिए अगले पॉइंटर्स बदलें
 for(int i=2; inext!=NULL) ( temp = temp->next; ) ) temp->next = temp->next->next;

पायथन, जावा, सी और सी ++ में लिंक्डलिस्ट संचालन को लागू करना

पायथन जावा सी सी ++
 # Linked list operations in Python # Create a node class Node: def __init__(self, item): self.item = item self.next = None class LinkedList: def __init__(self): self.head = None # Insert at the beginning def insertAtBeginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node # Insert after a node def insertAfter(self, node, data): if node is None: print("The given previous node must inLinkedList.") return new_node = Node(data) new_node.next = node.next node.next = new_node # Insert at the end def insertAtEnd(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last = self.head while (last.next): last = last.next last.next = new_node # Deleting a node def deleteNode(self, position): if self.head == None: return temp_node = self.head if position == 0: self.head = temp_node.next temp_node = None return # Find the key to be deleted for i in range(position - 1): temp_node = temp_node.next if temp_node is None: break # If the key is not present if temp_node is None: return if temp_node.next is None: return next = temp_node.next.next temp_node.next = None temp_node.next = next def printList(self): temp_node = self.head while (temp_node): print(str(temp_node.item) + " ", end="") temp_node = temp_node.next if __name__ == '__main__': llist = LinkedList() llist.insertAtEnd(1) llist.insertAtBeginning(2) llist.insertAtBeginning(3) llist.insertAtEnd(4) llist.insertAfter(llist.head.next, 5) print('Linked list:') llist.printList() print("After deleting an element:") llist.deleteNode(3) llist.printList()
 // Linked list operations in Java class LinkedList ( Node head; // Create a node class Node ( int item; Node next; Node(int d) ( item = d; next = null; ) ) public void insertAtBeginning(int data) ( // insert the item Node new_node = new Node(data); new_node.next = head; head = new_node; ) public void insertAfter(Node prev_node, int data) ( if (prev_node == null) ( System.out.println("The given previous node cannot be null"); return; ) Node new_node = new Node(data); new_node.next = prev_node.next; prev_node.next = new_node; ) public void insertAtEnd(int data) ( Node new_node = new Node(data); if (head == null) ( head = new Node(data); return; ) new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; ) void deleteNode(int position) ( if (head == null) return; Node node = head; if (position == 0) ( head = node.next; return; ) // Find the key to be deleted for (int i = 0; node != null && i < position - 1; i++) node = node.next; // If the key is not present if (node == null || node.next == null) return; // Remove the node Node next = node.next.next; node.next = next; ) public void printList() ( Node node = head; while (node != null) ( System.out.print(node.item + " "); node = node.next; ) ) public static void main(String() args) ( LinkedList llist = new LinkedList(); llist.insertAtEnd(1); llist.insertAtBeginning(2); llist.insertAtBeginning(3); llist.insertAtEnd(4); llist.insertAfter(llist.head.next, 5); System.out.println("Linked list: "); llist.printList(); System.out.println("After deleting an element: "); llist.deleteNode(3); llist.printList(); ) )
 // Linked list operations in C #include #include // Create a node struct Node ( int item; struct Node* next; ); void insertAtBeginning(struct Node** ref, int data) ( // Allocate memory to a node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // insert the item new_node->item = data; new_node->next = (*ref); // Move head to new node (*ref) = new_node; ) // Insert a node after a node void insertAfter(struct Node* node, int data) ( if (node == NULL) ( printf("the given previous node cannot be NULL"); return; ) struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->item = data; new_node->next = node->next; node->next = new_node; ) void insertAtEnd(struct Node** ref, int data) ( struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = *ref; new_node->item = data; new_node->next = NULL; if (*ref == NULL) ( *ref = new_node; return; ) while (last->next != NULL) last = last->next; last->next = new_node; return; ) void deleteNode(struct Node** ref, int key) ( struct Node *temp = *ref, *prev; if (temp != NULL && temp->item == key) ( *ref = temp->next; free(temp); return; ) // Find the key to be deleted while (temp != NULL && temp->item != key) ( prev = temp; temp = temp->next; ) // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; free(temp); ) // Print the linked list void printList(struct Node* node) ( while (node != NULL) ( printf(" %d ", node->item); node = node->next; ) ) // Driver program int main() ( struct Node* head = NULL; insertAtEnd(&head, 1); insertAtBeginning(&head, 2); insertAtBeginning(&head, 3); insertAtEnd(&head, 4); insertAfter(head->next, 5); printf("Linked list: "); printList(head); printf("After deleting an element: "); deleteNode(&head, 3); printList(head); ) 
 // Linked list operations in C++ #include #include using namespace std; // Create a node struct Node ( int item; struct Node* next; ); void insertAtBeginning(struct Node** ref, int data) ( // Allocate memory to a node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // insert the item new_node->item = data; new_node->next = (*ref); // Move head to new node (*ref) = new_node; ) // Insert a node after a node void insertAfter(struct Node* prev_node, int data) ( if (prev_node == NULL) ( cout  next = prev_node->next; prev_node->next = new_node; ) void insertAtEnd(struct Node** ref, int data) ( struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = *ref; new_node->item = data; new_node->next = NULL; if (*ref == NULL) ( *ref = new_node; return; ) while (last->next != NULL) last = last->next; last->next = new_node; return; ) void deleteNode(struct Node** ref, int key) ( struct Node *temp = *ref, *prev; if (temp != NULL && temp->item == key) ( *ref = temp->next; free(temp); return; ) // Find the key to be deleted while (temp != NULL && temp->item != key) ( prev = temp; temp = temp->next; ) // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; free(temp); ) // Print the linked list void printList(struct Node* node) ( while (node != NULL) ( cout  next, 5); cout << "Linked list: "; printList(head); cout << "After deleting an element: "; deleteNode(&head, 3); printList(head); )  

दिलचस्प लेख...