Introduction to the Linked
Lists
By Kristina Barooah
Computer Science Engineering, Lovely Professional University
Technical Member of GeeksforGeeks-LPU.
What are Linked Lists?
A linked list is a linear data structure, in which the elements are not stored
at contiguous memory locations, rather stored in memory location which are
free! This is what make the date in a linked list scattered. The elements in a
linked list are linked using pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a
data field and a reference(link) to the next node in the list.
Anatomy of a Linked List
.
A linked list consists of a sequence of nodes.
• Each node contains a value and a link (pointer or reference) to some other
node.
• The last node contains a NULL link.
• The list may (or may not) have a header. Here myList is the header, it ONLY
stores the address of the first node.
• A node’s successor is the NEXT node in the sequence.
• The last node has no successor.
• A node’s predecessor is the PREVIOUS node in the sequence.
• The first node has no predecessor.
• A list’s length is the number of elements in it.
• A list containing no elements exits I.e., a linked list may be empty.
• LinkedList is a dynamic structure.
Important Points about Linked Lists
Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have
some distinct advantages of using linked lists over arrays.
1. Dynamic: A linked list is dynamic in nature which means it can easily grow and shrink
in size. We don’t need to know how many nodes will be in the list. They are created in
memory as needed. In contrast, the size of a C++ array is fixed at compilation time.
2. Easy and fast insertions and deletions: To insert or delete an element in an array,
we need to copy to temporary variables to make room for new elements or close the gap
caused by deleted elements. However, with a linked list, no need to move other nodes.
Only need to reset some pointers.
4. Another point of difference between
an array and a linked list is that a linked
list does not allow random access of
data.
3. Both arrays and linked lists are a
linear collection of data elements. But
unlike an array, a linked list does not
store its nodes in consecutive memory
locations.
How to create a simple Linked
List?
In C/C++, we can implement a linked list using the following code:
struct node
{
int data;
struct node *next;
};
• Linked lists provide an efficient way of storing related data and
perform basic operations such as insertion, deletion, and updation
of information at the cost of extra space required for storing
address of the next node.
Memory
Management
in Linked
Lists
Types of Linked Lists
1. Singly linked lists
2. Doubly linked lists
3. Circular linked lists
Singly-linked lists
Doubly-linked lists
Creating a Doubly Linked List
In C/ C++, the structure of a doubly linked list can be given as, struct node
{ struct node *prev;
int data;
struct node *next; };
The PREV field of the first node and the NEXT field of the last node will contain
NULL
Memory representation of a Doubly Linked List
Circular linked lists
In a circular linked list, the last node contains a pointer to the first node of the list. We can
have a circular singly linked list as well as a circular doubly linked list. While traversing a
circular. Linked list, we can begin at any node and traverse the list in any direction, forward or
backward, until we reach the same node where we started. Thus, a circular linked list has no
beginning and no ending. It is abbreviated as CLL.
Memory representation of a Circular Linked list
Operations in Linked Lists
1. Traversing
2. Searching
3. Insertion
4. Deletion
Traversing a Singly Linked List
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR !=
NULL
Step 3: Apply Process to PTR DATA
Step 4: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 5: EXIT
Searching a Single Linked List
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3,
while PTR != NULL
Step 3: IF VAL = PTR -> DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR -> NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
If we have VAL = 4, then the flow of the algorithm can be explained as shown in the
figure:
Insertion in
linked list
Case 1: The new node is
inserted at the beginning.
Case 2: The new node is
inserted at the end.
Case 3: The new node is
inserted after a given node.
Case 4: The new node is
inserted before a given node.
Deletion in
linked list
Case 1: Deleting the First
Node from a Linked List.
Case 2: Deleting the Last
Node from a Linked List.
Case 3: Deleting the Node
After a Given Node in a
Linked List.
APPLICATIONS OF LINKED LIST
• Linked Lists can be used to implement Stacks , Queues.
• Linked Lists can also be used to implement Graphs. (Adjacency list representation of Graph).
• Implementing Hash Tables :- Each Bucket of the hash table can itself be a linked list. (Open chain
hashing).
• Undo functionality in Photoshop or Word . Linked list of states.
• A polynomial can be represented in an array or in a linked list by simply storing the coefficient and
exponent of each term.
• However, for any polynomial operation , such as addition or multiplication of polynomials , linked list
representation is easier to deal with.
• Linked lists are useful for dynamic memory allocation.
• The real-life application where the circular linked list is used is our Personal Computers, where
multiple applications are running.
• All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for

Linked list in Data Structure and Algorithm

  • 1.
    Introduction to theLinked Lists By Kristina Barooah Computer Science Engineering, Lovely Professional University Technical Member of GeeksforGeeks-LPU.
  • 2.
    What are LinkedLists? A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations, rather stored in memory location which are free! This is what make the date in a linked list scattered. The elements in a linked list are linked using pointers as shown in the below image: In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
  • 3.
    Anatomy of aLinked List . A linked list consists of a sequence of nodes. • Each node contains a value and a link (pointer or reference) to some other node. • The last node contains a NULL link. • The list may (or may not) have a header. Here myList is the header, it ONLY stores the address of the first node.
  • 4.
    • A node’ssuccessor is the NEXT node in the sequence. • The last node has no successor. • A node’s predecessor is the PREVIOUS node in the sequence. • The first node has no predecessor. • A list’s length is the number of elements in it. • A list containing no elements exits I.e., a linked list may be empty. • LinkedList is a dynamic structure. Important Points about Linked Lists
  • 5.
    Array versus LinkedLists Linked lists are more complex to code and manage than arrays, but they have some distinct advantages of using linked lists over arrays. 1. Dynamic: A linked list is dynamic in nature which means it can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time. 2. Easy and fast insertions and deletions: To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. However, with a linked list, no need to move other nodes. Only need to reset some pointers.
  • 6.
    4. Another pointof difference between an array and a linked list is that a linked list does not allow random access of data. 3. Both arrays and linked lists are a linear collection of data elements. But unlike an array, a linked list does not store its nodes in consecutive memory locations.
  • 7.
    How to createa simple Linked List? In C/C++, we can implement a linked list using the following code: struct node { int data; struct node *next; }; • Linked lists provide an efficient way of storing related data and perform basic operations such as insertion, deletion, and updation of information at the cost of extra space required for storing address of the next node.
  • 8.
  • 10.
    Types of LinkedLists 1. Singly linked lists 2. Doubly linked lists 3. Circular linked lists
  • 11.
  • 12.
  • 13.
    Creating a DoublyLinked List In C/ C++, the structure of a doubly linked list can be given as, struct node { struct node *prev; int data; struct node *next; }; The PREV field of the first node and the NEXT field of the last node will contain NULL
  • 14.
    Memory representation ofa Doubly Linked List
  • 15.
    Circular linked lists Ina circular linked list, the last node contains a pointer to the first node of the list. We can have a circular singly linked list as well as a circular doubly linked list. While traversing a circular. Linked list, we can begin at any node and traverse the list in any direction, forward or backward, until we reach the same node where we started. Thus, a circular linked list has no beginning and no ending. It is abbreviated as CLL.
  • 16.
    Memory representation ofa Circular Linked list
  • 17.
    Operations in LinkedLists 1. Traversing 2. Searching 3. Insertion 4. Deletion
  • 18.
    Traversing a SinglyLinked List Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Steps 3 and 4 while PTR != NULL Step 3: Apply Process to PTR DATA Step 4: SET PTR = PTR ->NEXT [END OF LOOP] Step 5: EXIT
  • 19.
    Searching a SingleLinked List Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Step 3, while PTR != NULL Step 3: IF VAL = PTR -> DATA SET POS = PTR Go To Step 5 ELSE SET PTR = PTR -> NEXT [END OF IF] [END OF LOOP] Step 4: SET POS = NULL Step 5: EXIT If we have VAL = 4, then the flow of the algorithm can be explained as shown in the figure:
  • 20.
    Insertion in linked list Case1: The new node is inserted at the beginning. Case 2: The new node is inserted at the end. Case 3: The new node is inserted after a given node. Case 4: The new node is inserted before a given node.
  • 21.
    Deletion in linked list Case1: Deleting the First Node from a Linked List. Case 2: Deleting the Last Node from a Linked List. Case 3: Deleting the Node After a Given Node in a Linked List.
  • 22.
    APPLICATIONS OF LINKEDLIST • Linked Lists can be used to implement Stacks , Queues. • Linked Lists can also be used to implement Graphs. (Adjacency list representation of Graph). • Implementing Hash Tables :- Each Bucket of the hash table can itself be a linked list. (Open chain hashing). • Undo functionality in Photoshop or Word . Linked list of states. • A polynomial can be represented in an array or in a linked list by simply storing the coefficient and exponent of each term. • However, for any polynomial operation , such as addition or multiplication of polynomials , linked list representation is easier to deal with. • Linked lists are useful for dynamic memory allocation. • The real-life application where the circular linked list is used is our Personal Computers, where multiple applications are running. • All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for