0% found this document useful (0 votes)
12 views16 pages

The Size of The Arrays Is Fixed Insertion of A New Element / Deletion of A Existing Element in An Array of Elements Is Expensive

A linked list is a dynamic data structure consisting of nodes that store data and pointers to the next node, allowing for efficient insertion and deletion compared to fixed-size arrays. There are three types of linked lists: singly, doubly, and circular, each with specific characteristics and operations. While linked lists offer advantages like dynamic memory allocation and ease of operations, they also have drawbacks such as sequential access and additional memory overhead for pointers.

Uploaded by

Ashok Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

The Size of The Arrays Is Fixed Insertion of A New Element / Deletion of A Existing Element in An Array of Elements Is Expensive

A linked list is a dynamic data structure consisting of nodes that store data and pointers to the next node, allowing for efficient insertion and deletion compared to fixed-size arrays. There are three types of linked lists: singly, doubly, and circular, each with specific characteristics and operations. While linked lists offer advantages like dynamic memory allocation and ease of operations, they also have drawbacks such as sequential access and additional memory overhead for pointers.

Uploaded by

Ashok Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Linked List

Why Linked List?

Arrays can be used to store linear data of similar types, but arrays have the following limitations.

 The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also,
generally, the allocated memory is equal to the upper limit irrespective of the usage.
 Insertion of a new element / Deletion of a existing element in an array of elements is expensive: The room has
to be created for the new elements and to create room existing elements have to be shifted but in Linked list if we
have the head node then we can traverse to any node through it and insert new node at the required position.

For example, in a system, if we maintain a sorted list of IDs in an array id[].


id[] = [1000, 1010, 1050, 2000, 2040].
And 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.

Advantages over arrays:

1. Dynamic Array.
2. Ease of Insertion/Deletion.

Drawbacks:

1. Random access is not allowed. We have to access elements sequentially starting from the first node(head node).
So we cannot do binary search with linked lists efficiently with its default implementation.
2. Extra memory space for a pointer is required with each element of the list.
3. Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there
in case of linked lists.

Representation:

A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the
linked list is empty, then the value of the head points to NULL.

Each node in a list consists of at least two parts:

1. A Data Item (we can store integer, strings or any type of data).
2. Pointer (Or Reference) to the next node (connects one node to another) or An address of another node

In C, we can represent a node using structures. Below is an example of a linked list node with integer data.
In Java, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class contains a
reference of Node class type.

Linked List Traversal


In the previous program, we have created a simple linked list with three nodes. Let us traverse the created list and
print the data of each node. For traversal, let us write a general-purpose function printList() that prints any given list.

Linked List Complexity:

Time Complexity

Time Complexity Worst Case Average Case


Search O(n) O(n)
Insert O(1) O(1)
Deletion O(1) O(1)
Auxiliary Space: O(n)

Linked List is an important topic belonging to the famous chapter of Computer Science i.e. Data Structure. And, when it
comes to a competitive examination like GATE, you have to read the whole topic quite deeply. In this article, we have
covered all the topics relevant to the linked list. We hope the notes for the CSE topics will help you understand this topic
in a better way.
 What Is A Linked List?
 Types Of Linked List
 Basic Operations In Linked List
 Advantages Of Linked List
 Disadvantages Of Linked List
 Practice Problems – Linked List
 FAQs Related To Linked List

What is a Linked List?


A linked list is a sequence of data structures. It is also known as a linear data structure that comprises a set of connected
nodes. Each node is used to store the data and also the address of the next node.

Explanation of Picture
 The starting point of the linked list is known as the head of the list. It is not a different node, but refers to the first
node.
 The node present in the end is called NULL.

Types of Linked List


There are 3 different types of Linked Lists:

1. Singly Linked List


2. Doubly Linked List
3. Circular Linked List

1. Single Linked List


It is the most manageable type of linked list in which every node includes some data and the address part, which means
a pointer to the next node in the series. In a singly linked list, we can perform operations like insertion, deletion, and
traversal.
2. Doubly Linked List
When a node holds a data part and two addresses, it is known as a doubly-linked list. Two addresses means a pointer to
the previous node and the next node.

3. Circular Linked List


In a circular linked list, the last node of the series contains the address of the first node to make a circular chain.

Basic Operations In Linked List


When it comes to linked lists, there are five operations supported by the series or list.

 Traversal – Through this operation, we can access elements.


 Insertion – In this operation, elements can be added at the starting of the list.
 Deletion – In this operation, elements can be deleted at the starting of the list.
 Search – Through this operation, we can easily search for an element utilising the provided key.
 Sort – Through this operation, we can sort the nodes of the linked list.

Advantages of Linked Lists


 A linked list is dynamic, which means it will provide memory whenever needed.
 In a linked list, we can swiftly execute the operations like insertion and deletion.
 We can easily implement stacks and queues.
 It helps in reducing the access time.

Disadvantages of Linked Lists


 Sometimes the memory gets wasted because pointers need extra memory for storage.
 We can access elements in sequence. You cannot do this process in a random manner.
 In a linked list, reverse traversing is challenging.

Practice Problems – Linked List


Q. In a circular linked list organization, insertion of a record involves modification of:

a. One Pointer
b. One Pointer
c. Multiple pointer
d. No pointer

Q. Consider the function f defined below.


struct item {
int data;
struct item * next;
};
int f(struct item *p) {
return ((p == NULL) || (p ->next == NULL) ||
((p->data <= p -> next -> data) &&
f(p-> next)));
}

For a given linked list p, the function f returns 1 if and only if

a. the list is empty or has exactly one element


b. the elements in the list are sorted in non-decreasing order of data value
c. the elements in the list are sorted in non-increasing order of data value
d. not all elements in the list have the same data value

FAQs Related to Linked List


Is a linked list difficult?
Not as such. In a linked list, we can easily manage the insertion and deletion operation. The only difficult part is to access
the element, or we can say that the access time is linear.

Which operations are supported by a linked list?


Insertion, deletion, search, traversal and sort.
What is a linked list?
A linked list is a set of nodes where each node is linked to the next node with the help of a pointer.

You might also like