Module 2.
0
Linear Data Structure: Linked
List
2 09/09/2023
Abstract Data Type
Defined as a "class of objects whose logical behavior is defined by a set
of values and a set of operations“
The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations.
Called “abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known
as abstraction.
Prof. Shweta Dhawan Chachra
3 09/09/2023
Think of ADT as a black box which hides the inner
structure and design of the data type.
Prof. Shweta Dhawan Chachra
Memory Allocation
& Deallocation for
Linked List
5 09/09/2023
Memory Allocation
There are two types of Memory Allocation
Compile Time or Static Allocation
Runtime or Dynamic Allocation
Prof. Shweta Dhawan Chachra
6 09/09/2023
Compile Time or Static Allocation
Memory allocated to the program element
at the start of the program.
Memory allocated
is fixed and determined by the compiler at compile time.
Eg- float a[5] ,
allocation of 20 bytes to the array, i.e. 5*4 bytes
Prof. Shweta Dhawan Chachra
7 09/09/2023
Compile Time or Static Allocation
Inefficient Use of Memory-
Can cause under utilization of memory in case of over
allocation
That is if you store less number of elements than the number
for elements which which you have declared memory.
Rest of the memory is wasted, as it is not available to other
applications.
Can cause Overflow in case of under allocation
For float a[5];
No bound checking in C for array boundaries, if you are
entering more than fives values,
It will not give error but when these extra element will be
accessed, the values will not be available.
Prof. Shweta Dhawan Chachra
8 09/09/2023
Compile Time or Static Allocation
No reusability of allocated memory
Difficult to guess the exact size of the data at the time of writing
the program
Prof. Shweta Dhawan Chachra
9 09/09/2023
Runtime or Dynamic Allocation
All Linked Data Structures are preferably implemented
through dynamic memory allocation.
Dynamic data structures provide flexibility in adding,
deleting or rearranging data objects at run time.
Managed in C through a set of library functions:
malloc()
Calloc()
free()
Realloc()
Prof. Shweta Dhawan Chachra
10 09/09/2023
Runtime or Dynamic Allocation
Memory space required by Variables is calculated and
allocated during execution
Get Required chunk of memory at Run time or As the need
arises
Best Suited –
When we do not know the memory requirement in advance,
which is the case in most of the real life problems.
Efficient use of Memory
Additional Space can be allocated at run time.
Unwanted Space can be released at run time.
Reusability of Memory space Prof. Shweta Dhawan Chachra
11 09/09/2023
The mallloc() fn
Allocates a block of memory in bytes
The user should explicitly give the block size needed.
Request to the RAM of the system to allocate memory,
If request is granted returns a pointer to the first block of the memory
If it fails, it returns NULL
The type of pointer is Void, i.e. we can assign it any type of
pointer.
Available in header file alloc.h or stdlib.h
Prof. Shweta Dhawan Chachra
12 09/09/2023
The mallloc() fn
Syntax-
ptr_var=(type_cast*)malloc(size)
ptr_var = name of pointer that holds the starting address of
allocated memory block
type_cast* = is the data type into which the returned pointer is to be
converted
Size = size of allocated memory block in bytes
Prof. Shweta Dhawan Chachra
13 09/09/2023
The mallloc() fn
Eg-
int *ptr;
ptr=(int *)malloc(10*sizeof(int));
Size of int=2bytes
So 20 bytes are allocated,
Void pointer is casted into int and assigned to ptr
Eg-
char *ptr;
ptr=(char *)malloc(10*sizeof(char));
Prof. Shweta Dhawan Chachra
14 09/09/2023
The mallloc() fn- Usage in Linked List
Eg-
struct student;
{
int roll_no;
char name[30];
float percentage;
};
struct student *st_ptr;
st_ptr=(struct student *)malloc(10*sizeof(struct student));
Prof. Shweta Dhawan Chachra
15 09/09/2023
The callloc() fn
Similar to malloc
It neds two arguments as against one argument in malloc() fn
Eg-
int *ptr;
ptr=(int *)calloc(10,2));
1st argument=no of elements
2nd argument=size of data type in bytes
On initialization-
Malloc() contains garbage value
Calloc() contains all zeros
Available in header file alloc.h or stdlib.h
Prof. Shweta Dhawan Chachra
16 09/09/2023
The free() fn
Used to deallocate the previously allocated memory using
malloc or calloc() fn
Syntax-
free(ptr_var);
ptr_var is the pointer in which the address of the allocated memory
block is assigned.
Returns the allocated memory to the system RAM..
Prof. Shweta Dhawan Chachra
17 09/09/2023
The realloc() fn
To resize the size of memory block, which is already
allocated using malloc.
Used in two situations:
If the allocated memory is insufficient for current application
If the allocated memory is much more than what is required by
the current application
Prof. Shweta Dhawan Chachra
18 09/09/2023
The realloc() fn
Syntax-
ptr_var=realloc(ptr_var,new_size);
ptr_var is the pointer holding the starting address of already
allocated memory block.
Available inn header file<stdlib.h>
Can resize memory allocated previously through malloc only.
Prof. Shweta Dhawan Chachra
Linked List
20 09/09/2023
Linked Lists Click icon to add picture
• Linear Collection of data
elements called Nodes
• where the linear order is
given by means of pointers.
• Each node may be divided into
atleast two fields for :
• Storing Data
• Storing Address of next
element.
• The Last node’s Address field
contains Null rather than a valid
address.
• It’s a NULL Pointer and
indicates the end of the list. Prof. Shweta Dhawan Chachra
21 09/09/2023
Advantages
Linked are Dynamic Data Structures
Grow and shrink during execution of the program
Efficient Memory Utilization
As memory is not preallocated.
Memory can be allocated whenever required and deallocated
when not needed.
Insertion and deletions are easier and efficient
Provide flexibility in inserting a data item at a specified
position and deletion of a data item from the given position
Many complex applications can be easily carried out with
linked lists
Prof. Shweta Dhawan Chachra
22 09/09/2023
Disadvantages
Access to an arbitary data item is little bit cumbersome and also
time consuming
More memory
If the number of fields are more, then more memory space is needed.
Prof. Shweta Dhawan Chachra
23 09/09/2023
Comparison between Array and Linked List
Simple to use and define
Supported by almost all programming languages
Constant access time
Array element can be accessed a[i]
Mapping by compiler
Compiler maps a[i] too its physical location in memory.
Address of a[i] =Starting address of a + i *size of array
This mapping is carried out in constant time, irrespective of which
element is accessed
Prof. Shweta Dhawan Chachra
24 09/09/2023
Comparison between Array and Linked List
Arrays suffer from some severe limitations
Static Data Structure-
Size of an array is defined at the time of programming
Insertion and Deletion is time consuming
Requires Contiguous memory
Prof. Shweta Dhawan Chachra
25 09/09/2023
Data Structures | Linked List | Question 2
Which of the following points is/are true about Linked List data structure
when it is compared with array
(A) Arrays have better cache locality that can make them better in terms
of performance.
(B) It is easy to insert and delete elements in Linked List
(C) Random access is not allowed in a typical implementation of Linked
Lists
(D) The size of array has to be pre-decided, linked lists can change their
size any time.
(E) All of the above
Prof. Shweta Dhawan Chachra
26 09/09/2023
Data Structures | Linked List | Question 2
Which of the following points is/are true about Linked List data structure
when it is compared with array
(A) Arrays have better cache locality that can make them better in terms
of performance.
(B) It is easy to insert and delete elements in Linked List
(C) Random access is not allowed in a typical implementation of Linked
Lists
(D) The size of array has to be pre-decided, linked lists can change their
size any time.
(E) All of the above
Answer: (E)
Prof. Shweta Dhawan Chachra
27 09/09/2023
Implementation of Linked Lists
Structures in C are used to define a node
Address of a successor node can be stored in a pointer type
variable
Prof. Shweta Dhawan Chachra
28 09/09/2023
Linked Lists
Information field
struct node
{
int info;
struct node *link; Pointer that points to the
structure itself,
}
Thus Linked List.
Prof. Shweta Dhawan Chachra
29 09/09/2023
Types of Linked List
Singly Linked List
Doubly Linked List
Circular Linked List
Prof. Shweta Dhawan Chachra
30 09/09/2023
Singly Linked List
All nodes are linked in sequential manner
Linear Linked List
One way chain
It has beginning and end
Problem-
The predecessor of a node cannot be accessed from
the current node.
This can be overcome in doubly linked list.
Prof. Shweta Dhawan Chachra
31 09/09/2023
Doubly Linked List
Linked List holds two pointer fields
Addresses of next as well as preceding elements are
linked with current node.
This helps to traverse in both Forward or Backward
direction
Prof. Shweta Dhawan Chachra
32 09/09/2023
Circular Linked List
The first and last elements are adjacent.
A linked list can be made circular by
Storing the address of the first node in the link field
of the last node.
Prof. Shweta Dhawan Chachra
33 09/09/2023
Linked List Operations
Creation
Insertion
Deletion
Traversal
Searching
Prof. Shweta Dhawan Chachra
09/09/2023
Singly Linked
List
34 Prof. Shweta Dhawan Chachra
35 09/09/2023
Creation of a new node
New node=temp
struct node *tmp;
tmp= (struct node *) malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;
Prof. Shweta Dhawan Chachra
36 09/09/2023
Creating a Linked List
create_list(int data)
{
struct node *q,*tmp;
tmp= (struct node *) malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;
if(start==NULL) /*If list is empty */
{
start=tmp;
}
else
{ /*Element inserted at the end */
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list()*/
Prof. Shweta Dhawan Chachra
37 09/09/2023
Traversing a Linked List
Assign the Value of start to another pointer say q
struct node *q=start;
Now q also points to the first element of linked list.
For processing the next element, we assign the address of the next
element to the pointer q as-
q=q->link;
Traverse each element of the Linked list through this assignment until
pointer q has NULL address, which is link part of last element.
while(q!=NULL)
{
q=q->link;
}
Prof. Shweta Dhawan Chachra
38 09/09/2023
Searching a Linked List
First traverse the linked list
While traversing compare the info part of each element with
the given element
Prof. Shweta Dhawan Chachra
39 09/09/2023
Searching a Linked List
search(int data)
{
struct node *ptr = start;
int pos = 1;
while(ptr!=NULL)
{
if(ptr->info==data)
{
printf("Item %d found at position %d\n",data,pos);
return;
}
ptr = ptr->link;
pos++;
}
if(ptr == NULL)
printf("Item %d not found in list\n",data);
}/*End of search()*/
Prof. Shweta Dhawan Chachra
40 09/09/2023
Insertion into a Linked List
Insertion is possible in two ways:
Insertion at Beginning
Insertion in Between
Prof. Shweta Dhawan Chachra
41 09/09/2023
Case 1- Insertion at Beginning
Prof. Shweta Dhawan Chachra
42 09/09/2023
Case 1- Insertion at Beginning
Lets say tmp is the pointer which points to the node that has to be
inserted
Assign data to the new node
tmp->info=data;
Start points to the first element of linked list
Assign the value of start to the link part of the inserted node as
tmp->link=start;
Now inserted node points beginning of the linked list.
To make the newly inserted node the first node of the linked list:
start=temp
Prof. Shweta Dhawan Chachra
43 09/09/2023
Case 2- Insertion in Between
Prof. Shweta Dhawan Chachra
44 09/09/2023
Case 2- Insertion in Between
First we traverse the linked list for obtaining the node after which we
want to insert the element
q=start;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
printf("There are less than %d elements",pos);
return;
}
}/*End of for*/
Prof. Shweta Dhawan Chachra
45 09/09/2023
Case 2- Insertion in Between
Then we add the new node by adjusting address fields
tmp->info=data;
tmp->link=q->link;
q->link=tmp;
Prof. Shweta Dhawan Chachra
46 09/09/2023
Deletion from a Linked List
For deleting the node from a linked list, first we traverse the linked list
and compare with each element.
After finding the element there may be two cases for deletion-
Deletion in beginning
Deletion in between
Prof. Shweta Dhawan Chachra
47 09/09/2023
Deletion in beginning
Prof. Shweta Dhawan Chachra
48 09/09/2023
Deletion in beginning
Start points to the first element of linked list.
If element to be deleted is the first element of linked list then we assign the
value of start to tmp as-
tmp = start;
So tmp points to the first node which has to be deleted.
Now assign the link part of the deleted node to start as-
start=start->link;
Since start points to the first element of linked list, so start->link will point to
the second element of linked list.
Now we should free the element to be deleted which is pointed by tmp.
free( tmp );
Prof. Shweta Dhawan Chachra
49 09/09/2023
Deletion in between
Prof. Shweta Dhawan Chachra
50 09/09/2023
Deletion in between
If the element is other than the first element of linked list then we give the link
part of the deleted node to the link part of the previous node. This can be as-
tmp =q->link;
q->link = tmp->link;
free(tmp);
Prof. Shweta Dhawan Chachra
51 09/09/2023
Deletion in between
If node to be deleted is last node of linked list then statement 2 will be as-
q->link = NULL;
Prof. Shweta Dhawan Chachra
52 09/09/2023
ISRO | ISRO CS 2013 | Question 4
The following steps in a linked list
p = getnode() info (p) = 10 next (p) = list list = p result in which
type of operation?
(A) pop operation in stack
(B) removal of a node
(C) inserting a node
(D) modifying an existing node
Prof. Shweta Dhawan Chachra
53 09/09/2023
ISRO | ISRO CS 2013 | Question 4
The following steps in a linked list
p = getnode() info (p) = 10 next (p) = list list = p result in which type of
operation?
(A) pop operation in stack
(B) removal of a node
(C) inserting a node
(D) modifying an existing node
Answer: (C)
Explanation: p = getnode() // getnode() allocates the space which is equal to
the size of the node type structure and returns a pointer.
info (p) = 10 // value of the new node is equal to 10
next (p) = list // adding new node to the list.
Clearly, through these steps, insertion of a node is performed.
Option (C) is correct.
Prof. Shweta Dhawan Chachra
54 09/09/2023
ISRO | ISRO CS 2014 | Question 49
Consider a single linked list where F and L are pointers to the first and
last elements respectively of the linked list. The time for performing
which of the given operations depends on the length of the linked list?
(A) Delete the first element of the list
(B) Interchange the first two elements of the list
(C) Delete the last element of the list
(D) Add an element at the end of the list
Answer: (C)
Prof. Shweta Dhawan Chachra
55 09/09/2023
ISRO | ISRO CS 2014 | Question 49
Consider a single linked list where F and L are pointers to the first and last elements
respectively of the linked list. The time for performing which of the given operations depends
on the length of the linked list?
(A) Delete the first element of the list
(B) Interchange the first two elements of the list
(C) Delete the last element of the list
(D) Add an element at the end of the list
Answer: (C)
Explanation: If F and L are pointers to the first and last elements respectively of the linked list,
then:
i) Deleting the first element of the list will not depend on the length of the link list as F = F-
>next and delete first node.
ii) Interchanging the first two elements of the list will also not require the length of linked list,
simply by taking a temp node, swap the two nodes of the list.
iii) Deleting the last element of the list will require the length traversal of the list so as to obtain
the pointer of the node previous to the last node.
iv) Adding an element at the end of the list, can be done by making L->next = new node
So, correct option is (C).
Prof. Shweta Dhawan Chachra
56 09/09/2023
Data Structures | Linked List | Question 13
What are the time complexities of finding 8th element from beginning and
8th element from end in a singly linked list?
Let n be the number of nodes in linked list, you may assume that n > 8.
(A) O(1) and O(n)
(B) O(1) and O(1)
(C) O(n) and O(1)
(D) O(n) and O(n)
Prof. Shweta Dhawan Chachra
57 09/09/2023
Data Structures | Linked List | Question 13
What are the time complexities of finding 8th element from beginning and
8th element from end in a singly linked list?
Let n be the number of nodes in linked list, you may assume that n > 8.
(A) O(1) and O(n)
(B) O(1) and O(1)
(C) O(n) and O(1)
(D) O(n) and O(n)
Answer: (A)
Explanation: Finding 8th element from beginning requires 8 nodes to be
traversed which takes constant time.
For Finding 8th from end :-
• First The complete list needs to be traversed for finding the total number
of nodes in the list.
• Find n-8=no_from_start,
• Will give the number of that node from the start node for traversal.
• So O(n)
Prof. Shweta Dhawan Chachra
58 09/09/2023
ISRO | ISRO CS 2008 | Question 68
The time required to search an element in a linked list of
length n is
(A) O (log n)
(B) O (n)
(C) O (1)
(D) O(n2)
Prof. Shweta Dhawan Chachra
59 09/09/2023
ISRO | ISRO CS 2008 | Question 68
The time required to search an element in a linked list of
length n is
(A) O (log n)
(B) O (n)
(C) O (1)
(D) O(n2)
Answer: (B)
Explanation: In the worst case, the element to be searched
has to be compared with all elements of linked list. It will
take O(n) time to search the element.
So, option (B) is correct.
Prof. Shweta Dhawan Chachra
60 09/09/2023
Complexity of operations in Linked List:
Access/Traverse
It is not possible to have a constant access time in linked list
operations. The data required may be at the other end of the list
and the worst case may be to traverse the whole list to get it.
The time complexity is hence O(n).
Arrays-
It’s just O(1) for array because of constant access time using a[i]
Prof. Shweta Dhawan Chachra
61 09/09/2023
Complexity of operations in Linked List:
Insertion
Insertion in a linked list involves only manipulating the pointers of
the previous node and the new node, provided we know the location
where the node is to be inserted. Thus, the insertion of an element is
O(1).
Deletion
Similar to insertion, deletion in a linked list involves only
manipulating the pointers of the previous node and freeing the new
node, provided we know the location where the node is to be deleted.
Thus, the deletion of an element is O(1).
In order to delete a node and connect the previous and the next node
together, you need to know their pointers.
Prof. Shweta Dhawan Chachra
62 09/09/2023
Complexity of operations in Linked List:
Insertion and deletion at a known position is O(1).
However, finding that position is O(n), unless it is the head
(Singly linked list) or tail of the list(Circular Linked list).
In a doubly-linked list, both next and previous pointers are
available in the node that is to be deleted. The time complexity
is constant in this case, i.e., O(1)
When we talk about insertion and deletion complexity, we
generally assume we already know where that's going to occur.
Prof. Shweta Dhawan Chachra
63 09/09/2023
Time complexity for Search Operation in Singly Linked List
Best Case-
Element found in the first node, while loop executes only
once so O(1)
Worst Case-
Element present in the last node so while node will work
n times so O(n)
Courtesy of https://www.youtube.com/watch?v=lWEBpaVPoJA
Prof. Shweta Dhawan Chachra
64 09/09/2023
Courtesy of https://www.bigocheatsheet.com/
Prof. Shweta Dhawan Chachra
65 09/09/2023
GATE | GATE-IT-2004 | Question 13
Let P be a singly linked list. Let Q be the pointer to an intermediate
node x in the list. What is the worst-case time complexity of the best
known algorithm to delete the node x from the list?
(A) O(n)
(B) O(log2 n)
(C) O(logn)
(D) O(1)
Prof. Shweta Dhawan Chachra
66 09/09/2023
GATE | GATE-IT-2004 | Question 13
Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-case
time complexity of the best known algorithm to delete the node x from the list?
(A) O(n)
(B) O(log2 n)
(C) O(logn)
(D) O(1)
Answer: (D)
Explanation: A simple solution is to traverse the linked list until you find the node you want to delete. But
this solution requires pointer to the head node which contradicts the problem statement.
Fast solution is to copy the data from the next node to the node to be deleted and delete the next node.
Something like following.
struct node *temp = node_ptr->next; // Find next node using next pointer
node_ptr->data = temp->data; // Copy data of next node to this node
node_ptr->next = temp->next; // Unlink next node
free(temp); // Delete next node
Time complexity of this approach is O(1)
Note that this approach doesn’t work when node to deleted is last node. Since the question says
intermediate node, we can use this approach.
Prof. Shweta Dhawan Chachra
67 09/09/2023
GATE CS 1998 | Question 75
Let p be a pointer as shown in the figure in a single linked list.
What do the following assignment statements achieve ?
q: = p → next
p → next:= q → next
q → next:=(q → next) → next
(p → next) → next:= q
Prof. Shweta Dhawan Chachra
68 09/09/2023
GATE CS 1998 | Question 75
Let p be a pointer as shown in the figure in a single linked list.
What do the following assignment statements achieve ?
q: = p → next p → next:= q → next q → next:=(q → next) → next (p
→ next) → next:= q
Answer:
Explanation: Initially p points to i and q points to i+1.
p ->next:= q ->next : i next points to i+2
q ->next:=(q ->next) ? next : i+1 next points to i+3
(p ->next) ->next:= q : i+2 next points to i+1
Deducing finally i->i+2->i+1->i+3.
So, it swaps the two nodes next to p in the linked list.
Prof. Shweta Dhawan Chachra
69 09/09/2023
GATE | GATE CS 1996 | Question 12
Consider the following statements:
i. First-in-first out types of computations are efficiently supported by STACKS.
ii. Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for
almost all the basic LIST operations.
iii. Implementing QUEUES on a circular array is more efficient than implementing QUEUES on
a linear array with two indices.
iv. Last-in-first-out type of computations are efficiently supported by QUEUES.
Which of the following is correct?
(A) (ii) and (iii) are true
(B) (i) and (ii) are true
(C) (iii) and (iv) are true
(D) (ii) and (iv) are true
Prof. Shweta Dhawan Chachra
GATE | GATE CS 1996 | Question 12 70 09/09/2023
Consider the following statements:
i. First-in-first out types of computations are efficiently supported by STACKS.
ii. Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for almost all the basic LIST
operations.
iii. Implementing QUEUES on a circular array is more efficient than implementing QUEUES on a linear array with two indices.
iv. Last-in-first-out type of computations are efficiently supported by QUEUES. Which of the following is correct?
(A) (ii) and (iii) are true
(B) (i) and (ii) are true
(C) (iii) and (iv) are true
(D) (ii) and (iv) are true
Answer: (A)
Explanation: i -STACK is the data structure that follows Last In First Out (LIFO) or First In Last Out (FILO) order, in which the
element which is inserted at last is removed out first.
ii – Implementing LISTS on linked lists is more efficient than implementing it on an array for almost all the basic LIST operations
because the insertion and deletion of elements can be done in O(1) in Linked List but it takes O(N) time in Arrays.
iii- Implementing QUEUES on a circular array is more efficient than implementing it on a linear array with two indices because
using circular arrays, it takes less space and can reuse it again.
iv – QUEUE is the data structure that follows First In First Out (FIFO) or Last In Last Out (LILO) order, in which the element
which is inserted first is removed first.
only (ii) and (iii) are TRUE.
Option (A) is correct.
Prof. Shweta Dhawan Chachra
Circular Linked
List
72 09/09/2023
Creation of circular linked list
Creation of circular linked list is same as single linked list.
Last node will always point to first node instead of
NULL.
Prof. Shweta Dhawan Chachra
73 09/09/2023
Creation of circular linked list
One pointer last which points to last node of list and link
part of this node points to the first node of list.
Prof. Shweta Dhawan Chachra
74 09/09/2023
Creation of circular linked list
New element can be added as-
If linked list is empty:
If (last==NULL)
{
last=tmp;
tmp->link=last
}
Prof. Shweta Dhawan Chachra
75 09/09/2023
Advantages of a Circular linked list
In circular linked list, we can easily traverse to its previous node,
which is not possible in singly linked list.
Entire list can be traversed from any node.
If we are at a node, then we can go to any node. But in linear linked list it is
not possible to go to previous node.
In Single Linked List, for insertion at the end, the whole list has to be
traversed.
In Circular Linked list, with pointer to the last node there won’t be any
need to traverse the whole list. So insertion in the begging or at the end
takes constant time irrespective of the length of the list i.e O(1).
It saves time when we have to go to the first node from the last node. It can
be done in single step because there is no need to traverse the in between
nodes
Prof. Shweta Dhawan Chachra
76 09/09/2023
Disadvantages of Circular linked list
Circular list are complex as compared to singly linked lists.
Reversing of circular list is a complex as compared to singly or
doubly lists.
If not traversed carefully, then we could end up in an infinite loop.
Like singly and doubly lists circular linked lists also doesn’t
supports direct accessing of elements.
Prof. Shweta Dhawan Chachra
77 09/09/2023
Insertion into a circular linked list :-
Insertion in a circular linked list may be possible in two ways-
Insertion at beginning
Insertion in between
Prof. Shweta Dhawan Chachra
78 09/09/2023
Creation of circular linked list
If linked list is not empty:
Insertion at the end of the list
{
tmp->link = last->link; /* added at the end of list*/
last->link = tmp;
last = tmp;
}
Prof. Shweta Dhawan Chachra
79 09/09/2023
Creation of circular linked list
If linked list is not empty:
Insertion at the beginning of the list
To Insert a node at the beginning of the list, follow these step:
1. Create a node, say tmp.
2. Make tmp-> next = last -> next.
3. last -> next = tmp.
Prof. Shweta Dhawan Chachra
80 09/09/2023
Traversal of circular linked list
Traversal in list has a need of pointer variable which points to
first node of list. Here we maintain the pointer last which points to
last node.
But link part of this last pointer points to first node of list, so we
can assign this value to pointer variable which will point to first
node.
Now we can traverse the list until the last node of list comes-
q = last->link;
while(q != last)
{
..........
q = q->link;
}
Prof. Shweta Dhawan Chachra
81 09/09/2023
Data Structures |GATE 2004
A circularly linked list is used to represent a Queue. A single variable p
is used to access the Queue. To which node should p point such that
both the operations enQueue and deQueue can be performed in
constant time? (GATE 2004)
(A) rear node
(B) front node
(C) not possible with a single pointer
(D) node next to front
Prof. Shweta Dhawan Chachra
82 09/09/2023
Data Structures | Linked List | Question 12
A circularly linked list is used to represent a Queue. A single variable p
is used to access the Queue. To which node should p point such that
both the operations enQueue and deQueue can be performed in
constant time? (GATE 2004)
A) rear node
(B) front node
(C) not possible with a single pointer
(D) node next to front
Answer: (A)
Explanation: Answer is not “(b) front node”, as we can not get rear
from front in O(1), but if p is rear we can implement both enQueue
and deQueue in O(1) because from rear we can get front in O(1).
Prof. Shweta Dhawan Chachra
09/09/2023
Doubly Linked
List
83 Prof. Shweta Dhawan Chachra
84 09/09/2023
Doubly Linked List
Drawback of single linked list-
In single linked list we can traverse only in one direction
because each node has address of next node only.
Suppose we are in the middle of singly linked list and
To do operation with just previous node then we have no way
to go on previous node,
We will again traverse from starting node.
Solution-
Doubly linked list, in this each node has address of
previous and next node also.
Prof. Shweta Dhawan Chachra
85 09/09/2023
Doubly Linked List
Prof. Shweta Dhawan Chachra
86 09/09/2023
Doubly Linked List
The data structure for doubly linked list will be as-
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
Prof. Shweta Dhawan Chachra
87 09/09/2023
Doubly Linked List
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
struct node *previous is a pointer to structure, which will contain
the address of previous node
struct node * next will contain the address of next node in the list.
Traversal in both directions at any time.
Prof. Shweta Dhawan Chachra
88 09/09/2023
Doubly Linked List-Insertion at beginning
Start points to the first node of doubly linked list. Assign the value of start
to the next part of inserted node and address of inserted node to the prev
part of start as-
tmp->next=start;
tmp->info=data
start->prev = tmp;
Now inserted node points to the next node, which was beginning node of
the doubly linked list and prev part of second node will point to the new
inserted node. Now inserted node is the first node of the doubly linked list.
So start will be reassigned as-
start= tmp;
Prof. Shweta Dhawan Chachra
89 09/09/2023
Doubly Linked List-Insertion at beginning
Now start will point to the inserted node which is first node of
the doubly linked list.
Assign NULL to prev part of inserted node since now it will
become the first node and prev part of first node is NULL-
tmp->prev=NULL;
Prof. Shweta Dhawan Chachra
90 09/09/2023
Doubly Linked List-Insertion in between
Prof. Shweta Dhawan Chachra
91 09/09/2023
Doubly Linked List-Insertion in between
Traverse the doubly linked list for obtaining the node (q) after which we
want to insert the element.
Assign the address of inserted node(tmp) to the prev part of next node.
q->next->prev=tmp;
Assign the next part of previous node to the next part of inserted node.
tmp->next=q->next;
Address of previous node will be assigned to prev part of inserted node and
address of inserted node will be assigned to next part of previous node.
tmp->prev=q;
q->next=tmp;
Prof. Shweta Dhawan Chachra
92 09/09/2023
Deletion from doubly linked list
Traverse the linked list and compare with each element. After finding
the element there may be three cases for deletion-
Deletion at beginning
Deletion in between
Deletion of last node
Prof. Shweta Dhawan Chachra
93 09/09/2023
Deletion from doubly linked list-Deletion at beginning
Prof. Shweta Dhawan Chachra
94 09/09/2023
Doubly linked list-Deletion at beginning
Assign the value of start to tmp as-
tmp = start;
Now we assign the next part of deleted node to start as-
start=start->next;
Since start points to the first node of linked list, so start->next will point to the
second node of list.
Then NULL will be assigned to start->prev.
start->prev = NULL;
Now we should free the node to be deleted which is pointed by tmp.
free( tmp );
Prof. Shweta Dhawan Chachra
95 09/09/2023
Prof. Shweta Dhawan Chachra
96 09/09/2023
Deletion from doubly linked list-Deletion in between
If the element is other than the first element of linked list then we assign the
next part of the deleted node to the next page of the previous node and
address of the previous node to prev part of next node. This can be as-
tmp=q->next;
q->next=tmp->next;tmp->next->prev=q;
free(tmp);
Here q is pointing to the previous node of node to be deleted. After
statement 1 tmp will point to the node to be deleted. After statement 2 next
part of previous node will point to next node of the node to be deleted and
after statement 3 prev part of next node will point to previous node.
Prof. Shweta Dhawan Chachra
97 09/09/2023
Doubly linked list-Deletion of last node
If node to be deleted is last node of doubly linked list then
we will just free the last node and
tmp=q->next;
free(tmp);
next part of second last node will be NULL.
q->next=NULL;
Here q is second last node
After statement 1, tmp will point to last node
After statement 2 last node will be deleted and after statement 3
second last node will become the last node of list.
Prof. Shweta Dhawan Chachra
98 09/09/2023
Doubly linked list-Deletion of last node
Prof. Shweta Dhawan Chachra
99 09/09/2023
GATE | GATE CS 1997 | Question 4
The concatenation of two lists is to be performed in O(1) time. Which
of the following implementations of a list should be used?
(A) singly linked list
(B) doubly linked list
(C) circular doubly linked list
(D) array implementation of lists
Prof. Shweta Dhawan Chachra
100 09/09/2023
GATE | GATE CS 1997 | Question 4
The concatenation of two lists is to be performed in O(1) time. Which
of the following implementations of a list should be used?
(A) singly linked list
(B) doubly linked list
(C) circular doubly linked list
(D) array implementation of lists
Answer: (C)
Explanation: Singly linked list cannot be answer because we cannot
find last element of a singly linked list in O(1) time.
Doubly linked list cannot also not be answer because of the same
reason as singly linked list.
Prof. Shweta Dhawan Chachra
101 09/09/2023
ISRO | ISRO CS 2017 – May | Question 79
In a doubly linked list, the number of pointers affected for an insertion
operation will be
(A) 4
(B) 0
(C) 1
(D) None of these
Prof. Shweta Dhawan Chachra
102 09/09/2023
ISRO | ISRO CS 2017 – May | Question 79
In a doubly linked list, the number of pointers affected for an insertion
operation will be
(A) 4
(B) 0
(C) 1
(D) None of these
Answer: (D)
Explanation: This depends on whether we are inserting the new node
in the middle of the list (surrounded by two nodes), or at the head or
tail of the list. Insertion at the middle node will affect 4 pointers
whereas at the head or tail will affect only 2 pointers.
So, option (D) is correct.
Prof. Shweta Dhawan Chachra
103 09/09/2023
ISRO | ISRO CS 2008 | Question 69
Which of the following operations is performed more efficiently by
doubly linked list than by linear linked list?
(A) Deleting a node whose location is given
(B) Searching an unsorted list for a given item
(C) Inserting a node after the node with a given location
(D) Traversing the list to process each node
Prof. Shweta Dhawan Chachra
104 09/09/2023
ISRO | ISRO CS 2008 | Question 69
Which of the following operations is performed more efficiently by
doubly linked list than by linear linked list?
(A) Deleting a node whose location is given
(B) Searching an unsorted list for a given item
(C) Inserting a node after the node with a given location
(D) Traversing the list to process each node
Answer: (A)
Prof. Shweta Dhawan Chachra
105 09/09/2023
ISRO | ISRO CS 2008 | Question 74
The minimum number of fields with each node of doubly linked list is
(A) 1
(B) 2
(C) 3
(D) 4
Prof. Shweta Dhawan Chachra
106 09/09/2023
ISRO | ISRO CS 2008 | Question 74
The minimum number of fields with each node of doubly linked list is
(A) 1
(B) 2
(C) 3
(D) 4
Answer: (C)
Explanation: In general, each node of doubly link list always has 3 fields, i.e., the
previous node pointer, the data field, and the next node pointer.
So, answer should be option (C) 3.
However, each node of doubly linked list can have only 2 fields, i.e., XOR pointer field,
and data field. This XOR pointer field can points both previous node and next node, this
is the best case with data field. This is called as memory efficient doubly linked list
Prof. Shweta Dhawan Chachra
107 09/09/2023
Data Structures | Linked List | Question 14
Is it possible to create a doubly linked list using only one pointer with every
node.
(A) Not Possible
(B) Yes, possible by storing XOR of addresses of previous and next nodes.
(C) Yes, possible by storing XOR of current node and next node
(D) Yes, possible by storing XOR of current node and previous node
Prof. Shweta Dhawan Chachra
108 09/09/2023
Data Structures | Linked List | Question 14
Is it possible to create a doubly linked list using only one pointer with every
node.
(A) Not Possible
(B) Yes, possible by storing XOR of addresses of previous and next nodes.
(C) Yes, possible by storing XOR of current node and next node
(D) Yes, possible by storing XOR of current node and previous node
Answer: (B)
Prof. Shweta Dhawan Chachra
109 09/09/2023
Ordinary Representation: XOR List Representation:
Node A: Let us call the address variable in
prev = NULL, next = add(B) // XOR representation as npx (XOR
previous is NULL and next is of next and previous)
address of B Node A:
Node B: npx = 0 XOR add(B) // bitwise
prev = add(A), next = add(C) // XOR of zero and address of B
previous is address of A and next is Node B:
address of C npx = add(A) XOR add(C) //
Node C: bitwise XOR of address of A and
prev = add(B), next = add(D) // address of C
previous is address of B and next Node C:
is address of D npx = add(B) XOR add(D) //
Node D: bitwise XOR of address of B and
prev = add(C), next = NULL // address of D
previous is address of C and next Node D:
is NULL npx = add(C) XOR 0 // bitwise
XOR of address of C and 0
Prof. Shweta Dhawan Chachra
110 09/09/2023
Polynomial arithmetic with linked list
One useful application of linear linked list
Linked list can be used
to represent polynomial expression
for arithmetic operations also.
Prof. Shweta Dhawan Chachra
111 09/09/2023
Polynomial arithmetic with linked list
Let us take a polynomial expression-
5x4 + x3 - 6x + 2
Here we can see every symbol x is attached with two things,
coefficient and exponent.
As in 5x4 , coefficient is 5 and exponent is 4.
So we can represent polynomial expression in single linked
list
where each node of list will contain the coefficient and
exponent of each term of polynomial expression.
Prof. Shweta Dhawan Chachra
112 09/09/2023
Polynomial arithmetic with linked list
Prof. Shweta Dhawan Chachra
113 09/09/2023
Polynomial arithmetic with linked list
So the data structure for polynomial expression will be-
struct node{
int coefficient;
int exponent;
struct node *link;
}
Prof. Shweta Dhawan Chachra
114 09/09/2023
Descending sorted linked list is used based on the exponent
because it will be easier for arithmetic operation with polynomial
linked list.
Otherwise, we have a need to traverse the full list for every
arithmetic operation.
Now we can represent polynomial expression 5x4 + x3 - 6x + 2 as-
Prof. Shweta Dhawan Chachra
115 09/09/2023
Creation of polynomial linked list
Creation of polynomial linked list will be same as
creation of sorted linked list but
it be in descending order and based on exponent of symbol.
Prof. Shweta Dhawan Chachra
116 09/09/2023
Creation of polynomial linked list
In if condition we are checking for the node to be added will
be first node or not.
/* list empty or exp greater than first one */
if(start==NULL || ex> start->expo)
{
tmp->link=start;
start=tmp;
return start;
}
Prof. Shweta Dhawan Chachra
117 09/09/2023
Creation of polynomial linked list
In else part we traverse the list and check the condition for exponent then we add the node
at proper place in list.
If node will be added at the end of list then we assign NULL in link part of added node.
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo > ex)
{
ptr=ptr->link;
}
tmp->link=ptr->link;
ptr->link=tmp;
if(ptr->link==NULL) /* item to be added in the end */
tmp->link=NULL;
}
Prof. Shweta Dhawan Chachra
118 09/09/2023
Addition with polynomial linked list
For addition of two polynomial linked list, we have a need to
traverse the nodes of both the lists.
If the node has exponent value higher than another, then that
node will be added to the resultant list or we can say that nodes
which are unique to both the lists will be added in the resultant
list.
If the nodes have same exponent value then first the coefficient
of both nodes will be added then the result will be added to the
resultant list.
Suppose in traversing one list is finished then remaining node of
the another list will be added to the resultant list.
Prof. Shweta Dhawan Chachra