Program:: Experiment-1 AIM-Program in C For LINEAR SEARCH
Program:: Experiment-1 AIM-Program in C For LINEAR SEARCH
int main()
{
scanf("%d", &arr[i]);
if(i == size){
printf("%d NOT found in the array", element);
return 0;
}
RESULT- Code executed successfully.
Output:
EXPERIMENT-2
AIM- Program in C for BINARY SEARCH.
Program:
#include <stdio.h>
int l = 0, r = n - 1, mid;
while (l <= r)
{
mid = (l + r) / 2;
if (arr[mid] == element)
return mid;
r = mid - 1;
else
l = mid + 1;
return -1;
int main()
scanf("%d", &size);
printf("Enter elements of array in ascending order: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
scanf("%d", &element);
if (index == -1)
{
else
return 0;
Output)
EXPERIMENT-3
AIM- program in C for BUBBLE SORT
Program:
#include <stdio.h>
int temp;
temp = arr[j];
arr[j + 1] = temp;
}
int main()
scanf("%d", &size);
printf("Enter elements of array: "); for (int i = 0; i <
size; i++)
{
scanf("%d", &arr[i]);
bubble_sort(arr, size);
return 0;
}
Output)
EXPERIMENT-4
AIM- program in C for INSERTION SORT
Program:
#include<stdio.h>
int j, temp;
temp = arr[i];
j = i-1;
arr[j+1] = arr[j];
j--;
arr[j+1] = temp;
int main(){
scanf("%d", &arr[i]);
insertion_sort(arr, size);
return 0;
Output)
EXPERIMENT-5
AIM- Program in C for MATRIX MULTIPLICATION.
Program:
#include <stdio.h> int
main()
{
int m, n, p, q;
if (n != p)
return 0;
}
scanf("%d", &arr1[i][j]);
}
scanf("%d", &arr2[i][j]);
{
for (int j = 0; j < q; j++)
printf("\n");
return 0;
}
RESULT- Code executed successfully.
Output-1)
Output-2)
EXPERIMENT-6
AIM- Program in C for CREATION and TRAVERSAL of
SINGLY LINKED LIST.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
struct Node *creation_LL(struct Node *head, struct Node*newnode, struct Node *temp)
int n; head = 0;
printf("How many nodes you want to create in linked list? ");
scanf("%d", &n);
if (!n)
return head;
else
{
if (head == 0)
temp->next = newnode;
temp = newnode;
}
}
}
return head;
temp = head;
printf("Linked list created :- \n");
while (temp != 0)
{
if (temp->next == 0)
printf("%d", temp->data);
temp = temp->next;
else
temp = temp->next;
}
int main()
Output)
EXPERIMENT-7
AIM- program for INSERTION in SINGLY LINKED LIST
at beginning , end , at a specific position
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
struct Node *creation_LL(struct Node *head, struct Node*newnode, struct Node *temp)
int n; head = 0;
printf("How many nodes you want to create in linked list? ");
scanf("%d", &n);
if (!n)
return head;
}
else
else
{
temp->next = newnode;
temp = newnode;
}
}
return head;
struct Node *insertion_at_begin(struct Node *head, struct Node *newnode, struct Node*temp)
{
scanf("%d", &newnode->data);
newnode->next = head;
head = newnode;
printf("New linked list after inserting node at the beginning:- \n");
traversing_LL(head, temp);
return head;
}
struct Node *insertion_at_end(struct Node *head, struct Node *newnode, struct Node*temp)
{
scanf("%d", &newnode->data);
newnode->next = 0; temp = head;
while (temp->next != 0)
temp = temp->next;
}
temp->next = newnode;
scanf("%d", &position);
if (position == 1)
else
{
temp = head;
while (temp->next != 0)
count++;
if (position == count)
{
newnode = (struct Node *)malloc(sizeof(struct Node));
printf("Enter data for newnode: ");
scanf("%d", &newnode->data);
newnode->next = temp->next;
temp->next = newnode;
counter = 1;
printf("\nNew linked list after inserting node at the
specified position:- \n");
traversing_LL(head, temp);
break;
}
else
temp = temp->next;
}
if (position == count + 1)
if (!counter)
printf("\nIncorrect position!");
return head;
}
return head;
temp = head;
while (temp!= 0)
if (temp->next == 0)
printf("%d", temp->data);
temp = temp->next;
}
else
temp = temp->next;
}
int main()
{
int choice; char ans = 'y';
struct Node *head, *newnode, *temp;
head = creation_LL(head, newnode, temp);
printf("Linked List created :- \n");
traversing_LL(head, temp);
while (ans == 'y')
{
printf("\nWhere do you want to insert a new node?\n1)Beginning\n2)End\n3)Specific
Position\n(Enter your choice in numeric) : ");
scanf("%d", &choice);
if (choice== 1)
head = insertion_at_begin(head, newnode, temp);
else
printf("Invalid choice!");
return 0;
}
RESULT- Code executed successfully.
Output)
EXPERIMENT-8
AIM- Implementing stack using array
Program:
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef struct {
int arr[MAX_SIZE];
int top;
} Stack;
void initialize(Stack *stack) {
stack->top = -1;
}
bool isEmpty(Stack *stack) {
return stack->top == -1;
}
bool isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->arr[++stack->top] = value;
printf("Pushed %d onto the stack\n", value);
}
int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
int popped = stack->arr[stack->top];
stack->top--;
printf("Popped %d from the stack\n", popped);
return popped;
}
int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}
int main() {
Stack stack;
initialize(&stack);
each push
push(&stack, 3);
printf("Top element: %d\n", peek(&stack));
push(&stack, 5);
printf("Top element: %d\n", peek(&stack));
push(&stack, 2);
printf("Top element: %d\n", peek(&stack));
push(&stack, 8);
printf("Top element: %d\n", peek(&stack));
each pop
while (!isEmpty(&stack)) {
printf("Top element: %d\n", peek(&stack));
printf("Popped element: %d\n", pop(&stack));
}
return 0;
}
RESULT- Code executed successfully.
Output)
EXPERIMENT-9
AIM- Implementing stack using linklist
Program:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
new_node->data = new_data;
new_node->next = NULL;
return new_node;
Node* head;
} Stack;
stack->head = NULL;
if (!new_node) {
printf("\nStack Overflow");
return;
}
new_node->next = stack->head;
stack->head = new_node;
if (isEmpty(stack)) {
printf("\nStack Underflow\n");
return;
} else {
stack->head = stack->head->next;
free(temp);
if (!isEmpty(stack))
return stack->head->data;
else {
printf("\nStack is empty");
return INT_MIN;
int main() {
Stack stack;
initializeStack(&stack);
push(&stack, 11);
push(&stack, 22);
push(&stack, 33);
push(&stack, 44);
pop(&stack);
pop(&stack);
return 0;
}
Output)
EXPERIMENT-10
#include <stdio.h>
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} Queue;
void initializeQueue(Queue* q) {
q->front = -1;
q->rear = 0;
bool isEmpty(Queue* q) {
bool isFull(Queue* q) {
if (isFull(q)) {
printf("Queue is full\n");
return;
q->items[q->rear] = value;
q->rear++;
void dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
q->front++;
int peek(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
void printQueue(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
printf("\n");
int main() {
Queue q;
initializeQueue(&q);
enqueue(&q, 10);
printQueue(&q);
enqueue(&q, 20);
printQueue(&q);
enqueue(&q, 30);
printQueue(&q);
dequeue(&q);
printQueue(&q);
return 0;
}
Output)
EXPERIMENT-11
#include <stdlib.h>
int data;
} Node;
new_node->data = new_data;
new_node->next = NULL;
return new_node;
} Queue;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
return q;
int isEmpty(Queue* q) {
if (q->rear == NULL) {
q->rear->next = new_node;
q->rear = new_node;
void dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue Underflow\n");
return;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
int getFront(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return INT_MIN;
return q->front->data;
int getRear(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return INT_MIN;
return q->rear->data;
}
int main() {
Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
dequeue(q);
dequeue(q);
enqueue(q, 30);
enqueue(q, 40)
enqueue(q, 50);
dequeue(q);
return 0;
}
Output)
EXPERIMENT-12
AIM- To implement a binary tree using an array.
Program:
#include <stdio.h>
#define MAX 15
} else {
tree[position] = data;
void display() {
int i;
printf("Tree elements:\n");
if (tree[i] != 0) {
int main() {
display();
return 0;
}
Output)
EXPERIMENT-13
AIM- To implement a doubly linked list with insertion,
deletion, and display operations.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
if (head != NULL) {
head->prev = newNode;
head = newNode;
if (temp == NULL) {
printf("List is empty!\n");
return;
if (temp == NULL) {
return;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
head = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
free(temp);
printf("Element deleted!\n");
void display() {
ptr = ptr->next;
printf("NULL\n");
int main() {
printf("\nMenu:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insert(value);
break;
case 2:
scanf("%d", &value);
delete(value);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
return 0;
}
Output)
EXPERIMENT-14
Aim: To implement a graph using an adjacency matrix in C.
Program:
#include <stdio.h>
#define MAX 10
int adjMatrix[MAX][MAX];
int n;
n = vertices;
adjMatrix[i][j] = 0;
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
void displayGraph() {
printf("Adjacency Matrix:\n");
printf("\n");
}
}
int main() {
scanf("%d", &vertices)
initGraph(vertices);
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
addEdge(u, v);
} else {
printf("Invalid vertices!\n");
break;
case 2:
displayGraph();
break;
case 3:
return 0;
default:
printf("Invalid choice!\n");
} } return 0}
RESULT- Code executed successfully.
Output)
EXPERIMENT-15
Aim: To implement Prim's algorithm for finding the Minimum
Spanning Tree (MST) of a graph
Program:
#include <stdio.h>
#include <limits.h>
#define MAX 10
int graph[MAX][MAX];
int n;
min = key[v];
minIndex = v;
return minIndex;
void primMST() {
int parent[MAX];
int key[MAX];
int mstSet[MAX];
key[i] = INF;
mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < n - 1; count++) {
mstSet[u] = 1;
parent[v] = u;
key[v] = graph[u][v];
printf("Edge \tWeight\n");
int main() {
scanf("%d", &vertices);
n = vertices;
graph[i][j] = 0;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
graph[u][v] = w;
graph[v][u] = w;
} else {
printf("Invalid vertices!\n");
break;
case 2:
primMST();
break;
case 3:
return 0;
default:
printf("Invalid choice!\n");
return 0;
}
RESULT- Code executed successfully.
Output)