SDC 1.1
SDC 1.1
0
Linear Data Structure: Linked
List
2 09/09/2023
It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations.
Memory Allocation
Difficult to guess the exact size of the data at the time of writing
the program
Best Suited –
When we do not know the memory requirement in advance,
which is the case in most of the real life problems.
The mallloc() fn
The mallloc() fn
Syntax-
ptr_var=(type_cast*)malloc(size)
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));
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
The free() fn
Used to deallocate the previously allocated memory using
malloc or calloc() fn
Syntax-
free(ptr_var);
The realloc() fn
To resize the size of memory block, which is already
allocated using malloc.
The realloc() fn
Syntax-
ptr_var=realloc(ptr_var,new_size);
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
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.
Mapping by compiler
Compiler maps a[i] too its physical location in memory.
Answer: (E)
Linked Lists
Information field
struct node
{
int info;
struct node *link; Pointer that points to the
structure itself,
}
Thus Linked List.
Singly Linked
List
New node=temp
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
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
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
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
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*/
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
Deletion in beginning
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 );
Deletion in between
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);
Deletion in between
If node to be deleted is last node of linked list then statement 2 will be as-
q->link = NULL;
Answer: (C)
Answer: (C)
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).
Answer: (A)
Explanation: Finding 8th element from beginning requires 8 nodes to be
traversed which takes constant time.
Answer: (B)
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]
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
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
Courtesy of https://www.bigocheatsheet.com/
Prof. Shweta Dhawan Chachra
65 09/09/2023
(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
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.
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.
One pointer last which points to last node of list and link
part of this node points to the first node of list.
If (last==NULL)
{
last=tmp;
tmp->link=last
}
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
q = last->link;
while(q != last)
{
..........
q = q->link;
}
Prof. Shweta Dhawan Chachra
81 09/09/2023
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
Solution-
Doubly linked list, in this each node has address of
previous and next node also.
Prof. Shweta Dhawan Chachra
85 09/09/2023
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
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;
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
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.
Answer: (C)
Answer: (D)
Answer: (A)
The minimum number of fields with each node of doubly linked list is
(A) 1
(B) 2
(C) 3
(D) 4
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.
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
Answer: (B)
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;
}