Linked List - DPP 01 (Of Lec 02)
Linked List - DPP 01 (Of Lec 02)
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;
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;
}