Lab Record Assignment 2024
Lab Record Assignment 2024
LAB REPORT
Submitted by,
Name: Amona L Mawnai
Reg.No: B061220019
3rd Semester
Submitted to,
Ma’am Saphilarisa Marbaniang
Table of Contents
Topics Pg no
1.Arrays 2-7
(a)Array Deletion of an element
(b)Array Insertion at any position
(c)2D Array
2.Strings 7-8
(a)Lower case to uppercase
(b)Reverse String
3. Linked List 9-14
(a)Singly Linked List
(b)Doubly Linked List
4. Stacks 15-26
(a)Operation of Stacks
(b)Infix to Postfix
(c)Infix to Prefix
5.Queues 27-28
6.Trees 29-35
(a)Binary Search Tree
(b)Tree Traversal
7.Graphs 36-45
(a)BFS and DFS graph traversal
(b) Graph using Adjacency Matrix
9.Sorting 46-55
(a)Bubble Sort
(b)Insertion Sort
(c)Selection Sort
(d)Quick Sort
1 | Page
//C program array_deletion of an element//
#include <stdio.h>
int main()
{
int arr[10], n, p, i;
printf("\nEnter the size of the array: ");
scanf("%d", &n);
printf("\nEnter the %d numbers of the array: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nThe array before deletion:\n");
for (i = 0; i < n; i++)
{
printf("\t%d\t", arr[i]);
}
printf("\nEnter the index location to delete: ");
scanf("%d", &p);
int d = arr[p];
for (i = p; i < n - 1; i++)
{
arr[i] = arr[i + 1];
}
n--;
printf("\nThe new array is:\n");
for (i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
2 | Page
OUTPUT
#include <stdio.h>
int main()
{
int a[100], n, m, p;
printf("Enter the size of an array : ");
scanf("%d", &n);
printf("Enter the elements of an array : ");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The array before insertion :\n");
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\nEnter the position of an element: ");
scanf("%d", &p);
printf("Enter the element which one to enter : ");
3 | Page
scanf("%d", &m);
for (int i = n; i >= p; i--)
{
a[i] = a[i - 1];
}
n++;
a[p] = m;
printf("The elements are \n");
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
return 0;
}
OUTPUT
4 | Page
// C program to print the elements of a 2D array
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[2][2] = {12, 34, 56, 32};
int i, j;
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
printf("%d\t", arr[i][j]);
}
return 0;
}
OUTPUT
5 | Page
// C program to convert string to uppercase//
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int i;
OUTPUT
6 | Page
//Program to reverse a string//
#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
7 | Page
if (*head == NULL)
{
printf ("Linked List Empty, nothing to delete");
return;
}
int main ()
{
8 | Page
struct Node *head = NULL;
deleteStart (&head);
deleteStart (&head);
display (head);
return 0;
}
Output:
9 | Page
// C program to create a doubly linked list
#include <stdio.h>
#include <stdlib.h>
// node creation
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
10 | P a g e
// allocate memory for newNode
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
11 | P a g e
temp = temp->next;
// if del_node is the head node, point the head pointer to the next of del_node
if (*head == del_node)
*head = del_node->next;
// if del_node is not at the last node, point the prev of node next to del_node to the
previous of del_node
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
// if del_node is not the first node, point the next of the previous node to the next node of
del_node
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
int main()
{
// initialize an empty node
struct Node *head = NULL;
insertEnd(&head, 5);
insertFront(&head, 1);
insertFront(&head, 6);
insertEnd(&head, 9);
displayList(head);
displayList(head);
}
OUTPUT
13 | P a g e
// Program to show the different stack operations (Push, Pop, Peek)
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
OUTPUT
16 | P a g e
17 | P a g e
18 | P a g e
//C program Infix to Prefix Expression//
/* #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int getPrecedence(char c) {
if (c == '+' || c == '-')
return 1;
else if (c == '*' || c == '/')
return 2;
return 0;
}
if (c == ')') {
stack[++top] = c;
} else if (c == '(') {
while (top >= 0 && stack[top] != ')') {
prefix[j++] = stack[top--];
}
top--;
} else if (isOperator(c)) {
while (top >= 0 && getPrecedence(c) < getPrecedence(stack[top])) {
prefix[j++] = stack[top--];
}
stack[++top] = c;
} else {
prefix[j++] = c;
}
19 | P a g e
}
prefix[j] = '\0';
strrev(prefix);
}
int main() {
char infix[100], prefix[100];
infixToPrefix(infix, prefix);
// using stack
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
start++;
end--;
}
}
int i, j = 0;
postfix[j] = '\0';
int main()
{
char infix[MAX_SIZE], prefix[MAX_SIZE];
return 0;
}
postfix[j] = '\0';
int main()
{
char infix[MAX_SIZE], prefix[MAX_SIZE];
infixToPostfix(infix, prefix);
return 0;
}
OUTPUT
25 | P a g e
//C program Infix To Postfix//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (isOperand(currentChar))
{
postfix[j++] = currentChar;
}
else if (currentChar == '(')
{
stack[++top] = currentChar;
}
else if (currentChar == ')')
{
26 | P a g e
while (top != -1 && stack[top] != '(')
{
postfix[j++] = stack[top--];
}
top--;
}
else
{
while (top != -1 && precedence(stack[top]) >= precedence(currentChar))
{
postfix[j++] = stack[top--];
}
stack[++top] = currentChar;
}
}
postfix[j] = '\0';
}
int main()
{
char infix[100];
char postfix[100];
infixToPostfix(infix, postfix);
return 0;
}
27 | P a g e
OUPUT
// Queue implementation in C
#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int main()
{
// deQueue is not possible on empty queue
deQueue();
// enQueue 5 elements
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
28 | P a g e
enQueue(6);
display();
return 0;
}
void deQueue()
{
if (front == -1)
printf("\nQueue is Empty!!");
else
{
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
OUTPUT
30 | P a g e
// Binary Search Tree operations in C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root)
{
if (root != NULL)
{
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key)
{
// Return a new node if the tree is empty
if (node == NULL)
return newNode(key);
return node;
}
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key)
{
// Return if the tree is empty
if (root == NULL)
return root;
else
{
// If the node is with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
32 | P a g e
return temp;
}
// Driver code
int main()
{
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
33 | P a g e
OUTPUT
34 | P a g e
// Tree traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
return newNode;
}
35 | P a g e
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
OUTPUT
36 | P a g e
37 | P a g e
//Graph_Adjacency Matrix//
#include <stdio.h>
int MIN(int, int); /* Function prototype for computing the minimum among two
integers */
void main()
{
int P[5][5], SP[5][5];
int i, j, k;
P[0][0] = 8;
P[0][1] = 3;
P[0][3] = 4;
P[1][2] = 7;
P[2][0] = 4;
P[2][4] = 5;
P[3][2] = 2;
P[4][3] = 1;
OUTPUT
39 | P a g e
// BFS algorithm in C
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
// BFS algorithm
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
// Creating a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Creating a graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
41 | P a g e
// Add edge from dest to src
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}
43 | P a g e
OUTPUT
// DFS algorithm in C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int vertex;
struct node *next;
};
struct Graph
{
int numVertices;
int *visited;
44 | P a g e
struct node **adjLists;
};
// DFS algo
void DFS(struct Graph *graph, int vertex)
{
struct node *adjList = graph->adjLists[vertex];
struct node *temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
if (graph->visited[connectedVertex] == 0)
{
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
// Create a node
struct node *createNode(int v)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Create graph
struct Graph *createGraph(int vertices)
{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
int i;
for (i = 0; i < vertices; i++)
45 | P a g e
{
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph *graph, int src, int dest)
{
// Add edge from src to dest
struct node *newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
int main()
{
struct Graph *graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
46 | P a g e
printGraph(graph);
DFS(graph, 2);
return 0;
}
OUTPUT
47 | P a g e
//C program to perform Bubble Sort//
#include <stdio.h>
// functions to performed Bubble sort
void bubble_Sort(int arr[], int n)
{
inti, j;
for (i = 0; i<= n - 1; i++)
{
for (j = 0; j < n - i; j++)
{
// Swapping
if (arr[j] >arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// main functions
intmain()
{
intarr[10], i, n;
printf("enter the size of and array\t");
scanf("%d", &n);
printf("enter the numbers of and array :", n);
for (int i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}
bubble_Sort(arr, n);
printf("The new sorted array is :");
for (int i = 0; i< n; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
48 | P a g e
OUTPUT
for(inti =0;i<n;i++){
for(int j =i+1;j<n;j++) {
if(a[i]>a[j]){
int temp =a[i];
a[i]= a[j];
a[j]=temp;
}
}
}
printf("The new sorted array is :\n ");
49 | P a g e
for(int i=0;i<n;i++){
printf("%d\t",a[i]);
}
return 0;
}
OUTPUT
int main()
{
int arr[10], n;
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("Enter the %d numbers of the array : ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
insertionSort(arr, n);
return 0;
}
Output:
51 | P a g e
//C program to perform Quick sort//
#include <stdio.h>
intpartition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] < pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
Int main()
{
Int arr[10], n;
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("Enter the %d numbers of the array : ", n);
for (int i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}
52 | P a g e
quickSort(arr, 0, n - 1);
for (inti = 0; i< n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT
53 | P a g e
// C program to perform merge sort//
#include <stdio.h>
OUTPUT
55 | P a g e
56 | P a g e