Skip to main content
Engineering LibreTexts

7.7: Create Linked List

  • Page ID
    34677
  • First Simple Linked List in C Let us create a simple linked list with 3 nodes.

    This code is covered in a video that follows this page...

    // A simple CPP program to introduce
    // a linked list
    #include<bits/stdc++.h>
    using namespace std;
    
    class Node {
       public:
       char data;
       Node* next;
    };
    
    // Program to create a simple linked
    // list with 3 nodes
    int main()
    {
       Node* head = NULL;
       Node* second = NULL;
       Node* third = NULL;
    
       // allocate 3 nodes in the heap
       head = new Node();
       second = new Node(); 
       third = new Node();
       
       graphical representation of a 3 node linked lilst
    
       // the first node
       head->data = 'A'; // assign data in first node
       head->next = second; // Link first node with
    
       // the second node
       // assign data to second node
       second->data = 'B';
       // Link second node with the third node 
       second->next = third;
    
       third->data = 'C'; // assign data to third node
       third->next = NULL;
    
       return 0;
    }

    "Linked List | Set 1 (Introduction)" by ashwani khemani is licensed under CC BY-SA 4.0