_Lucifer_

Untitled

Nov 6th, 2025
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. Explanation
  2. This code implements a singly linked list in C++ and demonstrates inserting a node at the end:
  3.  
  4. Node Structure: Holds an integer data and a pointer next to the next node.
  5. insertAtEnd Function:
  6.  
  7. Creates a new node with the given data and sets its next to nullptr.
  8. If the list is empty (head is nullptr), sets the new node as the head.
  9. Otherwise, traverses from the head to the last node (where next is nullptr).
  10. Updates the last node's next to point to the new node.
  11.  
  12.  
  13. printList Function: Traverses and prints the list for verification.
  14. Main Function: Creates an empty list, inserts nodes (10, 20, 30), prints it, inserts 40 at the end, and prints again.
  15.  
  16. Output:
  17. textLinked List before insertion: 10 -> 20 -> 30 -> NULL
  18. Linked List after insertion: 10 -> 20 -> 30 -> 40 -> NULL
  19. This matches the steps in the image: traverse to the last node, update its next from NULL to the new node, and set the new node's next to NULL.
  20.  
  21.  
  22.  
  23. #include <iostream>
  24.  
  25. // Define the Node structure
  26. struct Node {
  27.     int data;      // Data stored in the node
  28.     Node* next;    // Pointer to the next node
  29. };
  30.  
  31. // Function to insert a new node at the end of the linked list
  32. void insertAtEnd(Node** head, int newData) {
  33.     // Create a new node
  34.     Node* newNode = new Node();
  35.     newNode->data = newData;
  36.     newNode->next = nullptr;  // Next pointer of new node is NULL (end of list)
  37.  
  38.     // If the list is empty, the new node becomes the head
  39.     if (*head == nullptr) {
  40.         *head = newNode;
  41.         return;
  42.     }
  43.  
  44.     // Traverse to the last node
  45.     Node* last = *head;
  46.     while (last->next != nullptr) {
  47.         last = last->next;
  48.     }
  49.  
  50.     // Change the next pointer of the last node to the new node
  51.     last->next = newNode;
  52. }
  53.  
  54. // Helper function to print the linked list
  55. void printList(Node* head) {
  56.     Node* temp = head;
  57.     while (temp != nullptr) {
  58.         std::cout << temp->data << " -> ";
  59.         temp = temp->next;
  60.     }
  61.     std::cout << "NULL" << std::endl;
  62. }
  63.  
  64. int main() {
  65.     Node* head = nullptr;  // Initialize empty list
  66.  
  67.     // Insert some nodes
  68.     insertAtEnd(&head, 10);
  69.     insertAtEnd(&head, 20);
  70.     insertAtEnd(&head, 30);
  71.  
  72.     // Print the list before inserting new node
  73.     std::cout << "Linked List before insertion: ";
  74.     printList(head);
  75.  
  76.     // Insert a new node at the end
  77.     insertAtEnd(&head, 40);
  78.  
  79.     // Print the list after insertion
  80.     std::cout << "Linked List after insertion: ";
  81.     printList(head);
  82.  
  83.     // Clean up memory (optional for this example, but good practice)
  84.     Node* current = head;
  85.     while (current != nullptr) {
  86.         Node* next = current->next;
  87.         delete current;
  88.         current = next;
  89.     }
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment