Can you make a linked list in C?

Introduction

In this article, I am going to discuss one of the most important Data Structures- Linked List.

I will be explaining about

  • Linked List
  • Advantage of Linked List
  • Types of Linked List
  • How to Create a Linked List
  • Various operations on Linked list.

What is a Linked List?

Linked List is a linear data structure which consists of a group of nodes in a sequence. Each node contains two parts.

  • Data Each node of a linked list can store a data.
  • Address Each node of a linked list contains an address to the next node, called "Next".

The first node of a Linked List is referenced by a pointer called Head

Advantages of Linked List

  • They are dynamic in nature and allocate memory as and when required.
  • Insertion and deletion is easy to implement.
  • Other data structures such as Stack and Queue can also be implemented easily using Linked List.
  • It has faster access time and can be expanded in constant time without memory overhead.
  • Since there is no need to define an initial size for a linked list, hence memory utilization is effective.
  • Backtracking is possible in doubly linked lists.

Types of Linked List

  • Singly Linked List: Singly linked lists contain nodes which have a data part and an address part, i.e., Next, which points to the next node in the sequenceof nodes. The next pointer of the last node will point to null.

  • Doubly Linked List: In a doubly linked list, each node contains two links - thefirst link points to the previous node and the next link points to the next node in the sequence.Theprev pointer of the first node and next pointer of the last node will point to null.
  • Circular Linked List: In the circular linked list, the next of the last node will point tothe first node, thus forming a circular chain.

  • Doubly Circular Linked List: In this type of linked list, the next of the last node will point to the first node and the previous pointer of the first node will point to the last node.

Creating a Linked List


The node of a singly linked list contains a data part and a link part. The link will contain the address of next node and is initialized to null.So, we will create class definition of node for singly linked list as follows -
  1. internalclassNode{
  2. internalintdata;
  3. internalNodenext;
  4. publicNode[intd]{
  5. data=d;
  6. next=null;
  7. }
  8. }

The node for a Doubly Linked list will contain one data part and two link parts - previous link and next link.Hence, we create a class definition of a node for the doubly linked list as shown below.

  1. internalclassDNode{
  2. internalintdata;
  3. internalDNodeprev;
  4. internalDNodenext;
  5. publicDNode[intd]{
  6. data=d;
  7. prev=null;
  8. next=null;
  9. }
  10. }

Now, our node has been created, so, we will create a linked list class now.When a new Linked List is instantiated, it just has the head, which is Null.The SinglyLinkedList class will contain nodes of type Node class. Hence, SinglyLinkedList class definition will look like below.

  1. internalclassSingleLinkedList{
  2. internalNodehead;
  3. }

The DoublyLinkedList class will contain nodes of type DNode class. Hence, DoublyLinkedList class will look like this.

  1. internalclassDoubleLinkedList{
  2. internalDNodehead;
  3. }

Various operations on Linked list


Insert data at front of the Linked List
  • The first node, head, will be null when the linked list is instantiated. When we want to add any node at the front, we want the head to point to it.
  • We will create a new node. The next of the new node will point to the head of the Linked list.
  • The previous Head node is now the second node of Linked List because the new node is added at the front. So, we will assign head to the new node.
  1. internalvoidInsertFront[SingleLinkedListsinglyList,intnew_data]{
  2. Nodenew_node=newNode[new_data];
  3. new_node.next=singlyList.head;
  4. singlyList.head=new_node;
  5. }
To insert the data at front of the doubly linked list, we have to follow one extra step .i.e point the previous pointer of head node to the new node. So, the method will look like this.
  1. internalvoidInsertFront[DoubleLinkedListdoubleLinkedList,intdata]{
  2. DNodenewNode=newDNode[data];
  3. newNode.next=doubleLinkedList.head;
  4. newNode.prev=null;
  5. if[doubleLinkedList.head!=null]{
  6. doubleLinkedList.head.prev=newNode;
  7. }
  8. doubleLinkedList.head=newNode;
  9. }

Insert data at the end of Linked List

Video liên quan

Chủ Đề