//SORTING AND MERGING OF TWO ARRAYS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n1,n2,n3,i,j,k:
int a[10],b[10],c[10];
clrscr();
printf(“\n\tSORTING AND MERGING OF TWO ARRAYS”);
printf(‘\n\t****************************************”);
printf(“\nEnter the size of first array”);
scanf(“%d”,&n1);
printf(“\nEnter the size of second array”);
scanf(“%d”&n2);
n3=n1+n2;
printf(“\nEnter the sorted array elements”);
for( i=0;i<n1;i++)
{
scanf(“%d”,&a[i]);
c[i]=a[i];
}
k=n1
printf(“Enter the sorted array elements”);
for( i=0;i<n2;i++)
{
scanf(“%d”,&b[i]);
c[k]=b[i];
k++;
}
printf(“\n\tThe merged array”);
for( i=0;i<n3:i++)
printf(“%d”,c[i]);
printf(“\nAfter sorting”);
for( i=0;i<n3;i++)
{
int temp;
for(j=i+1;j<n3;j++)
{
if(c[i]<c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
for( i=0;i<n3;i++)
{
printf(“%d”,c[i]);
}
return 0;
getch();
}
SORTING AND MERGING OF TWO ARRAYS
****************************************
Enter the size of first array : 3
Enter the size of second array : 3
Enter the first array elements
2
4
1
Enter the second array elements
6
3
5
The Merged array : 2 1 4 6 3 5
After sorting : 6 5 4 3 2 1
// MATRIX MULTIPLICATION
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[25][25,b[25] [25],c[25][25],I,j,k,r,s;
Int m,n;
Clrscr();
printf(“/n/t MATRIX MULTIPLICATION”);
printf(“/n/t *********************”);
printf(“/n Enter the number of rows:”);
scanf(“%d”,&m);
printf(“/n Enter the number of columns:”);
scanf(“%d”,&n);
printf(“\n Enter the elements of first matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“/n Enter the elements of second matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“/t%d”,&b[i][j]);
}
}
printf(“/n/t VALUE OF A MATRIX”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“/n/t%d”,a[i][j]);
}
}
printf(“/n/t VALUE OF B MATRIX”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“/n/t %d”,b[i][j]);
}
}
for(i=0;i<m;i++)
{
printf(“/n”);
for(j=0;j<n;j++)
{
C[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf(“/n RESULTANT MATRIX”);
for(i=0;i<m;i++)
{
printf(“/n”);
for(j=0;j<n;j++)
Printf(“/t %d”,c[i][j]);
}
getch();
}
MATRIX MULTIPLICATION
****************************
Enter the number of rows: 2
Enter the number of columns : 2
Enter the first value of Matrix:
1
1
1
1
Enter the Second value of Matrix:
1
1
1
1
VALUE OF A MATRIX
1 1
1 1
VALUE OF B MATRIX
1 1
1 1
RESULTANT MATRIX
2 2
2 2
// QUEUE OPERATIONS
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int queue_arr[MAX];
int rear=-1;
int front=-1;
void insert(int item);
int del();
int peek();
void display();
int isFull();
int isEmpty();
int main()
{
int choice,item; while(1)
{
printf(“\n\tQUEUE OPERATIONS”);
printf(“\n\t********************”);
printf("\[Link]\n");
printf("[Link]\n");
printf("[Link] element at the front\n");
printf("[Link] all elements of the queue\n");
printf("[Link]\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nInput the element for adding in queue : ");
scanf("%d",&item);
insert(item);
case 2: break;
item=del();
printf("\nDeleted element is %d\n",item); break;
case 3:
printf("\nElement at the front is %d\n",peek());
break;
case 4:
display();
case 5:
break; exit(1)
}
default:
printf("\nWrong choice\n");
}
}
return 0;
void insert(int item)
{
if( isFull() )
{
printf("\nQueue Overflow\n");
return;
}
if( front == -1 )
front=0;
rear=rear+1;
queue_arr[rear]=item ;
}
int del()
{
int item;
if( isEmpty() )
{
printf("\nQueue Underflow\n");
exit(1);
}
item=queue_arr[front];
front=front+1;
return item;
}
int peek()
{
if( isEmpty() )
{
printf("\nQueue Underflow\n");
exit(1);
}
return queue_arr[front];
}
int isEmpty()
{
if( front==-1 || front==rear+1 )
return 1;
else
return 0;
}
int isFull()
{
if( rear==MAX-1 )
return 1;
else
return 0;
}
void display()
{
int i;
if ( isEmpty() )
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue is :\n\n");
for(i=front;i<=rear;i++)
printf("%d ",queue_arr[i]);
printf("\n\n");
}
QUEUE OPERATIONS
***********************
[Link]
[Link]
[Link] element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 1
Input the element for adding in queue : 6
[Link]
2. Delete
3. Display element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 1
Input the element for adding in queue : 7
[Link]
2. Delete
3. Display element at the front
[Link] all elements of the queue
[Link]
[Link]
[Link]
[Link] element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 3
Element at the front is 7
[Link]
[Link]
[Link] element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 4
Queue is :
6 7
[Link]
[Link]
[Link] element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 2
Deleted element is 6
[Link]
2. Delete
3. Display element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 4
Queue is :
7
[Link]
[Link]
[Link] element at the front
[Link] all elements of the queue
[Link]
Enter your choice : 5
//EMPLOYEE DETAILS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct emp_data
{
int empno;
char empName[MAX];
char designation[MAX];
struct emp_data *next;
};
struct emp_data *insert(struct emp_data *front, int id, char name[],char desg[])
{
struct emp_data *newnode;
newnode = (struct emp_data*)malloc(sizeof(struct emp_data));
if (newnode == NULL)
{
printf("\n Allocation failed \n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
}
void printNode(struct emp_data *p)
{
printf("\n Employee Details...\n");
printf("\n Emp No : %d", p->empno);
printf("\n Name : %s", p->empName);
printf("\n Designation : %s\n", p->designation);
printf("------------------------------------\n");
}
struct emp_data* deleteNode(struct emp_data *front, int id)
{
struct emp_data *ptr;
struct emp_data *bptr;
if (front->empno == id)
{
ptr = front;
printf("\n Node deleted:");
printNode(front);
front = front->next;
free(ptr);
return(front);
}
for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,bptr = bptr->next)
{
if (ptr->empno == id)
{
printf("\n Node deleted:");
printNode(ptr);
bptr->next = ptr-
>next; free(ptr);
return(front);
}
}
printf("\n Employee Number %d not found ", id);
return(front);
}
void search(struct emp_data *front, int key)
{
struct emp_data *ptr;
for (ptr = front; ptr != NULL; ptr = ptr -> next)
{
if (ptr->empno == key)
{
printf("\n Key found:");
printNode(ptr);
return;
}
}
printf("\n Employee Number %d not found ", key);
}
void display(struct emp_data *front)
{
struct emp_data *ptr;
for (ptr = front; ptr != NULL; ptr = ptr->next)
{
printNode(ptr);
}
}
void menu()
{
printf(“\n\t EMPLOYEE DETAILS”);
printf("--------------------------------------------\n");
printf("Press 1 to INSERT a node into the list\n");
printf("Press 2 to DELETE a node from the list\n");
printf("Press 3 to DISPLAY the list\n");
printf("Press 4 to SEARCH the list \n");
printf("Press 5 to EXIT \n");
}
char option()
{
char choice;
printf("\n\n>> Enter your choice: ");
switch(choice=getche())
{
case '1':
case '2':
case '3':
case '4':
case '5': return(choice);
default : printf("\n Invalid choice.");
}
return choice;
}
void main()
{
struct emp_data
*linkList; char name[21],
desig[51]; char choice;
int eno;
clrscr();
linkList = NULL;
printf("\n Welcome to demonstration of singly linked list \n");
menu();
do
{
choice = option();
switch(choice)
{
case '1':
printf("\n Enter the Employee Number : ");
scanf("%d", &eno);
printf("Enter the Employee name : ");
fflush(stdin);
gets(name);
printf("Enter the Employee Designation : ");
gets(desig);
linkList = insert(linkList, eno, name, desig);
break;
case '2':
printf("\n\n Enter the employee number to be deleted: ");
scanf("%d", &eno);
linkList = deleteNode(linkList, eno);
break;
case '3':
if (linkList == NULL)
{
printf("\n List empty.");
break;
}
display(linkList);
break;
case '4':
printf("\n\n Enter the employee number to be searched: ");
scanf("%d", &eno);
search(linkList, eno);
break;
case '5': break;
}
} while (choice != '5');
}
EMPLOYEE DETAILS
----------------------------------------------
Press 1 to INSERT a node into the list
Press 2 to DELETE a node from the list
Press 3 to DISPLAY the list
Press 4 to SEARCH the list
Press 5 to EXIT
---------------------------------------------
>> Enter your choice: 1
Enter the Employee Number 20
Enter the Employee name : ABI
Enter the Employee Designation : MANAGER
>> Enter your choice: 1
Enter the Employee Number 21
Enter the Employee name : AMMU
Enter the Employee Designation : HR
>> Enter your choice: 3
Employee Details...
Emp No 20
Name : ABI
Designation : MANAGER
-------------------------------------
Employee Details...
Emp No 21
Name : AMMU
Designation : HR
-------------------------------------
>> Enter your choice: 3
Employee Details...
Emp No 20
Name : ABI
Designation : MANAGER
Employee Details...
Emp No 21
Name : AMMU
Designation : HR
-------------------------------------
>> Enter your choice: 2
Enter the employee number to be deleted: 21
Node deleted:
Employee Details...
Emp No 21
Name : AMMU
Designation : HR
-------------------------------------
>> Enter your choice: 3
Employee Details...
Emp No 20
Name : ABI
Designation : MANAGER
-------------------------------------
>> Enter your choice: 5
//SINGLE LINKED LIST COUNT THE NUMBER OF NODES
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
int NodeCount();
void displayList();
int main()
{
int n,totalNode;
clrscr();
printf("\n\n COUNT THE NUMBER OF NODES IN SINGLE LINKED LIST AND :\
n");
printf(“\n\t************************************************************”);
printf(" Input the number of nodes : ");
scanf("%d", &n); createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
totalNode = NodeCount();
printf("\n Total number of nodes = %d\n", totalNode);
getch();
}
void createNodeList(int n)
{
struct node *fnNode, *tmp; int
num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory cannot be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
int NodeCount()
{
int ctr = 0;
struct node *tmp;
tmp = stnode;
while(tmp != NULL)
{
ctr++;
tmp = tmp->nextptr;
}
return ctr;
}
void displayList()
{
struct node *tmp; if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{ tmp = stnode; while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
COUNT THE NUMBER OF NODES IN SINGLE LINKED LIST
**********************************************************
number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 8
Input data for node 3 : 2
Data entered in the list are : Data =
5
Data = 8
Data = 2
Total number of nodes = 3
//DOUBLY LINKED LIST
#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *prev; struct
node *next;
} *head, *last;
void list (int n)
{
int i, data;
struct node *new_node;
if (n >= 1)
{
head = (struct node *) malloc (sizeof (struct node)); printf
("Enter data of node 1 : ");
scanf ("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for (i = 2; i <= n; i++)
{
new_node = (struct node *) malloc (sizeof (struct node)); printf
("\nEnter data of node %d : ", i);
scanf ("%d", &data); new_node-
>data = data; new_node->prev =
last; new_node->next = NULL;
last->next = new_node; last =
new_node;
}
}
void print()
{
struct node *temp; int n = 1;
if (head == NULL)
{
printf ("\nList is empty\n");
}
else
{
temp = head;
printf ("The Doubly Linked List is :\n"); while
(temp != NULL)
{
printf ("%d ", temp->data);
n++;
temp = temp->next;
}
}
}
void insert (int data)
{
struct node *new_node;
if (head == NULL)
{
printf ("Please enter data for node \n");
}
else
{
new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = data;
new_node->next = head;
new_node->prev = NULL;
head->prev = new_node;
head = new_node;
}
}
int main ()
{
int n, data, choice = 1;
head = last = NULL;
clrscr();
printf(“\n\t DOUBLY LINKED LIST”);
printf ("Enter the size of linked list : \n");
scanf ("%d", &n);
list (n);
print ();
printf ("\nEnter data for insert at the beginning :\n ");
scanf ("%d", &data);
insert(data);
print ();
getch();
return 0;
}
DOUBLY LINKED LIST
**********************
Enter the size of linked list : 3
Enter data of node 1 : 6
Enter data of node 2 : 5
Enter data of node 3 : 4
The Doubly Linked List is : 6 5 4
Enter data for insert at the beginning : 7
The Doubly Linked List is : 7 6 5 4
// MID SQUARE METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void intermediate_odd(long long,int);
int count_digit(long long, int);
int main()
{
int a[20];
int digit;
int n;
int i,j;
long long p; int count; int
divisor;
long long seed;
printf("Enter a number to generate the random number: ");
scanf("%lld",&seed);
p=seed; digit=0;
digit=count_digit(p,digit); count=digit;
if(digit%2!=0)
{
printf("\nThe mid square multipicate method cannot be applied as the number of digit of
seed is %d(odd)",digit);
}
else
{
printf("\n How many random number you want to generate: ");
scanf("%d",&n);
printf("\nThe random numbers are:\n");
for(i=0;i<n;i++)
seed=seed*seed;
p=seed;
digit=0;
digit=count_digit(p,digit); digit=ceil(digit/4.0);
for(j=0;j<digit;j++)
{
seed=seed/10;
}
divisor=pow(10,count);
seed=seed%divisor;
a[i]=seed;
printf("%lld\t",seed);
digit=0;
p=seed;
digit=count_digit(p,digit);
if(digit%2!=0)
{
intermediate_odd(seed,digit);
exit(0);
}
}
}
getch();
return(0);
}
int count_digit(long long seed,int digit)
{
long long p; p=seed;
while(p!=0)
{
p=p/10; digit++;
}
return digit;
}
void intermediate_odd(long long seed, int digit)
{
printf("\nThe intermediate seed %d has odd length %d and therefore mid square method
cannot be applied from here",seed,digit);
}
MID SQUARE METHOD
***********************
Enter a number to generate the random number: 11
How many random number you want to generate: 2
The random numbers are:
12 14
//BINARY TREE TRAVERSAL
#include <stdio.h>
#include <stdlib.h>
enum Traversal
{
PREORDER, INORDER, POSTORDER
};
typedef enum Traversal Traversal;
typedef struct Node Node;
struct Node
{
int value;
Node* left, *right;
};
Node* init_tree(int data)
{
Node* root = (Node*) malloc (sizeof(Node)); root->left
= root->right = NULL;
root->value = data; return
root;
}
Node* create_node(int data)
{
Node* node = (Node*) malloc (sizeof(Node)); node-
>value = data;
node->left = node->right = NULL; return
node;
}
void free_tree(Node* root)
{
Node* temp = root; if (!temp)
return; free_tree(temp-
>left); free_tree(temp->right);
if (!temp->left && !temp->right) {
free(temp);
return;
}
}
void print_tree(Traversal traversal, Node* root) { if (!root)
return; switch(traversal) {
case (PREORDER):
printf("%d -> ", root->value);
print_tree(traversal, root->left);
print_tree(traversal, root->right); break;
case (INORDER):
print_tree(traversal, root->left); printf("%d
-> ", root->value); print_tree(traversal,
root->right); break;
case (POSTORDER):
print_tree(traversal, root->left);
print_tree(traversal, root->right);
printf("%d -> ", root->value); break;
}
}
int main()
{
Node* root = init_tree(10);
clrscr();
printf(“\n\t TREE TRAVERSAL”);
printf(“\n\t********************”);
root->left = create_node(20);
root->right = create_node(30);
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal- - - -\n");
print_tree(PREORDER, root); printf("\n\n");
printf("----Inorder Traversal- - - -\n");
print_tree(INORDER, root); printf("\
n\n");
printf("----Postorder Traversal-----\n");
print_tree(POSTORDER, root); printf("\
n\n");
free_tree(root); getch();
}
TREE TRAVERSAL
*********************
----Preorder Traversal:----
10 -> 20 -> 40 -> 50 -> 30 -> 60 -> 70 ->
----Inorder Traversal:----
40 -> 20 -> 50 -> 10 -> 60 -> 30 -> 70 ->
----Postorder Traversal:----
40 -> 50 -> 20 -> 60 -> 70 -> 30 -> 10 ->
// 10. INSERT BINARY TREE
#include<stdio.h>
#include<stdlib.h> struct
node
{
int key;
struct node *left; struct node
*right;
};
struct node *getNewNode(int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insert(struct node *root, int val)
{
if(root == NULL)
return getNewNode(val);
if(root->key < val)
root->right = insert(root->right,val);
else if(root->key > val)
root->left = insert(root->left,val);
return root;
}
void inorder(struct node *root)
{
if(root == NULL) return; inorder(root-
>left);
printf("%d ",root->key); inorder(root->right);
}
void main()
{
struct node *root = NULL;
clrscr();
printf("\n\tINSERT BINARY TREE\n");
printf(“\n\t**********************”);
insert(root,100);
root = insert(root,75);
root = insert(root,150);
root = insert(root,55);
inorder(root);
getch();
}
INSERT BINARY TREE
**********************
50 75 150
INDEX
INDEX
S.N PAGE
DATE PARTICULARS
O. NO.
1 14.03.2022 SORTING AND MERGING TWO ARRAYS
2 14.03.2022 MATRIX MULTIPLICATION
3 24.03.2022 STACK OPERATIONS USING ARRAY
4 31.03.2022 IMPLEMENTATION OF QUEUE
5 07.04.2022 EMPLOYEE DETAILS USING LINKED LIST
COUNT THE NUMBER OF NODES IN
6 19.04.2022
SINGLE LINKED LIST
7 19.04.2022 INSERTION OF DOUBLY LINKED LIST
HASH TABLE USING MID SQUARE
8 25.04.2022
METHOD
9 02.05.2022 BINARY TREE TRAVERSAL
10 09.05.2022 INSERTION OF BINARY TREE