Data Structure University Practicals 007.
Data Structure University Practicals 007.
Program:
#include <stdio.h>
#include <stdlib.h>
Typedef struct {
Int items[SIZE];
} CircularQueue;
q->front = -1;
q->rear = -1;
If (isFull(q)) {
Return;
If (isEmpty(q)) {
q->front = 0;
q->items[q->rear] = value;
If (isEmpty(q)) {
Return -1;
q->front = -1;
q->rear = -1;
} else {
Return value;
If (isEmpty(q)) {
Printf(“Queue is empty.\n”);
Return;
Int I = q->front;
While (1) {
Printf(“%d “, q->items[i]);
If (I == q->rear) break;
I = (I + 1) % SIZE;
Printf(“\n”);
Int main() {
CircularQueue q;
initializeQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q);
dequeue(&q);
dequeue(&q);
display(&q);
enqueue(&q, 60);
enqueue(&q, 70);
display(&q);
return 0;
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
// Function to create a new node
If (!newNode) {
Exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
If (!head) {
Return newNode; // If the list is empty, the new node becomes the head
Temp = temp->next;
Return head;
If (!head) {
Return;
While (temp) {
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
While (1) {
Printf(“\nMenu:\n”);
Printf(“3. Exit\n”);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Scanf(“%d”, &data);
Break;
Case 2:
displayList(head);
break;
case 3:
exit(0);
default:
Return 0;
Output:
Menu:
3. Exit
Menu:
3. Exit
Menu:
3. Exit
List contents: 10 20
Menu:
3. Exit
Program :
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
If (!newNode) {
Exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
Return newNode; // If the list is empty, the new node becomes the head
Temp = temp->next;
Return head;
If (!head) {
Return NULL;
Head = temp->next;
Free(temp);
Return head;
}
// Search for the key in the list
Prev = temp;
Temp = temp->next;
If (!temp) {
Return head;
Prev->next = temp->next;
Free(temp);
Return head;
If (!head) {
Return;
While (temp) {
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
While (1) {
Printf(“\nMenu:\n”);
Printf(“4. Exit\n”);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Scanf(“%d”, &data);
Case 2:
Scanf(“%d”, &key);
Break;
Case 3:
displayList(head);
break;
case 4:
exit(0);
default:
Return 0;
Output:
Menu:
4. Exit
Enter your choice: 1
Menu:
4. Exit
Menu:
4. Exit
List contents: 10 20
Menu:
4. Exit
4. Exit
List contents: 20
Program:
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
Exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
If (!head) {
Return newNode; // If the list is empty, the new node becomes the head
Temp = temp->next;
Return head;
If (!head) {
Head = temp->next;
Free(temp);
Return head;
Prev = temp;
Temp = temp->next;
If (!temp) {
Return head;
Prev->next = temp->next;
Free(temp);
Return head;
If (!head) {
Return;
While (temp) {
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
While (1) {
Printf(“\nMenu:\n”);
Printf(“1. Add element to the list\n”);
Printf(“4. Exit\n”);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Scanf(“%d”, &data);
Break;
Case 2:
Scanf(“%d”, &key);
break;
case 3:
displayList(head);
break;
case 4:
exit(0);
default:
Return 0;
Output:
Menu:
4. Exit
Menu:
4. Exit
Menu:
List contents: 20
Menu:
4. Exit
List contents: 20
Program:
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
// Function to create a new node
If (!newNode) {
Exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
return top;
If (!top) {
*poppedData = temp->data;
Free(temp);
Return top;
If (!top) {
Return top->data;
If (!top) {
Return;
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
While (1) {
Printf(“\nMenu:\n”);
Printf(“5. Exit\n”);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Scanf(“%d”, &data);
Case 2:
If (poppedData != -1) {
Break;
Case 3:
Data = peek(top);
If (data != -1) {
Break;
Case 4:
displayStack(top);
break;
case 5:
exit(0);
default:
Return 0;
}
Output:
Menu:
5. Exit
Menu:
5. Exit
Menu:
5. Exit
Enter your choice: 4
Stack contents: 20 10
Menu:
5. Exit
Menu:
5. Exit
Stack contents: 10
#include <stdio.h>
#include <stdlib.h>
} PolyNode;
If (!newNode) {
Exit(1);
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
If (!head) {
Return newNode;
While (temp->next) {
Temp = temp->next;
Temp->next = newNode;
Return head;
If (!head) {
Return;
While (temp) {
If (temp->next) {
Printf(“ + “);
Temp = temp->next;
}
Printf(“\n”);
If (poly1->exponent == poly2->exponent) {
Poly1 = poly1->next;
Poly2 = poly2->next;
Poly1 = poly1->next;
} else {
Poly2 = poly2->next;
While (poly1) {
Result = insertTerm(result, poly1->coefficient, poly1->exponent);
Poly1 = poly1->next;
While (poly2) {
Poly2 = poly2->next;
Return result;
While (temp1) {
While (temp2) {
// Multiply terms and insert the result into the result polynomial
Temp2 = temp2->next;
Temp1 = temp1->next;
}
Return result;
Int main() {
displayPolynomial(poly1);
printf(“Second Polynomial: “);
displayPolynomial(poly2);
displayPolynomial(sum);
displayPolynomial(product);
return 0;
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
Int top;
Char items[MAX];
} Stack;
Void initStack(Stack* s) {
s->top = -1;
Int isEmpty(Stack* s) {
Int isFull(Stack* s) {
If (isFull(s)) {
Printf(“Stack overflow!\n”);
Return;
s->items[++(s->top)] = item;
Char pop(Stack* s) {
If (isEmpty(s)) {
Printf(“Stack underflow!\n”);
Return -1;
Return s->items[(s->top)--];
Char peek(Stack* s) {
If (!isEmpty(s)) {
Return s->items[s->top];
Return -1;
Return 0;
Int isOperator(char c) {
Stack s;
initStack(&s);
int I = 0, j = 0;
Postfix[j++] = current;
Push(&s, current);
// If the character is ‘)’, pop from the stack until ‘(‘ is found
Postfix[j++] = pop(&s);
} else if (isOperator(current)) {
Postfix[j++] = pop(&s);
Push(&s, current);
I++;
While (!isEmpty(&s)) {
Postfix[j++] = pop(&s);
}
Int main() {
Scanf(“%s”, infix);
infixToPostfix(infix, postfix);
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
Int data;
} Node;
If (!newNode) {
Exit(1);
newNode->data = data;
return newNode;
If (root == NULL) {
Return createNode(data);
Return root;
Return root;
If (root != NULL) {
Int main() {
printf(“\n”);
If (foundNode != NULL) {
} else {
Return 0;
Output:
50
/ \
30 70
/ \ / \
20 40 60 80
7. AVL trees and its operations
Program:
#include <stdio.h>
#include <stdlib.h>
Int data;
Int height;
} Node;
If (!newNode) {
Exit(1);
newNode->data = data;
return newNode;
}
// Function to get the height of a node
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
Return x;
}
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
Return y;
If (root == NULL) {
Return createNode(data);
}
If (data < root->data) {
} else {
Return rightRotate(root);
Return leftRotate(root);
}
// Left-Right (LR) Case
Root->left = leftRotate(root->left);
Return rightRotate(root);
Root->right = rightRotate(root->right);
Return leftRotate(root);
Return root;
If (root != NULL) {
inorderTraversal(root->left);
printf(“%d “, root->data);
inorderTraversal(root->right);
inorderTraversal(root);
printf(“\n”);
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
*a = *b;
*b = temp;
Largest = left;
If (largest != i) {
Swap(&arr[i], &arr[largest]);
Heapify(arr, n, largest);
// Increase size of heap and add the new element at the end
(*n)++;
Int I = *n – 1;
Arr[i] = key;
I = (I – 1) / 2;
If (*n <= 0) {
Printf(“Heap is empty!\n”);
Return -1;
(*n)--;
Return root;
Printf(“%d “, arr[i]);
Printf(“\n”);
Int main() {
Int heap[100]; // Array to represent the heap
Display(heap, n);
Display(heap, n);
Return 0;
Output:
Heap after insertions: 30 20 15 10 5
9. Dijkstra’s algorithm
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 9
// Function to find the vertex with the minimum distance that hasn’t been
visited
Min = dist[v];
Min_index = v;
}
Return min_index;
Int dist[V]; // dist[i] will hold the shortest distance from start to i
Dist[i] = INT_MAX;
Visited[i] = 0;
Parent[i] = -1;
Dist[start] = 0;
Visited[u] = 1;
Parent[v] = u;
Printf(“%d\t\t%d\t\t\t”, I, dist[i]);
Int j = I;
J = parent[j];
Printf(“%d\n”, start);
}
Int main() {
Int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
Dijkstra(graph, 0);
Return 0;
Output:
0 0 0
1 4 1 <- 0
2 12 2 <- 5 <- 0
4 21 4 <- 5 <- 0
5 11 5 <- 0
7 8 7 <- 0
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 9
// Function to find the vertex with the minimum key value that hasn’t been
included in MST
Min = key[v];
Min_index = v;
Return min_index;
Key[i] = INT_MAX;
mstSet[i] = 0;
// Pick the vertex with the minimum key value that is not yet included in
MST
mstSet[u] = 1;
// Update the key and parent values of the adjacent vertices of the
picked vertex
// Update the key only if the edge (u, v) is smaller than the current
key[v]
Key[v] = graph[u][v];
Parent[v] = u;
Printf(“Edge\tWeight\n”);
}
Int main() {
Int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
primMST(graph);
return 0;
Output:
Edge Weight
0–1 4
1–2 8
2–5 4
5–6 2
6–7 1
7–8 7
2–3 7
3–4 9
Program:
#include <stdio.h>
If (arr[i] == target) {
Int main() {
Scanf(“%d”, &target);
If (result != -1) {
} else {
Return 0;
Output:
Program:
#include <stdio.h>
Int key, j;
J = I – 1;
// Move elements of arr[0..i-1] that are greater than key, to one position
ahead
J = j – 1;
}
// Function to print the array
Printf(“%d “, arr[i]);
Printf(“\n”);
Int main() {
printArray(arr, n);
insertionSort(arr, n);
printArray(arr, n);
return 0;
Output:
Original array: 12 11 13 5 6
Sorted array: 5 6 11 12 13
13. merge sort
Program:
#include <stdio.h>
int I = 0, j = 0, k = left;
Arr[k] = leftArr[i];
I++;
} else {
Arr[k] = rightArr[j];
J++;
K++;
Arr[k] = leftArr[i];
I++;
K++;
Arr[k] = rightArr[j];
J++;
K++;
Printf(“%d “, arr[i]);
Printf(“\n”);
Int main() {
printArray(arr, size);
return 0;
Output:
Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
Program:
#include <stdio.h>
Int heap[MAX];
*a = *b;
*b = temp;
Void heapify(int i) {
Int largest = I;
Largest = left;
Largest = right;
}
// If the largest is not root, swap and continue heapifying
If (largest != i) {
Swap(&heap[i], &heap[largest]);
Heapify(largest);
If (heapSize == MAX) {
Printf(“Heap is full\n”);
Return;
Heap[heapSize] = value;
Int I = heapSize;
heapSize++;
// Function to extract the maximum element (root) from the max heap
Int extractMax() {
If (heapSize <= 0) {
Printf(“Heap is empty\n”);
Return -1;
If (heapSize == 1) {
heapSize--;
return heap[0];
heapSize--;
Heapify(0);
Return root;
Void printHeap() {
Printf(“%d “, heap[i]);
}
Printf(“\n”);
Int main() {
Insert(10);
Insert(20);
Insert(30);
Insert(5);
Insert(15);
printHeap();
printHeap();
return 0;
Output:
Max Heap: 30 20 10 5 15
Extracted max: 30
Program:
#include <stdio.h>
#include <stdlib.h>
Struct Node {
};
// Function to create a new node with the given coefficient and exponent
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}
// Function to insert a term in the polynomial (in descending order of
exponent)
newNode->next = *poly;
*poly = newNode;
} else {
Temp = temp->next;
Temp->next->coeff += coeff;
} else {
newNode->next = temp->next;
temp->next = newNode;
If (poly == NULL) {
Printf(“Polynomial is empty.\n”);
Return;
Printf(“+”);
Poly = poly->next;
Printf(“\n”);
poly1 = poly1->next;
poly2 = poly2->next;
} else {
Poly1 = poly1->next;
Poly2 = poly2->next;
poly1 = poly1->next;
poly2 = poly2->next;
Return result;
Int main() {
insertTerm(&poly1, 3, 3);
insertTerm(&poly1, 2, 2);
insertTerm(&poly1, 5, 0);
insertTerm(&poly2, 5, 3);
insertTerm(&poly2, 4, 1);
printf(“Polynomial 1: “);
printPolynomial(poly1);
printf(“Polynomial 2: “);
printPolynomial(poly2);
printPolynomial(result);
return 0;
}
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 50
Int stack[MAX];
} else {
Printf(“Stack Overflow\n”);
Int pop() {
If (top >= 0) {
Return stack[top--];
} else {
Printf(“Stack Underflow\n”);
Return -1;
Int I = 0;
If (isdigit(expression[i])) {
Else {
Int operand2 = pop();
Switch (expression[i]) {
Case ‘+’:
Push(operand1 + operand2);
Break;
Case ‘-‘:
Push(operand1 – operand2);
Break;
Case ‘*’:
Push(operand1 * operand2);
Break;
Case ‘/’:
Push(operand1 / operand2);
Break;
Default:
Return -1;
I++;
Int main() {
Char expression[MAX];
If (result != -1) {
Return 0;
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
// Queue structure
Struct Queue {
Int items[MAX];
Int front;
Int rear;
};
q->front = -1;
q->rear = -1;
Return 1;
}
Return 0;
If (q->front == -1) {
Return 1;
Return 0;
If (isFull(q)) {
Return;
If (q->front == -1) {
q->items[q->rear] = value;
}
// Function to dequeue an element
If (isEmpty(q)) {
Return -1;
If (q->front == q->rear) {
} else {
Return item;
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return -1;
Return q->items[q->front];
}
// Function to display the elements in the queue
If (isEmpty(q)) {
Printf(“Queue is empty\n”);
Return;
Int I = q->front;
While (I != q->rear) {
Printf(“%d “, q->items[i]);
I = (I + 1) % MAX;
Printf(“%d\n”, q->items[q->rear]);
Int main() {
Struct Queue q;
initializeQueue(&q);
Enqueue(&q, 10);
Enqueue(&q, 20);
Enqueue(&q, 30);
Enqueue(&q, 40);
Display(&q);
Display(&q);
Display(&q);
Return 0;
Output:
Queue contents: 10 20 30 40 50
Dequeued: 10
Queue contents: 20 30 40 50
60 enqueued to the queue
Queue contents: 20 30 40 50 60
Front element: 20
Program:
#include <stdio.h>
#include <stdlib.h>
Struct Node {
};
newNode->data = data;
newNode->next = NULL;
return newNode;
}
If (*head == NULL) {
*head = newNode; // If the list is empty, make the new node the head
Return;
Return;
Prev = temp;
Temp = temp->next;
If (temp == NULL) {
Return;
Return;
Temp = temp->next;
Printf(“NULL\n”);
Int main() {
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
displayList(head);
// Delete a node
displayList(head);
return 0;
Output:
After deleting node with value 20: 10 -> 30 -> 40 -> NULL
Program:
#include <stdio.h>
#include <stdlib.h>
};
newNode->data = data;
newNode->next = NULL;
return newNode;
If (*top == NULL) {
}
Struct Node* temp = *top;
Return poppedData;
If (top == NULL) {
Printf(“Stack is empty.\n”);
Return -1;
If (top == NULL) {
Printf(“Stack is empty.\n”);
Return;
}
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
Push(&stack, 10);
Push(&stack, 20);
Push(&stack, 30);
Push(&stack, 40);
Return 0;
Output:
Stack elements: 40 30 20 10
Peek: 40
Popped: 40
Stack elements: 30 20 10
Popped: 30
Stack elements: 20 10
Stack elements: 50 20 10
20. implementation of polynomial manipulation
Program:
#include <stdio.h>
#include <stdlib.h>
Struct Node {
};
newNode->coefficient = coeff;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
}
// Function to insert a term in the polynomial in descending order of
exponents
newNode->next = *poly;
*poly = newNode;
Return;
Temp = temp->next;
Temp->next->coefficient += coeff;
Free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
Printf(“0\n”);
Return;
Printf(“ + “);
Temp = temp->next;
Printf(“\n”);
temp1 = temp1->next;
temp2 = temp2->next;
} else {
If (sum != 0) {
Temp1 = temp1->next;
Temp2 = temp2->next;
temp1 = temp1->next;
temp2 = temp2->next;
Return result;
temp1 = temp1->next;
temp2 = temp2->next;
} else {
If (diff != 0) {
Temp1 = temp1->next;
Temp2 = temp2->next;
temp1 = temp1->next;
temp2 = temp2->next;
Return result;
temp2 = temp2->next;
Temp1 = temp1->next;
Return result;
Int main() {
Struct Node* poly1 = NULL;
insertTerm(&poly1, 5, 3);
insertTerm(&poly1, 4, 2);
insertTerm(&poly1, 2, 1);
insertTerm(&poly1, 3, 0);
insertTerm(&poly2, 3, 3);
insertTerm(&poly2, 2, 2);
insertTerm(&poly2, 1, 1);
insertTerm(&poly2, 1, 0);
displayPolynomial(poly1);
displayPolynomial(poly2);
displayPolynomial(result);
// Subtract the polynomials
displayPolynomial(result);
displayPolynomial(result);
return 0;
Output: