data_and_file_structure_implementation_and_theory
data_and_file_structure_implementation_and_theory
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
// stack as 0
stack->capacity = capacity;
stack->top = -1;
return stack;
if (isFull(stack))
return;
stack->array[++stack->top] = item;
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
int main()
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int* queue;
};
// Function to initialize the queue
q->capacity = capacity;
q->front = 0;
q->rear = -1;
return q;
if (q->rear == q->capacity - 1) {
printf("Queue is full\n");
return;
q->queue[++q->rear] = data;
return;
// Shift all elements from index 1 till rear to the left by one
// Decrement rear
q->rear--;
printf("Queue is Empty\n");
return;
printf("\n");
}
// Function to print the front of the queue
if (q->rear == -1) {
printf("Queue is Empty\n");
return;
// Driver code
int main() {
display(q);
enqueue(q, 20);
enqueue(q, 30);
enqueue(q, 40);
enqueue(q, 50);
display(q);
display(q);
// Dequeue elements
dequeue(q);
dequeue(q);
display(q);
enqueue(q, 60);
display(q);
front(q);
free(q->queue);
free(q);
return 0;
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data;
} Node;
Node list[MAX_SIZE];
int freeList = 0;
void initList() {
list[i].next = i + 1;
}
// Allocate a new node and return its index
int allocateNode() {
if (freeList == -1) {
return allocatedIndex;
list[index].next = freeList;
freeList = index;
list[newNodeIndex].data = data;
list[newNodeIndex].data = data;
if (current == -1) {
list[0].next = newNodeIndex; // If the list is empty, new node becomes the head
} else {
current = list[current].next;
current = list[current].next;
}
if (current == -1) {
return;
list[newNodeIndex].data = data;
list[newNodeIndex].next = list[current].next;
list[current].next = newNodeIndex;
prev = current;
current = list[current].next;
if (current == -1) {
printf("Error: Node with data %d not found.\n", data);
return;
if (prev == -1) {
list[0].next = list[current].next;
} else {
list[prev].next = list[current].next;
if (list[current].data == data) {
current = list[current].next;
}
// Traverse and print the linked list
void printList() {
if (current == -1) {
printf("List is empty.\n");
return;
current = list[current].next;
printf("NULL\n");
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);
insertAtBegin(5);
// Insert after a specific node
insertAfter(20, 25);
printList();
if (search != -1) {
} else {
// Delete a node
deleteNode(20);
printList();
return 0;
}
Question 4. Implementation of tree in c with all operations
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
if (!newNode) {
return NULL;
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
return root;
return root;
}
// Function to find the minimum value node in a tree
current = current->left;
return current;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
free(root);
return temp;
// Node with two children: Get the inorder successor (smallest in the right subtree)
root->data = temp->data;
return root;
if (root != NULL) {
if (root != NULL) {
if (root != NULL) {
if (root == NULL) {
}
int leftHeight = height(root->left); // Get the height of the left subtree
// Return the maximum height between left and right subtree + 1 for the current node
if (root == NULL) {
// Return 1 (current node) + size of the left subtree + size of the right subtree
int main() {
// Traversals
printf("\n");
preorder(root); // Should print the root first, then left and right
printf("\n");
printf("\n");
if (foundNode != NULL) {
} else {
// Deleting a node
inorder(root);
printf("\n");
return 0;
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is
named AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node is
associated with a balance factor which is calculated by subtracting the height of its right
sub-tree from that of its left sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree
contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than
the right sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor associated
with each node is in between -1 and +1. therefore, it is an example of AVL tree.
Complexity
SN Operation Description
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1.
There are basically four types of rotations which are as follows:
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR and
RL are double rotations. For a tree to be unbalanced, minimum height must be at least
2, Let us understand each rotation
1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right subtree of the
right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation,
which is applied on the edge below a node having balance factor -2
In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the
left subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is
applied on the edge below a node having balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation
Double rotations are bit tougher than single rotation which has already explained above.
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and
then LL rotation is performed on full tree, by full tree we mean the first node from the
path of inserted node whose balance factor is other than -1, 0, or 1.
Advertisement
State Action
4. RL Rotation
As already discussed, that double rotations are bit tougher than single rotation which
has already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL
rotation is performed on subtree and then RR rotation is performed on full tree, by full
tree we mean the first node from the path of inserted node whose balance factor is other
than -1, 0, or 1.
State Action
A node B has been inserted into the
left subtree of C the right subtree of A,
because of which A has become an
unbalanced node having balance
factor - 2. This case is RL rotation
where: Inserted node is in the left
subtree of right subtree of A
As RL rotation = LL rotation + RR
rotation, hence, LL (clockwise) on
subtree rooted at C is performed first.
By doing RR rotation, node C has
become the right subtree of B.
Graph
A graph can be defined as group of vertices and edges that are used to connect these
vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any
complex relationship among them instead of having parent child relationship.
Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are used to connect these vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following figure.
Advertisement
In a directed graph, edges form an ordered pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is called initial node while node B is called
terminal node.
Path
A path can be defined as the sequence of nodes that are followed in order to reach
some terminal node V from the initial node U.
Closed Path
A path will be called as closed path if the initial node is same as terminal node. A path
will be closed path if V0=VN.
Simple Path
If all the nodes of the graph are distinct with an exception V0=VN, then such path P is
called as closed simple path.
Cycle
A cycle can be defined as the path which has no repeated edges or vertices except the
first and last vertices.
Connected Graph
A connected graph is the one in which some path exists between every two vertices (u,
v) in V. There are no isolated nodes in connected graph.
Complete Graph
A complete graph is the one in which every node is connected with all other nodes. A
complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight.
The weight of an edge e can be given as w(e) which must be a positive (+) value
indicating the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.
Loop
An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as
neighbours or adjacent nodes.
Difference between BFS and DFS
Parameters BFS DFS
Conceptual BFS builds the tree level by DFS builds the tree sub-tree by
Difference level. sub-tree.