Generic Linked List

Generic linked list is combination of nodes element which consists different objects. This node connect to each other but value of node are different type. This type of linked list is called generic linked list. Let see an example.

generic-linked-list

In above example is consist primitive data values. Similar way generic linked list capable to work with custom data values. Here given code implementation process.

// Java Program For
// Generic Linked List
class LinkNode < T >
{
    T data;
    LinkNode next;
    public LinkNode(T data)
    {
        this.data = data;
        this.next = null;
    }
}
public class LinkedList
{
    public LinkNode head;
    public LinkNode tail;
    //Class constructors
    LinkedList()
    {
        this.head = null;
        this.tail = null;
    }
    // Insert element of linked list 
    // Which is capable to contain generic values
    public < T > void insert(T value)
    {
        // Create a node
        LinkNode < T > node = new LinkNode < T > (value);
        if (this.head == null)
        {
            // Add first node
            this.head = node;
            this.tail = node;
        }
        else
        {
            // Add node at the last 
            this.tail.next = node;

            // New Last node
            this.tail = node;
        }
    }
    // Display all Linked List elements
    public void display()
    {
        if (this.head != null)
        {
            LinkNode temp = this.head;
            while (temp != null)
            {
                // Display node value
                System.out.print(temp.data);
                System.out.print(" → ");
                // Visit to next node
                temp = temp.next;
            }
            System.out.println("NULL");
        }
        else
        {
            System.out.println("Empty Linked list");
        }
    }
    public static void main(String[] args)
    {
        LinkedList sll = new LinkedList();
        // Add node values
        sll.insert(10);
        sll.insert(4);
        sll.insert("Code");
        sll.insert(42.05);
        sll.insert('c');
        sll.insert(true);
        // Display linked list
        sll.display();
    }
}

Output

10 → 4 → Code → 42.05 → c → true → NULL
// C# Program
// Generic Linked List
using System;
// node class
public class Node < T >
{
    public object data;
    public Node < T > next;
}
class Program < T >
{
    Node < T > head;
    public Program()
    {
        head = null;
    }
    //insert node of linked list
    public void insert(object data)
    {
        Node < T > newNode = new Node < T > ();
        newNode.data = data;
        newNode.next = null;
        if (head == null) head = newNode;
        else
        {
            Node < T > temp = head;
            //get last node
            while (temp.next != null)
            {
                temp = temp.next;
            }
            //add new node
            temp.next = newNode;
        }
    }
    //display linked list nodes value
    public void display()
    {
        if (head == null)
        {
            Console.Write("Empty List");
        }
        else
        {
            Node < T > temp = head;
            while (temp != null)
            {
                Console.Write("  {0}", temp.data);
                temp = temp.next;
            }
        }
    }
}
class Operation
{
    // Main method 
    static void Main(string[] args)
    {
        Program < Object > obj = new Program < Object > ();
        //Insert Generic data on linked list
        obj.insert(10);
        obj.insert(4);
        obj.insert("code");
        obj.insert(42.05F);
        obj.insert('c');
        obj.insert(true);
        obj.display();
    }
}

Output

  10  4  code  42.05  c  True
// Generic linked list example in c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Node
{
    void *data;
    struct Node *next;
    char type;
};
void insertNode(struct Node **head, void *data, int size, char type)
{
    struct Node *newNode = malloc(sizeof(struct Node)); //make a node
    newNode->data = malloc(size);
    newNode->next = NULL;
    newNode->type = type;
    memcpy(newNode->data, data, size);
    if ( *head == NULL)
    {
        *head = newNode;
    }
    else
    {
        struct Node *temp = *head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}
void display(struct Node *temp)
{
    if (temp != NULL)
    {
        while (temp != NULL)
        {
            //Selected data type value
            switch (temp->type)
            {
                case 'i':
                    //Display ineteger value
                    printf("%d ", *(int *) temp->data);
                    break;
                case 's':
                    //Display string 
                    printf("%s ", *(char **) temp->data);
                    break;
                case 'c':
                    //Display char data value 
                    printf("%c ", *(char *) temp->data);
                    break;
                case 'd':
                    //Display double data type value
                    printf("%lf ", *(double *) temp->data);
                    break;
                case 'f':
                    //Display float data type value
                    printf("%f ", *(float *) temp->data);
                    break;
                    //Add new data type here 
                default:
                    //Missing 
                    printf(" ");
                    break;
            }
            temp = temp->next;
        }
    }
}
int main()
{
    struct Node *head = NULL;
    int intData = 0;
    char charData = ' ';
    char *textData = NULL;
    float floatData = 0;
    double doubleData = 0;
    intData = 1;
    insertNode( &head, &intData, sizeof(int), 'i');
    textData = "C++";
    insertNode( &head, &textData, sizeof(char *), 's');
    charData = 'Z';
    insertNode( &head, &charData, sizeof(char *), 'c');
    floatData = 10.5f;
    insertNode( &head, &floatData, sizeof(float), 'f');
    textData = "Java";
    insertNode( &head, &textData, sizeof(char *), 's');
    doubleData = 70.35;
    insertNode( &head, &doubleData, sizeof(double), 'd');
    display(head);
    return 0;
}

Output

1 C++ Z 10.500000 Java 70.350000
// C++ Program 
// Experimental
// This code is not create pure generic linked list
#include<iostream>

using namespace std;
template < class T >
    //create structure
    class Node
    {
        public: T data;
        Node *next;
    };
template < class T > class LinkedList
{
    public: Node < T > *head; // head node
    LinkedList < T > ()
    {
        head = NULL;
    }
    // Insert node at end of linked list
    void insert(T value)
    {
        // Create dynamic node
        Node < T > *node = new Node < T > ;
        if (node == NULL)
        {
            cout << "Memory overflow\n";
        }
        else
        {
            node->data = value;
            node->next = NULL;
            if (head == NULL)
            {
                // Base condition
                head = node;
            }
            else
            {
                Node < T > *temp = head;
                while (temp->next != NULL)
                {
                    temp = temp->next;
                }
                // Add newly node at last
                temp->next = node;
            }
        }
    }
    // Display all node value in linked list
    void display()
    {
        if (head == NULL)
        {
            cout << "Empty linked list";
        }
        else
        {
            Node < T > *temp = head;
            cout << "\nLinked List : ";
            while (temp != NULL)
            {
                // Print node value
                cout << temp->data << " ";
                temp = temp->next;
            }
        }
    }
};
int main()
{
    //create object
    LinkedList < int > intData;
    LinkedList < float > floatData;
    LinkedList < char > charData;
    //insert element of linked list
    intData.insert(10);
    intData.insert(20);
    floatData.insert(30.42f);
    floatData.insert(40.23f);
    floatData.insert(50.1f);
    charData.insert('A');
    charData.insert('B');
    //display all node
    intData.display();
    floatData.display();
    charData.display();
    /*When we combine two linked list, There is 
    type casting problem Node<int>*are not reliable to Node<float>*
    //intData.head->next->next=(Node<int>*)floatData.head;

    //intData.display();
    */
    return 0;
}

Output

Linked List : 10 20
Linked List : 30.42 40.23 50.1
Linked List : A B

Note that in C++ class templates are work on generic value. In above code are Experimental. Because this code are creating a three different type of linked list (integer,float,character).

C++ Experimental Generic Linked List

In this case we are try to connect two different linked list but there are some problems. First is when connect two different linked list there are type cast are required.

In case we are connect two different linked list (first linked list last node are connecte by second linked list first node) by using type casting. In this case when print this connected node value then second linked list are not print valid result. See this example.

intData.head->next->next=(Node<int>*) floatData.head; // (Node<int>*)  typecast
//when connecting of two different type of linked list
intData.display(); 

Output
Linked List : 10 20 1106467881 1109453701 1112041062

But print floatData linked list values.

floatData.display();
//Output
Linked List : 30.42 40.23 50.1

Only problem is occured in typecasting to print node.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved