0% found this document useful (0 votes)
2 views46 pages

Program:: Experiment-1 AIM-Program in C For LINEAR SEARCH

The document contains a series of C programming experiments focusing on various algorithms and data structures, including linear search, binary search, bubble sort, insertion sort, matrix multiplication, singly linked list creation and traversal, stack implementation using arrays and linked lists, and queue implementation. Each experiment includes the aim, program code, and results indicating successful execution. The document serves as a practical guide for implementing fundamental algorithms and data structures in C.

Uploaded by

aryangoyat2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views46 pages

Program:: Experiment-1 AIM-Program in C For LINEAR SEARCH

The document contains a series of C programming experiments focusing on various algorithms and data structures, including linear search, binary search, bubble sort, insertion sort, matrix multiplication, singly linked list creation and traversal, stack implementation using arrays and linked lists, and queue implementation. Each experiment includes the aim, program code, and results indicating successful execution. The document serves as a practical guide for implementing fundamental algorithms and data structures in C.

Uploaded by

aryangoyat2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

EXPERIMENT-1

AIM- Program in C for LINEAR SEARCH.


Program:
#include <stdio.h>

int main()
{

int arr[100], size, i, element;

printf("Enter size of array: ");


scanf("%d",&size);
printf("Enter elements of array: ");
for (i = 0; i < size; i++)
{

scanf("%d", &arr[i]);

printf("Enter element to search: ");


scanf("%d",&element);
for(i=0; i<size; i++)
{
if(arr[i] == element)
{
printf("%d is found at index %d",element, i); break;
}

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 binary_search(int arr[], int n, int element)

int l = 0, r = n - 1, mid;

while (l <= r)
{

mid = (l + r) / 2;

if (arr[mid] == element)

return mid;

else if (arr[mid] > element)

r = mid - 1;

else

l = mid + 1;

return -1;

int main()

int arr[100], size, i, element;

printf("Enter size of array: ");

scanf("%d", &size);
printf("Enter elements of array in ascending order: ");
for (i = 0; i < size; i++)
{

scanf("%d", &arr[i]);

printf("Enter element to be searched in array: ");

scanf("%d", &element);

int index = binary_search(arr, size, element);

if (index == -1)
{

printf("%d NOT found in array", element);

else

printf("%d is found at index %d", element, index);


}

return 0;

RESULT- Code executed successfully.

Output)
EXPERIMENT-3
AIM- program in C for BUBBLE SORT
Program:
#include <stdio.h>

void bubble_sort(int arr[], int n)

int temp;

for (int i = 0; i < n - 1; i++)

for (int j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

temp = arr[j];

arr[j] = arr[j+ 1];

arr[j + 1] = temp;
}

printf("After BUBBLE Sort :-\n");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);}

int main()

int arr[100], size;

printf("Enter size of array: ");

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;
}

RESULT- Code executed successfully.

Output)
EXPERIMENT-4
AIM- program in C for INSERTION SORT
Program:
#include<stdio.h>

void insertion sort(int arr[], int n)

int j, temp;

for(int i = 1; i<n; i++)

temp = arr[i];
j = i-1;

while(j>=0 && arr[j]>temp)

arr[j+1] = arr[j];

j--;

arr[j+1] = temp;

printf("After INSERTION Sort :-\n");

for(int i = 0; i<n; i++){

printf("%d ", arr[i]);

int main(){

int arr[100], size, i; printf("Enter size of


array: ");
scanf("%d", &size);
printf("Enter elements of array”);
for(i=0; i<size; i++){

scanf("%d", &arr[i]);

insertion_sort(arr, size);

return 0;

RESULT- Code executed successfully.

Output)
EXPERIMENT-5
AIM- Program in C for MATRIX MULTIPLICATION.
Program:
#include <stdio.h> int
main()
{

int m, n, p, q;

printf("Enter no of rows and columns for 1st matrix: ");

scanf("%d %d", &m, &n);

printf("Enter no of rows and columns for 2nd matrix: ");

scanf("%d %d", &p, &q);

if (n != p)

printf("Matrix Multiplication NOT possible");

return 0;
}

int arr1[m][n], arr2[p][q], arr3[m][q];

printf("Enter values for matrix 1: ");

for (int i = 0; i < m; i++)


{

for (int j = 0; j < n; j++)

scanf("%d", &arr1[i][j]);
}

printf("Enter values for matrix 2: ");

for (int i = 0; i < p; i++)


{

for (int j = 0; j < q; j++)


{

scanf("%d", &arr2[i][j]);

for (int i = 0; i < m; i++)

{
for (int j = 0; j < q; j++)

for (int k = 0; k < n; k++)

arr3[i][j] += arr1[i][k] * arr2[k][j];

printf("After matrix multiplication of matrix 1 and matrix 2:-\n");


for (int a = 0; a < m; a++)

for (int b = 0; b < q; b++)

printf("%d ", arr3[a][b]);

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 *next;

};

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
{

for (int i = 1; i <= n; i++)

newnode = (struct Node *)malloc(sizeof(struct Node));

printf("Enter data for node%d: ", i);


scanf("%d", &newnode->data);
newnode->next = 0;

if (head == 0)

head = temp = newnode;


else
{

temp->next = newnode;

temp = newnode;
}

}
}

return head;

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;
printf("Linked list created :- \n");
while (temp != 0)
{

if (temp->next == 0)

printf("%d", temp->data);

temp = temp->next;

else

printf("%d -> ", temp->data);

temp = temp->next;
}

int main()

struct Node *head, *newnode, *temp; head = creation_LL(head,


newnode, temp);
traversing_LL(head, temp);
return 0;

RESULT- Code executed successfully.

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 *next;

};
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)

printf("No nodes can be created!");

return head;
}

else

for (int i = 1; i <= n; i++)

newnode = (struct Node *)malloc(sizeof(struct Node));

printf("Enter data for node%d: ", i);


scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
head = temp = newnode;

else
{

temp->next = newnode;

temp = newnode;
}

}
return head;

struct Node *insertion_at_begin(struct Node *head, struct Node *newnode, struct Node*temp)
{

newnode = (struct Node *)malloc(sizeof(struct Node));

printf("Enter data for newnode: ");

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)
{

newnode = (struct Node *)malloc(sizeof(struct Node));

printf("Enter data for newnode: ");

scanf("%d", &newnode->data);
newnode->next = 0; temp = head;
while (temp->next != 0)

temp = temp->next;
}

temp->next = newnode;

printf("New linked list after inserting node at the end:-\n");

traversing_LL(head, temp); return head;


}
struct Node *insertion_at_specific_position(struct Node*head, struct Node *newnode, struct
Node *temp)

int position, count = 1, counter = 0, d = 0; printf("Enter position to


insert node: ");

scanf("%d", &position);
if (position == 1)

head = insertion_at_begin(head, newnode, temp);

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)

head = insertion_at_end(head, newnode, temp); counter = 1;


}

if (!counter)

printf("\nIncorrect position!");

return head;
}

return head;

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;

while (temp!= 0)

if (temp->next == 0)

printf("%d", temp->data);
temp = temp->next;

}
else

printf("%d -> ", temp->data);

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 if (choice == 2) head = insertion_at_end(head, newnode, temp);

else if (choice == 3) head = insertion_at_specific_position(head,


newnode, temp);

else

printf("Invalid choice!");

printf("\nDo you want to insert another node (y/n) ? ");

scanf(" %c", &ans);

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;
}

void push(Stack *stack, int value) {

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>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* createNode(int new_data) {

Node* new_node = (Node*)malloc(sizeof(Node));

new_node->data = new_data;

new_node->next = NULL;

return new_node;

typedef struct Stack {

Node* head;

} Stack;

void initializeStack(Stack* stack) {

stack->head = NULL;

int isEmpty(Stack* stack) {

return stack->head == NULL;

void push(Stack* stack, int new_data) {

Node* new_node = createNode(new_data);

if (!new_node) {

printf("\nStack Overflow");

return;
}

new_node->next = stack->head;

stack->head = new_node;

void pop(Stack* stack) {

if (isEmpty(stack)) {

printf("\nStack Underflow\n");

return;

} else {

Node* temp = stack->head;

stack->head = stack->head->next;

free(temp);

int peek(Stack* stack) {

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);

printf("Top element is %d\n", peek(&stack));


printf("Removing two elements...\n");

pop(&stack);

pop(&stack);

printf("Top element is %d\n", peek(&stack));

return 0;
}

RESULT- Code executed successfully.

Output)
EXPERIMENT-10

AIM- program in C using queue


#include <stdbool.h>

#include <stdio.h>

#define MAX_SIZE 100

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) {

return (q->front == q->rear - 1);

bool isFull(Queue* q) {

return (q->rear == MAX_SIZE);

void enqueue(Queue* q, int value) {

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;

return q->items[q->front + 1];

void printQueue(Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;

printf("Current Queue: ");

for (int i = q->front + 1; i < q->rear; i++) {

printf("%d ", q->items[i]);

printf("\n");

int main() {

Queue q;

initializeQueue(&q);

enqueue(&q, 10);

printQueue(&q);

enqueue(&q, 20);

printQueue(&q);

enqueue(&q, 30);
printQueue(&q);

printf("Front element: %d\n", peek(&q));

dequeue(&q);

printQueue(&q);

printf("Front element after dequeue: %d\n", peek(&q));

return 0;
}

RESULT- Code executed successfully.

Output)
EXPERIMENT-11

AIM- Program in C using queue link list


Program:
#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* createNode(int new_data) {

Node* new_node = (Node*)malloc(sizeof(Node));

new_node->data = new_data;

new_node->next = NULL;

return new_node;

typedef struct Queue {

Node *front, *rear;

} Queue;

Queue* createQueue() {

Queue* q = (Queue*)malloc(sizeof(Queue));

q->front = q->rear = NULL;

return q;

int isEmpty(Queue* q) {

return (q->front == NULL && q->rear == NULL);

void enqueue(Queue* q, int new_data) {

Node* new_node = createNode(new_data);

if (q->rear == NULL) {

q->front = q->rear = new_node;


return;

q->rear->next = new_node;

q->rear = new_node;

void dequeue(Queue* q) {

if (isEmpty(q)) {

printf("Queue Underflow\n");

return;

Node* temp = q->front;

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);

printf("Queue Front: %d\n", getFront(q));

printf("Queue Rear: %d\n", getRear(q));

dequeue(q);

dequeue(q);

enqueue(q, 30);

enqueue(q, 40)

enqueue(q, 50);

dequeue(q);

printf("Queue Front: %d\n", getFront(q));

printf("Queue Rear: %d\n", getRear(q));

return 0;
}

RESULT- Code executed successfully.

Output)
EXPERIMENT-12
AIM- To implement a binary tree using an array.
Program:
#include <stdio.h>

#define MAX 15

int tree[MAX] = {0}; // Initialize tree array with 0

void insert(int data, int position) {

if (position >= MAX) {

printf("Position out of bounds!\n");

} else {

tree[position] = data;

printf("Inserted %d at position %d\n", data, position);

void display() {

int i;

printf("Tree elements:\n");

for (i = 1; i < MAX; i++) {

if (tree[i] != 0) {

printf("Position %d: %d\n", i, tree[i]);

int main() {

insert(1, 1); // Root node

insert(2, 2); // Left child of root

insert(3, 3); // Right child of root


insert(4, 4); // Left child of node 2

insert(5, 5); // Right child of node 2

insert(6, 6); // Left child of node 3

insert(7, 7); // Right child of node 3

display();

return 0;
}

RESULT- Code executed successfully.

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;

struct Node* prev;

struct Node* next;

};

struct Node* head = NULL;

void insert(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = head;

newNode->prev = NULL;

if (head != NULL) {

head->prev = newNode;

head = newNode;

void delete(int key) {

struct Node* temp = head;

if (temp == NULL) {

printf("List is empty!\n");

return;

while (temp != NULL && temp->data != key) {


temp = temp->next;

if (temp == NULL) {

printf("Element not found!\n");

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() {

struct Node* ptr = head;

printf("List elements: ");

while (ptr != NULL) {

printf("%d <-> ", ptr->data);

ptr = ptr->next;

printf("NULL\n");

int main() {

int choice, value;


while (1) {

printf("\nMenu:\n");

printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insert(value);

break;

case 2:

printf("Enter value to delete: ");

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;

void initGraph(int vertices) {

n = vertices;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

adjMatrix[i][j] = 0;

void addEdge(int u, int v) {

adjMatrix[u][v] = 1;

adjMatrix[v][u] = 1;

void displayGraph() {

printf("Adjacency Matrix:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

printf("%d ", adjMatrix[i][j]);

printf("\n");

}
}

int main() {

int vertices, u, v, choice;

printf("Enter the number of vertices: ");

scanf("%d", &vertices)

initGraph(vertices);

while (1) {

printf("\nMenu:\n1. Add Edge\n2. Display Graph\n3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter edge (u v): ");

scanf("%d %d", &u, &v);

if (u < vertices && v < vertices) {

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

#define INF INT_MAX

int graph[MAX][MAX];

int n;

int minKey(int key[], int mstSet[]) {

int min = INF, minIndex;

for (int v = 0; v < n; v++) {

if (mstSet[v] == 0 && key[v] < min) {

min = key[v];

minIndex = v;

return minIndex;

void primMST() {

int parent[MAX];

int key[MAX];

int mstSet[MAX];

for (int i = 0; i < n; i++) {

key[i] = INF;

mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;
for (int count = 0; count < n - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = 1;

for (int v = 0; v < n; v++) {

if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {

parent[v] = u;

key[v] = graph[u][v];

printf("Edge \tWeight\n");

for (int i = 1; i < n; i++) {

printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);

int main() {

int vertices, u, v, w, choice;

printf("Enter the number of vertices: ");

scanf("%d", &vertices);

n = vertices;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

graph[i][j] = 0;

while (1) {

printf("\nMenu:\n1. Add Edge\n2. Find MST (Prim's Algorithm)\n3. Exit\n");


printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter edge (u v w): ");

scanf("%d %d %d", &u, &v, &w);

if (u < n && v < n) {

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)

You might also like