ALL PROGRAMS (1)
ALL PROGRAMS (1)
Module-1
Laboratory Component:
CODE:
#include<stdio.h>
#include<stdlib.h>
int n, i, a[20], opt;
void create()
{
printf("Enter the size of the array elements");
scanf("%d",&n);
printf("Enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}}
void display()
{
printf("The array elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}}
main()
{
printf("Program: Array operations 1\n Name: Manisha.K\n USN: 4SN21IS014");
do
{
printf("\n Menu \n");
printf("\n 1.create\n 2.display\n 3.exit\n");
printf("Enter your option\n");
scanf("%d",&opt);
switch(opt)
{
case 1: create();
break;
case 2: display();
break;
case 3: exit(0);
}}
while(1);
}
OUTPUT:
Figure 1
Laboratory Component:
Support the program with functions for each of the above operations.
CODE:
void display() {
printf ("The array elements are\n");
for (i=0; i<n; i++) {
printf ("%d\t", a[i]);
}
}
void insert() {
printf ("\n enter the position for new element:");
scanf ("%d",&pos);
printf ("\n enter the element to be inserted:");
scanf ("%d",&elem);
if (n<MAX && pos<=n)
{
for (i=n; i>=pos; i--)
{
a[i+1]=a[i];
}
a[pos]=elem;
n++;
}
}
void delete()
{
if (n==0)
{
printf ("\n no elements to delete!!");
}
else
{
printf ("\n enter the position of the element:");
scanf ("%d",&pos);
elem = a[pos];
if (pos<=n && pos>=0)
{
for (i=pos; i<n; i++)
{
a[i]=a[i+1];
}
n--;
printf ("\n deleted element is =%d",elem);
}
}
}
main()
{
printf ("Program: Array operations 2\n Name: Manisha.K\n USN: 4SN21IS014");
do
{
printf ("\n Menu \n");
printf ("\n 1.create\n 2.display\n 3.insertion\n 4.delete\n 5.exit\n");
printf ("Enter your option\n");
scanf ("%d", &opt);
switch(opt)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: delete ();
break;
case 5: exit(0);
}
}while(1);
}
OUTPUT:
Figure 2
Figure 3
Module-2
Laboratory Component:
Support the program with appropriate functions for each of the above
operations.
CODE:
#include <stdlib.h>
#include <stdio.h>
#define MAX 5
int stack[MAX], top=-1, opt;
int IsFull() {
if (top==(MAX-1))
{
printf ("stack is overflow\n");
return 1;
}
else
return 0;
}
int IsEmpty () {
if (top==-1)
{
printf ("stack is underflow\n");
return 1;
} else
return 0;
}
void Pop()
{
if (IsEmpty()==0)
{
printf ("the popped element is %d",stack[top--]);
}
}
void Display()
{
int i;
if (IsEmpty()==0)
{
printf ("stack contents are\n");
for (i=top; i>=0; i--)
{
printf ("%d\t",stack[i]);
}
}
}
void main()
{
printf("Program: Stack operations \n Name: Manisha.K\n USN: 4SN21IS014");
int item;
do
{
printf ("\n Menu \n");
printf ("\n1.Push\n 2.Pop\n 3.display\n 4.Exit\n");
printf ("enter your option");
scanf ("%d",&opt);
switch (opt)
{
case 1: printf ("enter an element to a stack\n");
scanf("%d", &item);
Push (item);
break;
case 2: Pop();
break;
case 3: Display();
break;
case 4: exit(0);
break;
}
}
while(1);
}
OUTPUT:
Figure 4
Figure 5
Figure 6
Figure 7
Laboratory Component:
CODE:
#include <stdio.h>
#include <math.h>
#include <string.h>
int compute (char symbol, int op1, int op2) {
int temp=1, i;
switch (symbol) {
case '+': temp= op1+ op2;
break;
case '-': temp= op1- op2;
break;
case '*': temp= op1* op2;
break;
case '/': if(op2!=0)
{
temp= op1/ op2;
break;
}
case '^': for(i=0;i<=op2;i++)
temp= temp*op1;
break;
default: break;
}
return temp;
}
void main() {
printf ("Program: Stack applications \n Name: Manisha.K\n USN: 4SN21IS014");
int i, s[20], op1, op2, top= -1;
char symbol, postfix[20];
printf ("enter the postfix expression");
scanf ("%s",postfix);
for(i=0; i<strlen(postfix); i++) {
symbol= postfix[i];
if (isdigit(symbol))
s[++top]=symbol-'0';
else {
op2= s[top--];
op1= s[top];
s[top]=compute(symbol,op1,op2);
}}
printf("the result is%d\n",s[top]);
}
OUTPUT:
Figure 8
Laboratory Component:
CODE:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void TOH (int n, char A, char B,char C) {
if (n>0)
{
TOH (n-1,A,C,B);
printf ("\nmove disc %d from %c to %c",n,A,C);
TOH (n-1,B,A,C);
}
}
void main(){
printf ("Program: Stack applications \n Name: Manisha.K\n USN: 4SN21IS014");
int n, A, B, C;
printf ("\nenter no. of disc");
scanf ("%d",&n);
TOH (n,'A','B','C');
}
OUTPUT:
Figure 9
Module-3
Laboratory Component:
CODE:
#include <stdio.h>
#include <stdlib.h>
int MAX=4;
struct SLL {
int data;
struct SLL*next;
};
NODE * Insert_Rear(NODE*head)
{
NODE * newnode,*p;
newnode = Getnode();
if (head==NULL)
head=newnode;
else
{
p=head;
while (p->next!=NULL)
{
p = p->next;
}
p -> next=newnode;
}
return(head);
}
NODE * Delete_Rear(NODE*head)
{
NODE*p,*q;
p=head;
if (head==NULL)
printf ("\n Empty(Underflow)");
else
{
if (head->next==NULL)
head=NULL;
else
{
while (p->next!=NULL)
{
q=p;
p= p->next;
}
q->next=NULL;
free(p);
}
}
return head;
}
NODE*Concatenate(NODE*head1,NODE*head2)
{
NODE*head3=head1,*p=head1;
Display(head1);
Display(head2);
while(p->next!=NULL)
{
p=p->next;
}
p->next=head2;
return(head3);
}
void Search(NODE*head1)
{
NODE*p=head1;
int flag=0,data;
if (p->data==data)
{
flag=1;
printf("Data found");
}
p=p->next;
}
if(flag==0)
printf("Data not found");
}
int main()
{
printf("Program: Singly linked list \n Name: Manisha.K\n USN: 4SN21IS014");
int ch;
NODE*head1=NULL,*head2=NULL,*head3=NULL;
printf("\n DATA \n");
do{
printf("\n MENU: \n1.Create SLL1 \n2.Create SLL2 \n3.Display SLL-1 \n4.Display SLL-2 \
n5.SLL1-Stack 1- PUSH\n6.SLL1- Stack 1-POP\n7.Concatenate\n8.Search in SLL1\n9.Exit \n
Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: head1=Insert_Rear(head1);
break;
case 2: head2=Insert_Rear(head2);
break;
case 3: Display(head1);
break;
case 4: Display(head2);
break;
case 5: head1= Insert_Rear(head1);
break;
case 6: head1= Delete_Rear(head1);
break;
case 7: head3= Concatenate(head1,head2);
printf("\n After concatenation:\n");
Display (head3);
break;
case 8: Search(head1);
break;
case 9: exit(0);
}
}
while(1);
}
OUTPUT:
Figure 10
Figure 11
Figure 12
Figure 13
Figure 14
Figure 15
Figure 16
Laboratory Component:
CODE:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int MAX=4,count=0;
struct Prof{
char ID[5], Name[20], Branch[5], Area[10];
struct Prof *left, *right;
};
typedef struct Prof NODE;
NODE * Getnode()
{
NODE* node= (NODE*) malloc (sizeof(NODE));
printf ("\n Enter ID, Name, Dept, Area of spl:");
scanf ("%s %s %s %s", node-> ID, node-> Name, node-> Branch, node-> Area);
node->right= node->left=NULL;
count ++;
return node;
}
void Display (NODE*p)
{
if (p!=NULL)
{
printf ("\n ID\t Name\t Branch\t Area");
while(p!=NULL)
{
printf ("\n%s\t%s\t%s\t%s ",p-> ID, p-> Name,p->Branch,p->Area);
p=p->right;
}
printf ("\n Number of nodes %d \n",count);
}
else
printf ("\n No data available!!");
}
NODE* Insert_Rear(NODE*head){
NODE* p= head, *newnode;
if (count==MAX)
printf ("\n Overflow!!");
else
{
newnode= Getnode();
if (head==NULL)
head =newnode;
else
{
while (p->right!=NULL)
{
p=p->right;
}
p-> right= newnode;
newnode->left=p;
}
}
return head;
}
NODE* Delete_Rear(NODE*head){
NODE* p=head,*q;
if (head==NULL)
printf ("\n list is empty(QUEUE)");
else {
while (p->right!=NULL){
p=p->right;
}
if (p->left!=NULL){
q= p->left;
q->right=NULL;
}
else
head=NULL;
free (p);
count--;
}
return head;
}
NODE* Delete_Front (NODE*head){
NODE* p=head,*q;
if (head==NULL)
printf ("\n list is empty(QUEUE)");
else
{
if (p->right==NULL)
head =NULL;
else {
head =p->right;
head->left=NULL;
}
free (p);
count--;
}
return head;
}
int main()
{
printf ("Program: Doubly linked list \n Name: Manisha.K\n USN: 4SN21IS014");
int ch,i;
NODE * head=NULL;
printf ("\n How many professors data you want to create:");
do{
printf ("\n 1.Display\n 2.Stack-PUSH\n 3.Stack-POP\n 4.Queue- Insert\n 5.Queue-Delete\n
6.EXIT");
printf ("\nenter your choice:");
scanf ("%d",&ch);
switch(ch)
{
case 1: Display(head);
break ;
case 2: head= Insert_Rear(head);
break;
case 3: head= Delete_Rear(head);
break;
case 4: head= Insert_Rear(head);
break;
case 5: head= Delete_Front(head);
break;
case 6: exit(0);
}
}
while(1);
}
OUTPUT:
Figure 17
Figure 18
Figure 19
Module-4
Laboratory Component:
CODE:
#include <stdio.h>
#include <stdlib.h>
struct CBTree
{
int data;
struct CBTree *left,*right;
};
int main(){
printf("Program: Binary tree \n Name: Manisha.K\n USN: 4SN21IS014");
int arr[50], n=0, num;
NODE* root=NULL;
while (1){
printf ("enter number to insert to CBT:");
scanf ("%d",&arr[n]);
printf ("enter 0 to stop and 1 to continue..");
scanf ("%d",&num);
if (num==0)
break;
else
n++;
}
root=insertLevelOrder(root,arr,0,n+1);
inOrder(root);
}
OUTPUT:
Figure 20
Laboratory Component:
CODE:
#include <stdio.h>
#include <stdlib.h>
struct BST{
int data;
struct BST* left, *right;
};
typedef struct BST NODE;
NODE * Createtree(NODE* node,int data) {
if (node==NULL) {
NODE*temp= (NODE*) calloc (1,sizeof(NODE));
temp->data=data;
return temp;
}
if (data<(node->data))
node->left= Createtree (node->left,data);
else if(data> node->data)
node->right= Createtree (node->right,data);
else
printf ("Duplicate data found");
return node;
}
NODE* Search (NODE* node,int data){
if (node==NULL)
printf ("Element not found");
else if (data<node->data)
node->left= Search(node->left,data);
else if (data>node->data)
node->right =Search (node-> right,data);
else
printf ("\n Element found: %d",node->data);
return node;
}
void Inorder (NODE*node){
if (node!=NULL) {
Inorder (node-> left);
printf ("%d\t",node->data);
Inorder (node-> right);
}
}
void Preorder (NODE*node) {
if (node!=0) {
printf ("%d\t",node->data);
Preorder (node-> left);
OUTPUT:
Figure 21
Figure 22
Figure 23
Figure 24
Module-5
Laboratory Component:
CODE:
#include <stdlib.h>
#include <stdio.h>
int adj[20][20], visited[20], G[20], front= -1, rear= -1, top= -1,n,i;
void BFS(int v){
int cur;
visited[v]=1;
G[++rear]=v;
while (front!=rear) {
cur=G[++front];
for (i=1; i<=n; i++) {
if ((adj[cur][i]==1)&&(visited[i]==0))
{
G[++rear]==i;
visited[i]=1;
printf("%d->%d\n", cur, i);
}
}
}
}
void DFS2 (int v){
int i;
top= -1;
visited[v]=1;
do {
i=1;
while (i<=n){
if(adj[v][i]==1&&visited[i]==0){
G[++top]==v;
visited[i]=1;
printf("%d-->%d", v, i);
v=i;
i=1;
}
else
i++;
}
v= G[top--];
}
while (top!=-1);
}
int main(){
int ch, start, i, j;
printf ("Program: GRAPH \n Name: Manisha.K\n USN: 4SN21IS014");
printf ("\n Enter the number of vertices in graph:");
scanf ("%d",&n);
printf ("\n enter the adjacency matrix:\n");
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
scanf ("%d",&adj[i][j]);
do{
printf ("\n1.BFS\n 2.DFS\n 3.exit");
printf ("enter your choice");
scanf ("%d",&ch);
for (i=1; i<=n; i++)
visited[i]=0;
printf ("enter the starting vertex");
scanf ("%d",&start);
printf ("\n Nodes reachable from starting vertex %d are",start);
switch (ch)
{
case 1: BFS(start);
break;
case 2: DFS2(start);
break;
case 3: exit(0);
}
}while(ch!=3);
}
OUTPUT:
Figure 25
Laboratory Component:
10. Design and develop a program in C that uses Hash Function H:K->L as
H(K)=K mod m(reminder method) and implement hashing technique to map
a given key K to the address space L. Resolve the collision (if any) using linear
probing.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define HTSIZE 10
#define KEYSPACE 50
struct employee
{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[KEYSPACE];
EMP HashTable[HTSIZE];
void Display(){
int i, ch;
printf("\n1.Display ALL\n2.Filtered Display \n Enter the choice:");
scanf("%d",&ch);
printf("\nHash Value\tEmpID\tEmpName");
switch (ch){
case 1:
for(i=0; i<HTSIZE; i++)
printf("\n%d\t\t%d\t%s", i, HashTable[i].id,
HashTable[i].name);
break;
case 2:
for(i=0; i<HTSIZE; i++)
{
if(HashTable[i].id == -1)
continue;
else
printf("\n%d\t\t%d\t%s", i, HashTable[i].id,
HashTable[i].name);
}
break;
}
}
int main()
{
printf ("Name: Manisha K\n");
printf ("USN:4SN21IS014\n");
printf("Program name: Hashing\n");
int i;
int ch = 1;
printf("\nCollision handling by linear probing: ");
for (i=0; i < HTSIZE; i++)
{
HashTable[i].id = -1;
}
i=0;
do{
printf("\nEnter emp id: ");
scanf("%d",&emp[i].id);
printf("\n Enter emp name: ");
scanf("%s",emp[i].name);
Linear_prob(emp[i]);
printf("\n Do you wish to continue? (1/0): ");
scanf("%d",&ch);
}
while(ch==1);
Display();
}
OUTPUT:
Figure 26
Figure 27
Figure 28
Figure 29
Figure 30
Figure 31