3

An Introduction to Linked List

 2 years ago
source link: https://dev.to/tastaslim/an-introduction-to-linked-list-1bmp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
An Introduction to Linked List

A Linked List is a linear data structure which consists of a group of nodes.
Unlike an array, In a Linked List, elements are stored in random memory locations.

Each node contains two fields :

  1. data stored at that particular address.
  2. Pointer which contains the address of the next node.

The last node of the linked list contains a pointer to null to represent the termination of the linked list.
Generally, We call the first node as the head node and the last node as the Tail node in Linked List.


Why Linked List Over Array?

The array contains the following limitations:

  1. The size of an array is fixed. We must know the size of the array at the time of its creation, hence it is impossible to change its size at runtime.

  2. Inserting a new element in an array of elements is expensive because we need to shift elements to create room for a new element to insert.

  3. Deleting an element in an array is also expensive as it also takes the shifting of elements in an array.


Advantages of Linked Lists:

  1. Insertion and deletion operations can be implemented very easily and these are not costly as they do not require shifting of elements.

  2. They are dynamic in nature. Hence, we can change their size whenever required.

  3. Stacks and queues can be implemented very easily using Linked Lists.


Disadvantages of Linked Lists:

  1. Random access of an element is not possible in Linked Lists, we need to traverse Linked List from starting to search an element into it.

  2. It is relatively slow to process in comparision of an Array.

  3. Since node of a Linked List conatins both data and pointer to next node, hence extra memory is required to store pointer of each node.


Types of Linked Lists:

There are 3 types of Linked Lists:

  1. Singly Linked List

  2. Circular Linked List

  3. Doubly Linked List


Singly Linked List:

Singly Linked List contains a node that has both data part and pointer to the next node.
The last node of the singly Linked List has a pointer to null to represent the end of the Linked List.
Traversal to previous nodes is not possible in singly Linked List i.e We can not traverse in the backward direction.


Circular Linked List

Circular Linked List is similar to singly liked list but the last node of singly Linked List has a pointer to the node which points to the first node (head node) of Linked List.


Doubly Linked List

Doubly Linked List contains a node that has three entries:

  1. data part,
  2. pointer to next node, and
  3. pointer to the previous node.

We can traverse in both forward and backward directions in doubly Linked Lists.


Implementation of Linked List:

I am implementing singly Linked List below for the sake of understanding.

#include<iostream>
using namespace std;
// Create a class which contains 2 properties data and pointer to next node
class Taslim
{
  public:
  int data;
  Taslim *next;

  Taslim(int data)
  {
   this->data=data;
   this->next=NULL;
  }
};

// Take Input from the user in Linked List  and Stop when a user enters data== -1

Taslim *takeInput()
{
  int data;
  cin>>data;
  // Intially both head and tail are NULL
  Taslim *head=NULL;
  Taslim *tail=NULL;
  while(data!=-1)
  {
    Taslim *newNode=new Taslim(data);
    // Inserting first element in Empty linked list
    if(head==NULL)
    {
      head=newNode;
      tail=newNode;
    }
    // List is not empty
    else
    {
      tail->next=newNode;
      tail=tail->next;
    }
    cin>>data;
  }
  return head;
}

// Printing elements of Linked List  1->2->3->4->5->NULL
void printNode(Taslim *head)
{
  Taslim *temp=head;
  while(temp!=NULL)
  {
    cout<<temp->data<<" ";
    temp=temp->next;
  }
}
int main()
{
  Taslim *head=takeInput(); // Taking input
  printNode(head); // Printing data
}
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK