Types of Linked List - Singly linked, doubly linked and circularIn this tutorial, you will learn different types of linked list. Also, you will find implementation of linked list in C. Show Before you learn about the type of the linked list, make sure you know about the LinkedList Data Structure. There are three common types of Linked List.
Singly Linked ListIt is the most common. Each node has data and a pointer to the next node. Singly linked listNode is represented as: struct node { int data; struct node *next; }A three-member singly linked list can be created as: /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = NULL; /* Save address of first node in head */ head = one;Doubly Linked ListWe add a pointer to the previous node in a doubly-linked list. Thus, we can go in either direction: forward or backward. Doubly linked listA node is represented as struct node { int data; struct node *next; struct node *prev; }A three-member doubly linked list can be created as /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; one->prev = NULL; two->next = three; two->prev = one; three->next = NULL; three->prev = two; /* Save address of first node in head */ head = one;If you want to learn more about it, please visit doubly linked list and operations on it. Circular Linked ListA circular linked list is a variation of a linked list in which the last element is linked to the first element. This forms a circular loop. Circular linked listA circular linked list can be either singly linked or doubly linked.
A three-member circular singly linked list can be created as: /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = one; /* Save address of first node in head */ head = one;If you want to learn more about it, please visit circular linked list and operations on it. Table of Contents
|