Linked List _ DPP
Linked List _ DPP
CSE/IT Batch-English
Data Structure & Programming DPP
Linked List Section-01
[NAT] [NAT]
1. Consider a single linked list q with 2023 elements is 3. Consider the following statements:
passed to the following function: P: Linked Lists supports linear accessing of elements
struct node { Q: Linked Lists supports random accessing of
int data; elements.
struct node *next; Which of the following statements is/are
}; INCORRECT?
void f(struct node *q){ (a) P only (b) Q only
struct node *p; (c) Both P and Q (d) Neither P nor Q
p=q->next;
q->next=p->next->next; [MCQ]
} 4. Consider a single linked list q[‘A’, ‘B’, ‘C’, ‘D’] is
The size of the linked list q after the execution of the passed to the following function:
function is ____________. void f(struct node *q)
{
[MCQ] if(q==NULL) return;
2. Consider a single linked list q[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, f(q->next);
‘F’] is passed to the following function: printf(“%c ”, q->data);
struct node { }
int data; The output is-
struct node *next; (a) C D B A (b) D C B A
}; (c) A B C D (d) B C D A
void f(struct node *q)
{ [NAT]
struct node *p; 5. Consider the following statements:
p=q->next->next->next; P: Insertion at the end of the linked list is difficult
q->next->next->next=p->next->next; than insertion at the beginning of the linked list.
p->next->next=q->next; Q: Deletion at the beginning of linked list is easier as
printf(“%c”, p->next->next->next->data); compared to deletion at the end of the linked list.
} Which of the following statements is/are CORRECT?
The output is- (a) Both P and Q (b) P only
(a) C (b) D (c) Q only (d) Neither P nor Q
(c) E (d) B
2
[NAT] [NAT]
6. The following C function takes a single-linked list p of 8. The following C function takes a single-linked list p of
integers as a parameter. It deletes the last element of integers as a parameter. It inserts the element at the end
the single linked list. Fill in the blank space in the code: of the single linked list. Fill in the blank space in the
struct node { code:
int data; struct node
struct node *next; {
}; int data;
void delete_last(struct node *head) struct node *next;
{ };
struct node *p=head, *q; void insert_last(struct node *head, struct node *q){
if(!head) return; struct node *p=head;
if(head->next==NULL){free(head);head=NULL; if(!head) return;
return;} while(______a______){
while(______a_________){ p=p->next;
q = p; _____b______;
p=p->next; q=NULL;
} p=NULL;
______b_________; }
free(p); }
q=NULL; p=NULL; Assume, q is the address of the new node to be added.
} (a) a: !head ; b: q->next = NULL;
(a) a: !head ; b: q->next = NULL; (b) a: q->next ! = NULL; b: p->next = q
(b) a: p->next ! = head ; b: q->next = q (c) a: p->next ! = NULL ; b: p->next = q
(c) a: p->next ! = NULL ; b: q->next = NULL (d) a: head->next ! = p ; b: q->next = p
(d) a: head->next ! = p ; b: q->next = p
[MCQ]
7. Consider a single linked list q[[‘A’, ‘B’, ‘C’, ‘D’, ‘E’,
‘F’, ‘G’] is passed to the following function:
void func(struct node *head){
struct node *p=head, *q=head;
Answer Key
1. (2021) 5. (a)
2. (a) 6. (c)
3. (b) 7. (b)
4. (b) 8. (c)
4
7. (b) 8. (c)
The code prints the middle element in the linked list q. void insert_last(struct node *head, struct node *q){
A B C D E F G. struct node *p=head;
Output: D if(!head) return;
while(p->next!=NULL)
p=p->next;
p->next=q;
q=NULL;
p=NULL;
}
1
CSE/IT Batch-English
Data Structure & Programming
Linked List
Section-02
(c) Reverses the list. (b) Returns the pointer to the node where a
(d) None of the above. cycle/loop starts (assume the leftmost node in a
loop is the start of a cycle).
[MCQ] (c) Reverses the list.
5. Consider a single linked list [1, 2, 3, 4, 5] is passed to (d) Detects a cycle in the list.
the following function:
struct node [MCQ]
{ 7. Consider the following function:
int data; struct node
struct node *next; {
}; int data;
void func(struct node *p){ struct node *next;
struct node *q=p->next, *temp; };
if(!p||!(p->next)) return; void f(struct node *head, int e){
else{ struct node *p, *q;
temp=q->data; if(head->data==e){
q->data=p->data; q=p;
p->data=temp; p=p->next;
func(p->next->next); _________;
} head=p;
} return;
Initially, the address of the head/start node is passed }
to the function func(*p), the arrangement of the q=head; p=head->next;
linked list after function execution is – while(p->next!=NULL){
(a) 2 3 4 5 1 (b) 5 4 3 2 1 if(p->data==e){
(c) 2 1 4 3 5 (d) 2 1 4 5 3 _____________;
free(p);
[MSQ] return;
6. Consider the following function: }
struct node q=p;
{ p=p->next;
int data; }
struct node *next; if(p->data==e){
}; q->next=NULL;
struct node * f(struct node *head){ free(p);
struct node *p=head, *q=head; }
while(q!=NULL && q->next!=NULL && q- }
>next->next != NULL){ Assume there are at least two elements in the single
p=p->next; linked list of integers. The starting node’s address is
q=q->next->next; contained in the head pointer passed to the function.
if(p==q) break; The function f() searches for the element e in the list.
} If found, the function deletes the node. The missing
return p;} statements are-
Assume head points to the start node of the linked list, (a) free(q), q->next=p->next
the function- (b) free(q), q=p->next
(a) Returns the pointer to the node where a (c) free(p), q=p->next
cycle/loop ends (assume the leftmost node in a (d) free(p), q->next=p->next
loop is the start node of a cycle).
3
Answer Key
1. (99) 5. (c)
2. (42) 6. (b, d)
3. (a) 7. (a)
4. (c) 8. (c)
5
1. (99) if(head->data==e){
printf(“%d”, ptr->next->next->data); //The ASCII of c q=p;
is printed i.e 99 p=p->next;
free(q);
2. (42) head=p;
It computes the sum of all the data elements in a return;
recursive manner. }
Output = 9+8+7+6+5+4+3=42 q=head; p=head->next;
while(p->next!=NULL){
3. (a) if(p->data==e){
It returns the pointer to the middle element in the linked q->next=p->next;
list. free(p);
return;
4. (c) }
It reverses the list. q=p;
p=p->next;
5. (c) }
The function reverses the elements in groups of 2. if(p->data==e){
So, output is 2 1 4 3 5 q->next=NULL;
free(p);
6. (b, d) }
It detects a cycle in the linked list and returns the }
pointer to the node where the loop starts from.
8. (c)
7. (a) The malloc() returns NULL only if the memory is full.
void f(struct node *head, int e){
struct node *p, *q;