0% found this document useful (0 votes)
11 views10 pages

Linked List _ DPP

The document contains a series of questions and answers related to linked lists in data structures and programming. It includes multiple-choice questions (MCQs) and natural answer type (NAT) questions that test knowledge on linked list operations, such as insertion, deletion, and traversal. Additionally, it provides hints and solutions for each question, along with an answer key for quick reference.

Uploaded by

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

Linked List _ DPP

The document contains a series of questions and answers related to linked lists in data structures and programming. It includes multiple-choice questions (MCQs) and natural answer type (NAT) questions that test knowledge on linked list operations, such as insertion, deletion, and traversal. Additionally, it provides hints and solutions for each question, along with an answer key for quick reference.

Uploaded by

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

1

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;

while(q!=NULL && q->next!=NULL && q->next-


>next != NULL){
p=p->next;
q=q->next->next;
}
printf(“%c”, p->data);
}
The output is-
(a) C (b) D
(c) E (d) B
3

Answer Key
1. (2021) 5. (a)
2. (a) 6. (c)
3. (b) 7. (b)
4. (b) 8. (c)
4

Hints and Solutions

1. (2021) P3: It prints 2->data i.e B.


The above function implementation skip the second and f(3):
third elements. It connects the head element to the fourth 3 is NOT NULL
element. f(X).
So, the size of the linked list is 2021. P2: It prints 3->data i.e C.
f(4):
2. (a) 4 is NOT NULL;
A 1 B 2 C 3 D 4 E 5 F X f(X).
5 1 P1: It prints 4->data i.e D.
0 1 2 3 4 5 f(X):
X represents NULL.
X is equal to NULL. So it returns to f(4);
Initially, q points to node 0.
OUTPUT: D C B A
p=q->next->next->next;//p=3
q->next->next->next=p->next->next;//2->next=5 5. (a)
p->next->next=q->next;//4->next=1 P: CORRECT. Insertion at the end of the linked list is
printf(“%c”, p->next->next->next->data); difficult than insertion at the beginning of the linked
3->next->next->next->data list.
=4->next->next->data Q: CORRECT. Deletion at the beginning of linked list is
=1->next->data easier as compared to deletion at the end of the linked
=2->data list.
=C
3. (b) 6. (c)
Linked List supports only linear accessing of void delete_last(struct node *head)
elements. {
4. (b) struct node *p=head, *q;
void f(struct node *q){ if(!head) return;
if(q==NULL) return; if(head->next==NULL){free(head);head=NULL;
f(q->next); return;
printf(“%c”, q->data); }
} while(p->next!=NULL)
A 2 B 3 C 4 D X {
1 2 3 4 q = p;
X represents NULL. p=p->next;
f(1): }
1 is NOT NULL. q->next=NULL;
f(2). free(p);
P4: It prints 1->data i.e A. q=NULL; p=NULL;
f(2): }
2 is NOT NULL.
f(3).
5

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

[NAT] struct node * f(struct node *head, int k){


1. Consider a linked list [a, b, c, d, e]. ptr is a pointer struct node *p=head;
pointing to the head/start node. Assume a node in the int i=0;
linked list is defined as: while(i<k/2){
struct node p=p->next;
{ i++;
int data; }
struct node *next; return p;
}; }
The output of the statement printf(“%d”, ptr->next- Assume head points to the start node of the linked list
>next->data) is ____. and k is the number of elements in the linked list, the
function returns-
[NAT] (a) The pointer to the middle element in the linked
2. Consider a single linked list of integers [9, 8, 7, 6, 5, list.
4,3] is passed to the following function: (b) The pointer to the second element in the linked
struct node list.
{ (c) The middle element in the linked list.
int data; (d) The second last element in the linked list.
struct node *next;
}; [MCQ]
int func(struct node *q){ 4. Consider the following function:
static int k=0; struct node
struct node *ptr=q; {
if(!ptr) return 0; int data;
else if(ptr->next==NULL) return k+=ptr->data; struct node *next;
else{ };
k+=ptr->data; void f(struct node *head){
func(ptr->next); struct node *a=head, *b=NULL, *c=NULL ;
return k; while(a){
} c=a->next;
} a->next=b;
Assume, q points to head/start node in the linked list. b=a;
The value returned by func(q) is __________. a=c;
}
[MCQ] head=b;
3. Consider the following function: }
struct node Assume head points to the start node of the linked list,
{ the function-
int data; (a) Sorts the linked list.
struct node *next; (b) Interchanges every two consecutive elements in
}; the list.
2

(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

[MCQ] (b) Memory leakage occurs.


8. A node of a linked list is to be created by calling (c) Memory is full.
malloc() function. The malloc() returns NULL if- (d) None of the above.
(a) Stack overflow occurs.
4

Answer Key
1. (99) 5. (c)
2. (42) 6. (b, d)
3. (a) 7. (a)
4. (c) 8. (c)
5

Hints and Solutions

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;

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4

You might also like