UNIT II - Ds
UNIT II - Ds
UNIT II - Ds
Part-A
Multiple Choice Questions
2. __________ an array meansaccessing each and every element of the array for a specific
purpose. [Pg. No.71]
a)Traversing
b)Inserting
c)Searching
d)Sorting
Ans:a
4.___________ is a matrix that has large number of elements with a zero value. [Pg.No.110]
a)Sparse matrix
b)dense matrix
c)Simple matrix
d)Complex matrix
Ans: a
6. A________ linked list allows traversal of data only in one way. [Pg.no.167]
a)Singly
b)doubly
c)circular
d)None
Ans: a
7._________ linked lists are widely used in operating systems for task maintenance. [Pg.no.180]
a)Singly
b)doubly
c)circular
d)None
Ans: c
8. The main advantage of using a _______ linked list is that it makes searching twice as
efficient. [Pg.no.188]
a)Singly
b)doubly
c)circular
d)None
Ans: b
10._________ is a condition that occurs when we try to delete a node from linked list that is
empty.
a)Overflow
b)Underflow
c)NULL
d)None
Ans: b
12. Which of the following operations is performed more efficiently by doubly linked list than
by singly linked list?
a) Deleting a node whose location in given
b) Searching of an unsorted list for a given item
c) Inverting a node after the node with given location
d) Traversing a list to process each node
Ans: a
13.In linked list each node contain minimum of two fields. One field is data field to store the
data second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
Ans: c
14.A variant of linked list in which last node of the list points to the first node of the list is?
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Multiply linked list
Ans:c
16.What kind of linked list is best to answer question like “What is the item at position n?”
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Array implementation of linked list
Ans: d
17.A variation of linked list is circular linked list, in which the last node in the list points to first
node of the list. One problem with this type of list is?
a) It waste memory space since the pointer head already points to the first node and thus the list node
does not need to point to the first node.
b) It is not possible to add a node at the end of the list.
c) It is difficult to traverse the list as the pointer of the last node is now not NULL
d) All of above
Ans: c
18.A variant of the linked list in which none of the node contains NULL pointer is?
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) None
Ans: a
PART-B
ptr=ptr->next;
Now ptr holds the address of the next node of the list.
We can traverse each element of the linked list through this assignment untillptr has
NULL address (which denotes the last node of the list).
Thus the traversal of a linked list can be implemented with the following statements
while(ptr!=NULL)
ptr = ptr -> next;
The data part of the new node is assigned to the data (i.e., d) through the temp pointer as :
temp->data=d;
Again we know that the start pointer points to the first node of the linked list.
Therefore, for inserting at the beginning we will have to assign start to the next part of temp
i.e.,
Now inserted node points to the next node which was the first node of the linked list.
So the inserted node is the first node of the linked list and start pointer is reassigned as :
start = temp;
Insertion in between
For inserting a node at any position of the linked list (except at the beginning) we will have to
first traverse the linked list for obtaining the node after which we want to insert the new node.
Let us consider a pointer temp which points to the node that has to be inserted. The data part
of the new node is assigned to the data (i.e., d) through the temp pointer as :
temp->data=d;
Again we consider a pointer q which points to the node after which we have to insert the new
node.
For inserting the element after the node, we assign the next part of the node to the next part of
new inserted node as:
temp->next=q->next;
Then the address part of the new inserted node is assigned to the next part of the previous
node.
q->next=temp;
Q7.Explain deletion from a linked list.
An element that is to be deleted from a linked list is to be searched first.
For this, the traversing operation must be carried out thoroughly on the list.
After finding the element there may be two cases for deletion:
1.Deletion at beginning
2.Deletion in between
Deletion at beginning :
We know that start pointer points to the first element of a linked list.
If element to be deleted is the first element of linked list then we assign the value of start to
temp as:
temp=start ;
So now temp points to first node which has to be deleted. Now we assign the next part of the
deleted node to start as:
start=start->next;
Since start points to the first element of the linked list, so start->next will point to the second
element of the linked list.
Now we should free the element to be deleted which is pointed by temp with the following
statement:
free(temp);
Deletion in between :
If the element is other than the first element of the linked list then we assign the next
part of the deleted node to the next part of the previous node.
This can be written as:
temp=q->next;
q->next=temp->next;
free(temp);
Here, the pointer q is pointing to the previous node of the node to be deleted.
After the first statement temp will point to the node to be deleted, after second
statement next of previous node will point to next node of the node to be deleted.
If the node to be deleted is the last node of the linked list then the second statement
will be as:
q->next=NULL;
The Josephus problem (or Josephus permutation) is a theoretical problem related to a certain
counting-out game. People are standing in a circle waiting to be executed. Counting begins at a
specified point in the circle and proceeds around the circle in a specified direction. After a specified
number of people are skipped, the next person is executed. The procedure is repeated with the
remaining people, starting with the next person, going in the same direction and skipping the same
number of people, until only one person remains, and is freed. The problem — given the number of
people, starting point, direction, and number to be skipped — is to choose the position in the initial
circle to avoid execution.
The doubly linked list can be traversed in any order to reach the given element. The following listing
demonstrates how to search an element from the beginning.
node *search (node *head, int item)
{
while(head!=NULL)
{
if(head->info==item) // if the values match,
return head; // return the matching node.
head=head->fnext; // otherwise, move on
}
return NULL;
}
Q16 Differentiate between circular linked list and doubly linked list.
In the last node of a list, the link field often contains a null reference, a special value used to indicate
the lack of further nodes. A less common convention is to make it point to the first node of the list; in
that case the list is said to be 'circular' or 'circularly linked'; otherwise it is said to be 'open' or 'linear'.
In the case of a circular doubly linked list, the only change that occurs is that the end, or "tail", of the
said list is linked back to the front, or "head", of the list and vice versa.
A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next
node, and the link backward to the previous node
PART-C