This document introduces linked lists, a linear data structure where elements are stored in non-contiguous memory locations, linked via pointers. It discusses the advantages of linked lists over arrays, such as dynamic sizing and efficient insertions/deletions, while also outlining types of linked lists—singly, doubly, and circular. The document also covers operations on linked lists, such as traversing and searching, and highlights various applications including implementing stacks, queues, and managing dynamic memory.
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.
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
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.
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