viva
viva
A data structure is a way of organizing and storing data in a computer so that it can be
accessed and modified efficiently. It is mainly 2 type:
• Primitive datastructure: Fundamental data structure, represent single values. Eg: int,
float, char
• Non- primitive datastructure: Higher level data structure built using primitive data
types. Divided into two:
Linear data structure: Data elements stored in a sequential order. Eg: Array,
queue, Stack, Linked list
Non linear data structure: Data elements are not sequentialy stored. Eg: Tree,
Graph
Static memory allocation: Has a fixed memory size. Allocate memory during compile time. Eg : array
Dynamic memory allocation: Size is not fixed. Allocate memory during run time. Eg : linked list
Array is a collection of elements, all of the same type, that are stored in contiguous memory
locations. The elements in an array are indexed, meaning each element can be accessed
directly using its index or position in the array.
Mainly 2 types of array:
• One- dimensional array
• Multi-dimensional array
int main()
{
int arr[6] = {1, 2, 3, 4, 5}; // Array of size 5
int newElement = 10;
int position = 2; // Insert at position 2 (0-based index)
// Shift elements to the right to make space for the new element
for (int i = 4; i >= position; i--)
{
arr[i + 1] = arr[i];
}
// Insert the new element
arr[position] = newElement;
}
Condition check before push an element: Stack overflow (stack is full) (top>= max)
Condition check before pop an element: Stack underflow (stack is empty) (top<0)
Data structure used for infix to postfix conversion and evaluation of postfix expression: Stack
Circular Queue is a type of queue data structure that uses a circular array to efficiently manage
elements in a queue, allowing both ends (front and rear) to be connected in a loop.
• In a regular queue, once elements are dequeued and the front pointer moves, there may be
unused space at the beginning of the array. This is inefficient as new elements cannot be
added until the front pointer reaches the rear pointer again. In a circular queue, the unused
spaces are reused.
18. What is Priority Queue?
A Priority Queue is a type of abstract data structure that operates similarly to a regular
queue, but with one key difference: each element in a priority queue has a priority
associated with it. Elements with higher priority are dequeued before those with lower
priority, regardless of their position in the queue.
Two types:
o Max-Priority Queue: In a max-priority queue, the element with the highest
priority is always dequeued first.
o Min-Priority Queue: In a min-priority queue, the element with the lowest
priority is dequeued first.
21. what is the difference between singly and doubly linked lists?
Feature Singly linked list Doubly linked list
Memory Usage Less memory (1 pointer per More memory (2 pointers per
node) node)
A Binary Search Tree (BST) is a type of binary tree in which each node has at most two
children, typically referred to as the left and right child. For any node in the BST:
o All the nodes in the left subtree have values less than the node's value.
o All the nodes in the right subtree have values greater than the node's value.