You are on page 1of 4

Important Question Answers (Data Structure) BCA 3Sem

Q1 What is a Data Structure?


A data structure is a way of organizing the data so that the data can be used efficiently. Different kinds
of data structures are suited to different kinds of applications, and some are highly specialized to
specific tasks. For example, B-trees are particularly well-suited for implementation of databases, while
compiler implementations usually use hash tables to look up identifiers. (Source: Wiki Page)

Q2 What are linear and non linear data Structures?


Linear: A data structure is said to be linear if its elements form a sequence or a linear list. Examples:
Array. Linked List, Stacks and Queues
Non-Linear: A data structure is said to be non-linear if traversal of nodes is nonlinear in nature.
Example: Graph and Trees.

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.

Q4 How is an Array different from Linked List?


The size of the arrays is fixed, Linked Lists are Dynamic in size.
Inserting and deleting a new element in an array of elements is expensive, Whereas both insertion and
deletion can easily be done in Linked Lists.
Random access is not allowed in Linked Listed.
Extra memory space for a pointer is required with each element of the Linked list.
Arrays have better cache locality that can make a pretty big difference in performance.

Q5 What is Stack and where it can be used?


Stack is a linear data structure which the order LIFO(Last In First Out) or FILO(First In Last Out) for
accessing elements. Basic operations of stack are : Push, Pop , Peek
Applications of Stack:
1. Infix to Postfix Conversion using Stack
2. Evaluation of Postfix Expression
3. Reverse a String using Stack
4. Implement two stacks in an array
5. Check for balanced parentheses in an expression

Q6 What is a Queue, how it is different from stack and how is it implemented?


Queue is a linear structure which follows the order is First In First Out (FIFO) to access elements.
Mainly the following are basic operations on queue: Enqueue, Dequeue, Front, Rear
The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added. Both Queues and Stacks can
be implemented using Arrays and Linked Lists.

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

Q8 What is a Linked List and What are its types?


A linked list is a linear data structure (like arrays) where each element is a separate object. Each
element (that is node) of a list is comprising of two items the data and a reference to the next node.
Types of Linked List :
1. Singly Linked List : In this type of linked list, every node stores address or reference of next node in
list and the last node has next address or reference as NULL. For example 1->2->3->4->NULL
2. Doubly Linked List : Here, here are two references associated with each node, One of the reference
points to the next node and one to the previous node. Eg. NULL<-1<->2<->3->NULL
3. Circular Linked List : Circular linked list is a linked list where all nodes are connected to form a
circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly
circular linked list. Eg. 1->2->3->1 [The next pointer of last node is pointing to the first]

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.

Q9: Why Linked List?


Arrays can be used to store linear data of similar types, but arrays have following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive, because room has to be created for the
new elements and to create room existing elements have to shifted.
For example, in a system if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to
delete 1010 in id[], everything after 1010 has to be moved.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion

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;
};

Q10: What is Circular Linked List?


Answer: Circular linked list is a linked list where all nodes are connected to form a circle. There is
no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked
list.

Advantages of Circular Linked Lists:

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,

Function to insert node in the beginning of the List,


struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
// Creating a node dynamically.
struct Node *temp
= (struct Node *)malloc(sizeof(struct Node));
// Assigning the data.
temp -> data = data;
// Adjusting the links.
temp -> next = last -> next;
last -> next = temp; return last;}

You might also like