Professional Documents
Culture Documents
Q3 What are the various operations that can be performed on different Data Structures?
Insertion Add a new data item in the given collection of data items.
Deletion Delete an existing data item from the given collection of data items.
Traversal Access each data item exactly once so that it can be processed.
Searching Find out the location of the data item if it exists in the given collection of data items.
Sorting Arranging the data items in some order i.e. in ascending or descending order in case of
numerical data and in dictionary order in case of alphanumeric data.
1
Q7 What are Infix, prefix, Postfix notations?
Infix notation: X + Y Operators are written in-between their operands. This is the usual way we
write expressions. An expression such as
A*(B+C)/D
Postfix notation (also known as Reverse Polish notation): X Y + Operators are written after their
operands. The infix expression given above is equivalent to
A B C + * D/
Prefix notation (also known as Polish notation): + X Y Operators are written before their
operands. The expressions given above are equivalent to
/*A+BCD
Linked List
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at
contiguous location; the elements are linked using pointers.
2
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node.
So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
Representation in C:
A linked list is represented by a pointer to the first node of the linked list. The first node is called head.
If the linked list is empty, then value of head is NULL.
Each node in a list consists of at least two parts:
1) data
2) pointer to the next node
In C, we can represent a node using structures. Below is an example of a linked list node with an
integer data.
In Java, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class
contains a reference of Node class type.
// A linked list node
struct Node
{
int data;
struct Node *next;
};
1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just
need to stop when the first visited node is visited again.
2) Useful for implementation of queue. In this case, we dont need to maintain two pointers for front
and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can
always be obtained as next of last.
3) Circular lists are useful in applications to repeatedly go around the list. For example, when multiple
applications are running on a PC, it is common for the operating system to put the running applications
on a list and then to cycle through them, giving each of them a slice of time to execute, and then
making them wait while the CPU is given to another application. It is convenient for the operating
system to use a circular list so that when it reaches the end of the list it can cycle around to the front of
the list.
4) Circular Doubly Linked Lists are used for implementation of advanced data structures
like Fibonacci Heap
3
Q12: How to traverse the circular linked List?
Ans: To traverse the list, list is started from the head node and stop the traversal when we reach
NULL. In a circular linked list, we stop traversal when we reach the first node again. Following is C
code for linked list traversal.
/* Function to traverse a given Circular linked list and print nodes */
void printList(struct Node *first)
{
struct Node *temp = first;
// If linked list is not empty
if (first != NULL)
{
// Keep printing nodes till we reach the first node again
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while (temp != first);
}
}
Q13: How to insert element in the circular linked List?
Ans:
To Insert a node at the beginning of the list, follow these step:
1. Create a node, say T.
2. Make T -> next = last -> next.
3. last -> next = T.
After insertion,