Data Structure Solution
Data Structure Solution
1.ANS :-
#include <stdio.h>
int main()
{
int m,n,c,d,k,i,j, first[10][10], second[10][10],
sum[10][10],mul[10][10];
printf("Enter the number of rows and columns of
matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
printf("multiply of the matrix=\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
{
mul[i][j]+=first[i][k]*second[k][j];
}
}
}
//for printing result
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT :-
2.ANS :-
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
OUTPUT : -
3.ANS : -
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int STACK[MAX],TOP;
/* display stack element*/
void display(int []);
void main()
{
int ITEM=0;
int choice=0;
TOP=-1;
while(1)
{
/clrscr();/
printf("\nEnter Choice\n");
printf("1: display\n");
printf("2: insert (PUSH)\n");
printf("3: remove(POP)\n");
printf("4: Exit..:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(STACK);
break;
case 2:
printf("Enter Item to be insert :");
scanf("%d",&ITEM);
PUSH(STACK,ITEM);
break;
case 3:
POP(STACK);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice.");
break;
}
getch();
}// end of while(1)
/* function : display(),
to display stack elements.
*/
void display(int stack[])
{
int i=0;
if(TOP==-1)
{
printf("Stack is Empty .\n");
return;
}
/* function : PUSH(),
to push an item into stack.
*/
void PUSH(int stack[],int item)
{
if(TOP==MAX-1)
{
printf("\nSTACK is FULL CAN't ADD
ITEM\n");
return;
}
TOP++;
stack[TOP]=item;
}
/* function : POP(),
to pop an item from stack.
*/
void POP(int stack[])
{
int deletedItem;
if(TOP==-1)
{
printf("STACK is EMPTY.\n");
return;
}
deletedItem=stack[TOP];
TOP--;
printf("%d deleted
successfully\n",deletedItem);
return;
}
OUTPUT :-
4.ANS : -
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue
\n");
printf("2.Delete element from
queue \n");
printf("3.Display all elements of
queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue :
");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from
queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
OUTPUT : -
5.ANS :-
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
/Begin of insert/
void insert(int item)
{
if((front == 0 && rear == MAX-1) ||
(front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1) /*rear is at last
position of queue */
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
/End of insert/
/Begin of del/
void del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is :
%d\n",cqueue_arr[front]);
if(front == rear) /* queue has only one
element */
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
/*End of del() */
/Begin of display/
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d
",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d
",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d
",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
/End of display/
/Begin of main/
int main()
{
int choice,item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
switch(choice)
{
case 1 :
printf("Input the element for
insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=4);
return 0;
}
OUTPUT : -
6.ANS :-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void pop()
{
//temp is used to free the head node
struct node *temp;
if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head-
>data);
int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
printList();
pop();
printf("After the pop, the new linked
list\n");
printList();
pop();
printf("After the pop, the new linked
list\n");
printList();
return 0;
}
OUTPUT :-
7.ANS :-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n**********Main
Menu**********\n");
printf("\n============================
====================================
=\n");
printf("\n1.insert an
element\n2.Delete an element\n3.Display
the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct
node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
OUTPUT : -
8.ANS :-
#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);
struct node* temp = graph-
>adjLists[currentVertex];
while (temp) {
int adjVertex = temp->vertex;
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;
graph->adjLists = malloc(vertices *
sizeof(struct node*));
graph->visited = malloc(vertices *
sizeof(int));
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;
// 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;
}
OUTPUT :-
9.ANS :-
#include<stdio.h>
#include<stdlib.h>
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
OUTPUT :-
10.ANS :-
#include <stdio.h>
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, n, x);
(result == -1)
? printf("Element is not present in
array")
: printf("Element is present at index
%d", result);
return 0;
}
OUTPUT :-
11.ANS :-
#include <stdio.h>
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not
present in array")
: printf("Element is present at
index %d",
result);
return 0;
}
OUTPUT :-
12.ANS :-
#include <stdio.h>
int main()
{
int arr[100], i, n, step, temp;
// ask user for number of elements to be
sorted
printf("Enter the number of elements to
be sorted: ");
scanf("%d", &n);
// input elements if the array
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0;
}
OUTPUT :-
13.ANS :-
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
OUTPUT :-
14.ANS :-
#include <math.h>
#include <stdio.h>
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT :-
15.ANS :-
#include <stdio.h>
#include <stdlib.h>
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
OUTPUT :-
16.ANS : -
#include <iostream>
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
OUTPUT :-
17.ANS :-
#include<stdio.h>
#include<conio.h>
int main()
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix:
");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++) {
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]); }
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]); }
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++) {
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
} return 0;}
OUTPUT :-
18.ANS :-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// a structure to represent a weighted
edge in graph
struct Edge {
int src, dest, weight;
};
graph->edge = (struct
Edge*)malloc(sizeof( struct Edge)*E);
return graph;
}
return subsets[i].parent;
}
// A function that does union of two sets
of x and y
// (uses union by rank)
void Union(struct subset subsets[], int x,
int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
KruskalMST(graph);
return 0;
}
OUTPUT : -
19.ANS :-
temp->next = newNode;
temp->next = temp->next->next;
20.ANS :-
#include <bits/stdc++.h>
using namespace std;
// Structure for the elements in the
// priority queue
struct item {
int value;
int priority;
};
// Driver Code
int main()
{
// Function Call to insert elements
// as per the priority
enqueue(10, 2);
enqueue(14, 2);
enqueue(16, 4);
enqueue(12, 3);
return 0;
}
OUTPUT :-
21.ANS :-
#include <bits/stdc++.h>
using namespace std;
class DoubleHash {
// Pointer to an array containing
buckets
int* hashTable;
int curr_size;
public:
// function to check if hash table is full
bool isFull()
{
DoubleHash()
{
hashTable = new int[TABLE_SIZE];
curr_size = 0;
for (int i = 0; i < TABLE_SIZE; i++)
hashTable[i] = -1;
}
// if collision occurs
if (hashTable[index] != -1) {
// get index2 from second hash
int index2 = hash2(key);
int i = 1;
while (1) {
// get newIndex
int newIndex = (index + i *
index2) % TABLE_SIZE;
// if no collision occurs
else
hashTable[index] = key;
curr_size++;
}
// Driver's code
int main()
{
int a[] = { 19, 27, 36, 10, 64 };
int n = sizeof(a) / sizeof(a[0]);
OUTPUT : -