0% found this document useful (0 votes)
19 views51 pages

21CS32 L

Uploaded by

AKHILA R
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)
19 views51 pages

21CS32 L

Uploaded by

AKHILA R
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/ 51

1.

Design, Develop and Implement a menu driven Program in C for the following Array Operations

a. Creating an Array of N Integer Elements

b. Display of Array Elements with Suitable Headings

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 1 to create an array of n elements:\t");

printf("\nEnter 2 to display element in array:\t");

printf("\nEnter 3 to Exit:\t");

printf("\nEnter the choice \n");

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:

printf("Enter a valid choice\n");

getch();

void create()

int i;

printf("Enter %d elements\n", n);

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

a. Inserting an Element (ELEM) at a given valid Position (POS)

b. Deleting an Element at a given valid Position POS)

c. Display of Array Elements

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

int ch, pos, value;

clrscr();

while(1)

printf("\nEnter 1 to create an array of n elements:");

printf("\nEnter 2 to display element in array:");


printf("\nEnter 3 to insert an element at POS in array:");

printf("\nEnter 4 to delete an element from POS in array:");

printf("\nEnter 5 to Exit:");

printf("\nEnter the choice:\n ");

scanf("%d" , &ch);

switch(ch)

case 1:

printf("Enter number of elements in array\t:");

scanf("%d", &n);

create();

break;

case 2:

display();

break;

case 3:

printf ("Enter the position where you wish to insert an element\t:");

scanf ("%d", &pos);

printf ("Enter the value to insert in array\t:");

scanf ("%d", &value);

insert(pos,value);

break;

case 4:

printf ("Enter the position where you wish to delete element\t:");

scanf ("%d", &pos);

del(pos);

break;
case 5:

exit(0);

default:

printf("Enter a valid choice");

break;

getch();

void create()

int i;

printf("Enter %d elements\n", n);

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

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

void insert (int pos ,int value)

int i;

for(i=n-1; i>=pos-1; i--)

a[i+1]=a[i];

a[pos-1]=value;

n=n+1;
printf("\nArray Elements Updated.\n");

void del(int pos)

int i;

if ( pos >= n+1 )

printf("Deletion not possible.\n");

else

for ( i = pos-1 ; i < n-1; i++ )

a[i] = a[i+1];

n=n-1;

printf("Element deleted\n");

void display()

int i;

printf("Elements in the array :\t");

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

printf("%d\t",a[i]);

}
3. Design, Develop and Implement a menu driven Program in C for the following operations on

STACK of Integers (Array Implementation of Stack with maximum size MAX)

a. Push an Element on to Stack

b. Pop an Element from Stack

c. Demonstrate Overflow and Underflow situations on Stack

d. Display the status of Stack

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

printf("\nEnter your choice:\t");

scanf("%d",&ch);

switch(ch)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Enter valid number");

break;

getch();

void push()
{

int item;

if(top==(max-1))

printf("Stack overflow");

else

top+=1;

printf("Enter the element:\t");

scanf("%d",&item);

a[top]=item;

void pop()

int item;

if(top==-1)

printf("Stack underflow");

else

item=a[top];

top-=1;

printf("The value popped is: %d",item);

void display()

int i;
if(top<0)

printf("Stack underflow");

else{

printf("Elements in stack are");

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

a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

b. Solving Tower of Hanoi problem with n disks

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;

printf("Enter the suffix expression\n");

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

printf("\n\t The result of %s=%.2f",suffix,result);

float evaluate(char s[],float d[])

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>

void tower(int n,char BEG,char AUX,char END)

if(n==1)

printf("MOVE THE DISK FROM %c TO %c\n",BEG,END);

else

tower(n-1,BEG,END,AUX);

tower(1,BEG,AUX,END);

tower(n-1,AUX,BEG,END);

void main()

int num;

printf("Enter the number of disks\n");

scanf("%d",&num);;

printf("\nThe number of sequence to move the disk are\n");

tower(num,'A','B','C');

}
5. Singly Linked List (SLL) of Integer Data

a. Create a SLL stack of N integer.

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;

struct node *next;

};

struct node *head;

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("3.Insert at any random location\n");

printf("4.Delete from beginning\n");

printf("5.Delete from last\n");

printf("6.delete node after specified location\n");

printf("7.Search for an element\n");

printf("8.Show\n");

printf("9.Exit\n");

printf("\nEnter your choice:\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:

printf("please enter valid choice");

break;

void beginsert()

{
struct node *ptr;

int item;

ptr=(struct node*)malloc(sizeof(struct node*));

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

struct node *ptr,*temp;

int item;

ptr=(struct node*)malloc(sizeof(struct node));

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;

struct node *ptr,*temp;


ptr=(struct node*)malloc(sizeof(struct node));

if(ptr==NULL)

printf("\nOVERFLOW");

else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data=item;

printf("\nEnter the location after which you wnat to insert");

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

struct node *ptr;

if(head==NULL)

printf("\nList is empty\n");

else

ptr=head;

head=ptr->next;

free(ptr);

printf("\nNode deleted from the beginning\n");

void last_delete()

struct node *ptr,*ptr1;

if(head==NULL)

printf("\nlist is empty");

else if(head->next==NULL)

head=NULL;
free(head);

printf("\nOnly node of the list deleted\n");

else

ptr=head;

ptr=ptr->next;

ptr1->next=NULL;

free(ptr);

printf("\nDeleted node from the last");

void random_delete()

struct node *ptr,*ptr1;

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

printf("\nDeleted node %d",loc+1);

void search()

struct node *ptr;

int item,i=0,flag;

ptr=head;

if(ptr==NULL)

printf("\nEmpty list\n");

else

printf("\nEnter item which you want to search\n");

scanf("%d",&item);

while(ptr!=NULL)

if(ptr->data==item)

printf("item found at location %d",i+1);

flag=0;

else
{

flag=1;

i++;

ptr=ptr->next;

if(flag==1)

printf("item not found\n");

void display()

struct node *ptr;

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

level 0. Ex: Input :

arr[] = {1, 2, 3, 4, 5, 6}

Output : Root of the following tree

/ \

2 3

/ / \

4 5 6

#include<stdio.h>

#include<stdlib.h>

struct NODE

int data;

struct NODE *left, *right;

};

typedef struct NODE Node;

Node* getNode(int data)

Node* node=(Node *)malloc(sizeof(struct NODE));

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;

void inorderTrav(Node* root)

if(root!=NULL)

inorderTrav(root->left);

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

inorderTrav(root->right);

int main()

int i,n,a[10];

struct NODE* root;

printf("Enter the number of Nodes\n");


scanf("%d",&n);

printf("\nEnter the array elements\n");

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

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

root=insertLevelWise(a,root,0,n);

printf("Inoder Traversal of created tree:");

inorderTrav(root);

return 0;

}
8. Design, Develop and Implement a menu driven Program in C for the following operations on

Binary Search Tree (BST) of Integers

a. Create a BST of N Integers

b. Traverse the BST in Inorder, Preorder and Post Order

#include<stdio.h>

#include<stdlib.h>

struct BST

int data;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;NODE *node;

NODE *createtree(NODE *node,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;

NODE *search(NODE *node,int data)

if(node==NULL)

printf("\nElement not found\n");

else if(data<(node->data))

node->left=search(node->left,data);

else if(data>(node->data))

node->right=search(node->right,data);

else

printf("\nElement found is : %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!=NULL)

printf("%d\t",node->data);

preorder(node->left);

preorder(node->right);

void postorder(NODE *node)

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("\n1.Insertion in binary search tree");

printf("\n2.Search element in binary search tree");

printf("\n3.Inorder");

printf("\n4.Preorder");

printf("\n5.Postorder");

printf("\n6.Exit");

printf("\nEnter your choice");

scanf("%d",&ch);

switch(ch)

case 1:

printf("\nEnter N value :");

scanf("%d",&n);

printf("\nEnter values to create BST\n");

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

{
scanf("%d",&data);

root=createtree(root,data);

break;

case 2:

printf("\nEnter the element to search;");

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:

printf("\nEnter the valid choice:");

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

};

typedef struct employee EMP;

EMP emp[MAX];

int a[MAX];

int create(int num)

int key;

key=num%10;

return key;

int getemp(EMP emp[],int key)

printf("\nEnter emp id:");

scanf("%d",&emp[key].id);

printf("\nEnter emp name:");

scanf("%s",emp[key].name);

return key;
}

void display()

int i,ch;

printf("\n1.Display\n2.All\n3.Filtered Display\n");

printf("\nEnter your choice: ");

scanf("%d",&ch);

if(ch==1)

printf("\nThe hash table is:\n");

printf("\nHT Key\tEmp ID\tEmp Name");

for(i=0;i<MAX;i++)

printf("\n%d\t%d\t%s",i,emp[i].id,emp[i].name);

else

printf("\nThe hash table is:\n");

printf("\nHT Key\tEmpID\tEmp Name");

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;

void linear_prob(int key,int num)

{
int flag,i;

flag=0;

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

if(a[i]==-1)

a[i]=getemp(emp,i);

flag=1;

break;

for(i=0;i<key && flag==0;i++)

if(a[i]==-1)

a[i]=getemp(emp,i);

flag=1;

break;

if(!flag)

printf("Hash table full:");

void main()

int num,key,i;

int ans=1;

printf("\nCollision handling by linear probing:");


for(i=0;i<MAX;i++)

a[i]=-1;

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

do

printf("\nEnter the data:\n");

scanf("%d",&num);

key=create(num);

if(a[key]==-1)

a[key]=getemp(emp,key);

else

printf("collision detected\n");

linear_prob(key,num);

printf("\nDo yopu wish to continue?(1/0):");

scanf("%d",&ans);

while(ans);

display();

You might also like