0% found this document useful (0 votes)
28 views5 pages

Linked List - DPP 01 (Of Lec 02)

liked list practice question

Uploaded by

rahulkuiry04
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)
28 views5 pages

Linked List - DPP 01 (Of Lec 02)

liked list practice question

Uploaded by

rahulkuiry04
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/ 5

1

CSE/IT Batch-Hinglish
Data structure & Programming
Linked List DPP-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;
}

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