21CS32 L
21CS32 L
Design, Develop and Implement a menu driven Program in C for the following Array Operations
c. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int a[100];
int n;
void create( );
void display( );
void main( )
int ch;
clrscr();
while(1)
printf("\nEnter 3 to Exit:\t");
scanf("%d",&ch);
switch(ch)
case 1:
printf("Enter number of elements in array\n");
scanf("%d", &n);
create();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
getch();
void create()
int i;
for (i=0;i<n;i++)
scanf("%d", &a[i]);
void display()
int i;
printf("Elements in an array\n");
for (i=0;i<n;i++)
printf("%d\t",a[i]);
}
2. Design, Develop and Implement a menu driven Program in C for the following Array operations
d. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int a[100];
int n;
void create();
void insert(int,int);
void del(int);
void display();
void main()
clrscr();
while(1)
printf("\nEnter 5 to Exit:");
scanf("%d" , &ch);
switch(ch)
case 1:
scanf("%d", &n);
create();
break;
case 2:
display();
break;
case 3:
insert(pos,value);
break;
case 4:
del(pos);
break;
case 5:
exit(0);
default:
break;
getch();
void create()
int i;
scanf("%d", &a[i]);
int i;
a[i+1]=a[i];
a[pos-1]=value;
n=n+1;
printf("\nArray Elements Updated.\n");
int i;
else
a[i] = a[i+1];
n=n-1;
printf("Element deleted\n");
void display()
int i;
printf("%d\t",a[i]);
}
3. Design, Develop and Implement a menu driven Program in C for the following operations on
e. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define max 10
int top=-1;
int a[10];
void push();
void pop();
void display();
void main()
int ch;
clrscr();
while(1)
{
printf("\nEnter 1 to PUSH");
printf("\nEnter 2 to POP");
printf("\nEnter 3 to DISPLAY");
printf("\nEnter 4 to EXIT");
scanf("%d",&ch);
switch(ch)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
break;
getch();
void push()
{
int item;
if(top==(max-1))
printf("Stack overflow");
else
top+=1;
scanf("%d",&item);
a[top]=item;
void pop()
int item;
if(top==-1)
printf("Stack underflow");
else
item=a[top];
top-=1;
void display()
int i;
if(top<0)
printf("Stack underflow");
else{
for(i=0;i<=top;i++)
printf("\n%d",a[i]);
}
4.Design, Develop and Implement a Program in C for the following Stack Applications
4a.)
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
float stack[25];
int top=-1;
void push(float);
float pop();
float evaluate(char[],float[]);
void main()
char suffix[25];
float data[20],result;
int i=0;
scanf("%s",suffix);
while(suffix[i]!='\0')
if(isalpha(suffix[i]))
{
printf("Enter the values of %c",suffix[i]);
scanf("%f",&data[i]);
i++;
result=evaluate(suffix,data);
int i=0;
float op1,op2;
char c;
while(s[i]!='\0')
c=s[i];
if(isalpha(s[i]))
push(d[i]);
else
op2=pop();
op1=pop();
switch(c)
case'+':push(op1+op2);break;
case'-':push(op1-op2);break;
case'*':push(op1*op2);break;
case'/':push(op1/op2);break;
i++;
return(pop());
void push(float d)
stack[++top]=d;
float pop()
return(stack[top--]);
}
4b.)
#include<stdio.h>
if(n==1)
else
tower(n-1,BEG,END,AUX);
tower(1,BEG,AUX,END);
tower(n-1,AUX,BEG,END);
void main()
int num;
scanf("%d",&num);;
tower(num,'A','B','C');
}
5. Singly Linked List (SLL) of Integer Data
b. Display of SLL
c. Linear search. Create a SLL queue of N Students Data Concatenation of two SLL of
integers.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main()
{
int choice=0;
while(choice!=9)
printf("\n\n****MENU****\n");
printf("\n Choose one option from the the following list \n");
printf("\n==================\n");
printf("\n1.Insert in begining\n");
printf("2.Insert at last\n");
printf("8.Show\n");
printf("9.Exit\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
break;
void beginsert()
{
struct node *ptr;
int item;
if(ptr==NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data=item;
ptr->next=head;
head=ptr;
printf("\nNode inserted");
void lastinsert()
int item;
if (ptr==NULL)
printf("\nOVERFLOW");
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data=item;
if(head==NULL)
ptr->next=NULL;
head=ptr;
printf("\nNode inserted");
else
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=ptr;
ptr->next=NULL;
printf("\nNode inserted");
void randominsert()
int i,loc,item;
if(ptr==NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
ptr->data=item;
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp=temp->next;
//ptr=temp->next;
//temp->next=ptr;
if(temp==NULL)
printf("\ncan't insert\n");
return;
ptr->next=temp->next;
temp->next=ptr;
printf("\nNode inserted");
void begin_delete()
if(head==NULL)
printf("\nList is empty\n");
else
ptr=head;
head=ptr->next;
free(ptr);
void last_delete()
if(head==NULL)
printf("\nlist is empty");
else if(head->next==NULL)
head=NULL;
free(head);
else
ptr=head;
ptr=ptr->next;
ptr1->next=NULL;
free(ptr);
void random_delete()
int loc,i;
printf("\nEnter the location of the node after which you want to perform deletion\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
printf("\nCan't delete");
return;
}
ptr1->next=ptr->next;
free(ptr);
void search()
int item,i=0,flag;
ptr=head;
if(ptr==NULL)
printf("\nEmpty list\n");
else
scanf("%d",&item);
while(ptr!=NULL)
if(ptr->data==item)
flag=0;
else
{
flag=1;
i++;
ptr=ptr->next;
if(flag==1)
void display()
ptr=head;
if(ptr==NULL)
printf("Nothing to print");
else
printf("\nPrinting values\n");
while(ptr!=NULL)
printf("\n%d",ptr->data);
ptr=ptr->next;
}
}
6.Design, Develop and Implement a menu driven Program in C for the following operations
on
Doubly Linked List (DLL) of Professor Data with the fields: ID, Name, Branch, Area of
specialization
a. Create a DLL stack of N Professor’s Data.
b. Create a DLL queue of N Professor’s Data
Display the status of DLL and count the number of nodes in it
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void display();
void deletefront();
void create();
void insertfront();
void deleterear();
void node();
struct prof
{
char usn[15],name[20],specialisation[15],branch[10];
struct prof *next;
struct prof *prev;
};
struct prof *start=NULL, *second=NULL, *temp;
main()
{
int choice;
while(1)
{
printf("\n\n\nMENU\n");
printf("1.CREATE\n");
printf("2.DISPLAY\n");
printf("3.INSERT FROM FRONT\n");
printf("4.INSERT FROM REAR\n");
printf("5.DELETE FROM FRONT\n");
printf("6.DELETE FROM REAR\n");
printf("7.COUNT NODES\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();break;
case 2:display();break;
case 3:insertfront();break;
case 4:create();break;
case 5:deletefront();break;
case 6:deleterear();break;
case 7:node();break;
default:printf("Invalid Input");exit(0);
}
}
}
void create()
{
struct prof *newprof;
newprof=(struct prof*)malloc(sizeof(struct prof));
if(newprof==NULL)
{
printf("Out of memory");
exit(0);
}
printf("Enter student name\n");
scanf("%s",newprof->name);
printf("Enter student specialisation\n");
scanf("%s",newprof->specialisation);
printf("Enter prof branch\n");
scanf("%s",newprof->branch);
if(start==NULL)
{
temp=start=newprof;
temp->next=NULL;
temp->prev=NULL;
}
else
{
temp->next=newprof;
newprof->prev=temp;
temp=newprof;
temp->next=NULL;
}
}
void display()
{
struct prof *ptr;
if(start==NULL)
{
printf("List is empty");
return;
}
else
{
ptr=start;
printf("The elements are\n");
while(ptr!=NULL)
{
printf("%s\n",ptr->name);
printf("%s\n",ptr->specialisation);
printf("%s\n",ptr->branch);
ptr=ptr->next;
}
}
}
void insertfront()
{
struct prof* newprof;
newprof=(struct prof*)malloc(sizeof(struct prof));
if(newprof==NULL)
{
printf("Out of memory");
exit(0);
}
printf("Enter student name\n");
scanf("%s",newprof->name);
printf("Enter student usn\n");
scanf("%s",newprof->specialisation);
printf("Enter student branch\n");
scanf("%s",newprof->branch);
if(start==NULL)
{
temp=start=newprof;
temp->next=NULL;
temp->prev=NULL;
}
else
{
newprof->prev=NULL;
newprof->next=start;
start->prev=newprof;
start=newprof;
}
}
void deletefront()
{
if(start==NULL)
{
printf("List is empty");
return;
}
else
{
start=start->next;
free(start->prev);
start->prev=NULL;
}
}
void deleterear()
{
struct prof *temp;
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
if(start==NULL)
{
printf("List is empty");
return;
}
else
{
temp=temp->prev;
free(temp->next);
temp->next=NULL;
}
}
void node()
{
int count=0;
struct prof *temp;
temp=start;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
printf("The number of node is %d",count);
}
7.Given an array of elements, construct a complete binary tree from this array in level order
fashion. That is, elements from left in the array will be filled in the tree level wise starting from
arr[] = {1, 2, 3, 4, 5, 6}
/ \
2 3
/ / \
4 5 6
#include<stdio.h>
#include<stdlib.h>
struct NODE
int data;
};
node->data=data;
node->left=node->right=NULL;
return node;
}
Node* insertLevelWise(int arr[],Node* root,int i,int n)
if(i<n)
Node* temp=getNode(arr[i]);
root=temp;
root->left=insertLevelWise(arr,root->left,2*i+1,n);
root->right=insertLevelWise(arr,root->right,2*i+2,n);
return root;
if(root!=NULL)
inorderTrav(root->left);
printf("%d ",root->data);
inorderTrav(root->right);
int main()
int i,n,a[10];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
root=insertLevelWise(a,root,0,n);
inorderTrav(root);
return 0;
}
8. Design, Develop and Implement a menu driven Program in C for the following operations on
#include<stdio.h>
#include<stdlib.h>
struct BST
int data;
};
if(node == NULL)
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=data;
temp->left=temp->right=NULL;
return temp;
if(data<(node->data))
{
node->left=createtree(node->left,data);
else if(data>(node->data))
node->right=createtree(node->right,data);
return node;
if(node==NULL)
else if(data<(node->data))
node->left=search(node->left,data);
else if(data>(node->data))
node->right=search(node->right,data);
else
return node;
if(node!=NULL)
inorder(node->left);
printf("%d\t",node->data);
inorder(node->right);
if(node!=NULL)
printf("%d\t",node->data);
preorder(node->left);
preorder(node->right);
if(node!=NULL)
postorder(node->left);
postorder(node->right);
printf("%d\t",node->data);
void main()
int data,ch,i,n;
NODE *root=NULL;
while(1)
printf("\n3.Inorder");
printf("\n4.Preorder");
printf("\n5.Postorder");
printf("\n6.Exit");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&data);
root=createtree(root,data);
break;
case 2:
scanf("%d",&data);
root=search(root,data);
break;
case 3:
printf("\nInorder Traversal:\n");
inorder(root);
break;
case 4:
printf("\nPreorder Traversal:\n");
preorder(root);
break;
case 5:
printf("\nPostorder Traversal:\n");
postorder(root);
break;
case 6:
exit(0);
default:
break;
}
}
}
9. Design, Develop and implement a program in C for the following operations on Graph (G) of cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a diagraph using DFS/BFS
method.
#include<stdio.h>
#include<stdlib.h>
int a[20][20],q[20],visited[20],reach[10],n,i,j,f=0,r=-1,count=0;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
{
if(a[v][i]&&!reach[i])
{
printf("\n%d->%d",v,i);
count++;
dfs(i);
}
}
}
void main()
{
int v,choice;
printf("\nEnter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
for(i=1;i<=n-1;i++)
reach[i]=0;
printf("\nEnter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("1.BFS\n2.DFS\n3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the starting vertex:");
scanf("%d",&v);
bfs(v);
if((v<1)||(v>n))
{
printf("\nBS is not possible");
}
else
{
printf("\nThe nodes which are reachable from %d:\n",v);
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
}
break;
case 2:
dfs(1);
if(count==n-1)
printf("\nGraph is connected");
else
printf("\nGraph is not connected");
break;
case 3:
exit(0);
}
}
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.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct employee
int id;
char name[15];
};
EMP emp[MAX];
int a[MAX];
int key;
key=num%10;
return key;
scanf("%d",&emp[key].id);
scanf("%s",emp[key].name);
return key;
}
void display()
int i,ch;
printf("\n1.Display\n2.All\n3.Filtered Display\n");
scanf("%d",&ch);
if(ch==1)
for(i=0;i<MAX;i++)
printf("\n%d\t%d\t%s",i,emp[i].id,emp[i].name);
else
for(i=0;i<MAX;i++)
if(a[i]!=-1)
printf("\n%d\t%d\t%s",i,emp[i].id,emp[i].name);
continue;
{
int flag,i;
flag=0;
for(i=key+1;i<=MAX;i++)
if(a[i]==-1)
a[i]=getemp(emp,i);
flag=1;
break;
if(a[i]==-1)
a[i]=getemp(emp,i);
flag=1;
break;
if(!flag)
void main()
int num,key,i;
int ans=1;
a[i]=-1;
printf("%d",a[i]);
do
scanf("%d",&num);
key=create(num);
if(a[key]==-1)
a[key]=getemp(emp,key);
else
printf("collision detected\n");
linear_prob(key,num);
scanf("%d",&ans);
while(ans);
display();