Linked Lists Vs Arrays
Linked Lists Vs Arrays
Linked Lists Vs Arrays
AND ALGORITHMS
COURSE CODE: CS-201
INSTRUCTOR: SAHAR ARIF
EMAIL: [email protected]
LINKED LISTS VS ARRAYS
LINKED LISTS
• Linked List is a linear data structure, in which elements are not stored
at a contiguous location, rather they are linked using pointers. Linked
List forms a series of connected nodes, where each node stores the
data and the address of the next node.
• Basic Terminologies of Linked List
• Head: The Head of a linked list is a pointer to the first node or reference of the first node of linked list. This pointer
marks the beginning of the linked list.
• Node: Linked List consists of a series of nodes where each node has two parts: data and next pointer.
• Data: Data is the part of node which stores the information in the linked list.
• Next pointer: Next pointer is the part of the node which points to the next node of the linked list.
• Importance of Linked List
• Here are a few advantages of a linked list that is listed below, it will help you understand why it is necessary to know.
• Dynamic Data structure: The size of memory can be allocated or de-allocated at run time based on the operation
insertion or deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than arrays since no elements need to
be shifted after insertion and deletion, Just the address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic data structure the size increases or decreases as per
the requirement so this avoids the wastage of memory.
• Implementation: Various advanced data structures can be implemented using a linked list like a stack, queue, graph,
hash maps, etc.
Example:
In a system, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000,
2040].
If we want to insert a new ID 1005, then to maintain the sorted order, we have to move all
the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For
example, to delete 1010 in id[],
everything after 1010 has to be moved due to this so much work is being done which affects
the efficiency of the code.
• Types of linked lists:
• There are mainly three types of linked lists:
• Single-linked list
• Double linked list
• Circular linked list
• 1. Single-linked list:
• In a singly linked list, each node contains a reference to the next node
in the sequence. Traversing a singly linked list is done in a forward
direction.
• // Singly linked list node in C++
• class Node {
• public:
•
• // Data field
• int data;
•
• // Pointer to the next node
• Node* next;