Linked List Questions1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Data structure Assignment 3

Instructions
1. Submit assignment before mid sem exam
2. Submit in hardcopy
3. Write both question and answer with proper format
4. While writing output take care of all the spaces and newlines properly.
5. All incorrect answers need to be submitted with the next assignment. If not done, then the next assignment will not
be checked
6. WAP: Write a program

1) Write the structure code fragment for defining a node for doubly linked list?
2) WAP to find nth node from the end of a singly linked list. n value given by user.
3) WAP to Insert a node in a sorted singly linked list.
4) WAP to reverse a singly linked list.
5) WAP to find the middle of the singly linked list.
6) WAP to check whether the given singly linked list length is even or odd?
7) Given two sorted singly linked lists, WAP to merge them into the third list in sorted order?
8) WAP to reverse the singly linked list in pairs. If you have a linked list that holds 1 -> 2 -> 3 -> 4 -> X,
then after the function has been called the linked list would hold 2->1->4->3->X.
9) WAP to check if the singly linked list is palindrome or not?
10) Is it possible to get O(1) access time for linked list? If yes, then how?
11) Given a linked list with even and odd numbers, WAP for making changes to the singly list in such a
way that all even numbers appear at the beginning.
12) Given a singly linked list, write a function to find the sqrt(n) element, where n is the number of
elements in the list. Assume the value of n is not known in advance.
13) Define the structure of doubly linked list?
14)WAP to remove a node from a doubly linked list?
15) Write a program to convert a binary tree into a doubly linked list?
16) WAP to remove duplicate nodes in an unsorted singly linked list?
17) Write a program to create a linked list which stores the details of employees in a department. Read
and print the information stored in the list.
18) WAP to delete alternate nodes of a singly linked List? For example, if the given linked list is 1->4-
>8->10->15 then your function should convert it to 1->8->15.
19) Describe what is Node in link list? And name the types of Linked Lists?
20) What type of memory allocation is referred for Linked lists?
21)Mention what are the applications of Linked Lists?
22) What is the difference between an array and linked list?
23) What is the difference between singly and doubly linked list?
24) Mention what are the applications that use Linked lists?
25) Mention what is the biggest advantage of linked lists?
26) Mention what is the biggest disadvantage of linked lists?
27) Linked list or a linear array, which one will you prefer to use and when?
28) Why is a doubly linked list more useful than a singly linked list?
29) Give the advantages and uses of a circular linked list.
30) Specify the use of a header node in a header linked list.
31) Explain the difference between a circular linked list and a singly linked list.

32) Write a program to print the total number of occurrences of a given item in the linked list.
33) WAP to delete the first occurrence of a given character in a linked list
34) WAP to delete the last occurrence of a given character
35) WAP to delete all occurrences of a given character
36) Write a program to sort the values stored in a doubly circular linked list.
37) Write a program to input an n digit number. Now, break this number into its individual digits and then
store every single digit in a separate node thereby forming a linked list. For example, if
you enter 12345, then there will 5 nodes in the list containing nodes with values 1, 2, 3, 4, 5.
38) Write a program to exchange the value of the first element with the last element, the second element
with the second last element, so on and so forth of a doubly linked list.
39) Write a program to form a linked list of floating point numbers. Display the sum and mean of these
numbers.
40) Write a program to delete all nodes from a singly linked list that has negative values in its data part.
41) Describe a recursive algorithm that counts the number of nodes in a circularly linked list.
42) Write a program to delete the first element of a doubly linked list. Add this node as the last node of
the list.
43) Write a program to reverse a linked list using recursion.
44) What does the following function do, for a given Linked List with first node as head?

void fun1(struct node* head){


if(head == NULL)
return;

fun1(head->next);
printf("%d ", head->data);
}

45) Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.
void fun(struct node **head_ref){
struct node *temp = NULL;
struct node *current = *head_ref;
while (current != NULL) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function 1 <--> 2 <--> 3
<--> 4 <--> 5 <-->6. What should be the modified linked list after the function call?

46) The following function reverse() is supposed to reverse a singly linked list. There is one line missing
at the end of the function.

struct node{
int data;
struct node* next;
};

static void reverse(struct node** head_ref){


struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
/*ADD A STATEMENT HERE*/
}
What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly
reverses a linked list.

47) What is the output of following function for start pointing to first node of following linked list? 1->2-
>3->4->5->6
void fun(struct node* start){
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}

48) The following C function takes a single-linked list of integers as a parameter and rearranges the
elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the
given order. What will be the contents of the list after the function completes execution?

struct node{
int value;
struct node *next;
};
void rearrange(struct node *list){
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q) {
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}

49) In the worst case, how many comparisons are needed to search a singly linked list of length n for a
given element?
50) What are the time complexities of finding 8th element from beginning and 8th element from end in a
singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8.
51) What does the following function do for a given Linked List with first node as head?
void fun1(struct node* head){
if(head == NULL)
return;

fun1(head->next);
printf("%d ", head->data);
}

52) What will be the output of the function for the linked list
1 <--> 2 <--> 3 <--> 4 <--> 5 <-->6
void func(node* root) {
if(!root)
return;
func(root->link);
printf("%d ",root->data);
}

53) Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.
void fun(struct node **head_ref){
struct node *temp = NULL;
struct node *current = *head_ref;

while (current != NULL) {


temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function
1 2 3 4 5 6.
What should be the modified linked list after the function call?

54) The following C function takes a simply-linked list as input argument. It modifies the list by moving
the last element to the front of the list and returns the modified list. Some part of the code is left blank.
Fill the correct alternative to replace the blank line.

typedef struct node{


int value;
struct node *next;
}Node;

Node *move_to_front(Node *head) {


Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
return head;
}

55) What does the following function do for a given Linked List?
void fun1(struct Node* head){
if(head == NULL)
return;

fun1(head->next);
printf("%d ", head->data);
}

56) What does the following function do for a given Linked List ?
void fun2(struct Node* head){
if(head== NULL)
return;
printf("%d ", head->data);
if(head->next != NULL )
fun2(head->next->next);
printf("%d ", head->data);
}

57) Consider the function f defined below.


struct item{
int data;
struct item * next;
};

int f(struct item *p){


return ( (p == NULL) || (p->next == NULL) || (( P->data <= p->next->data) && f(p->next))
);
}
For a given linked list p, when the function f will return 1
58) A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue.
To which node should p point such that both the operations enQueue and deQueue can be performed in

constant time?

59) Let p be a pointer as shown in the figure in a single linked list.

What do the following assignment statements achieve ?


q: = p → next
p → next:= q → next
q → next:=(q → next) → next
(p → next) → next:= q

60) Consider the following piece of 'C' code fragment that removes duplicates from an ordered list of
integers.
Node *remove-duplicates(Node *head, int *j){
Node *t1, *t2;
*j=0;
t1 = head;
if (t1! = NULL) t2 = t1 →next;
else return head;
*j = 1;
if(t2 == NULL)
return head;
while t2 != NULL)
{
if (t1.val != t2.val) --------------------------→ (S1)
{
(*j)++; t1 -> next = t2; t1 = t2: ----------→ (S2)
}
t2 = t2 →next;
}
t1 →next = NULL;
return head;
}

Assume the list contains n elements (n≥2) in the following questions. a). How many times is the
comparison in statement S1 made? b). What is the minimum and the maximum number of times
statements marked S2 get executed?
61) Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.
void fun(struct node **head_ref)
{
struct node *temp = NULL;
struct node *current = *head_ref;

while (current != NULL)


{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function
6 5 4 3 2 1.

62) What will be the output of the function for the linked list
6 <--> 5 <--> 4 <--> 3 <--> 2 <-->1
void func(node* root) {
if(!root)
return;
func(root->link);
printf("%d ",root->data);
}

63) What does the following function do for a given Linked List ?
void fun2(struct Node* head){
if(head== NULL)
return;
printf("%d ", head->data);
if(head->next != NULL )
fun2(head->next->next->next);
printf("%d ", head->data);
}

You might also like