DS Lab Manual
DS Lab Manual
DS Lab Manual
Compiled &Prepared by
Dr. Savita Choudary
Professor & Head
Dept. of IOT
MISSION
The Computer Science and Engineering department strives for excellence in teaching, applying,
promoting and imparting knowledge through comprehensive academic curricula.
Train students to effectively apply the knowledge to solve real-world problems, thus enhance
their potential for life-long high-quality career and give them a competitive advantage in the
ever-changing and fast paced computing.
Prepare students to demonstrate a sense of societal and ethical responsibilities in their
professional endeavors.
Creating amongst students and faculty a collaborative environment open to the free exchange of
ideas, which leads to research activity and fuels innovation thinking.
PROGRAM OUTCOMES
PO's PO Description
Engineering knowledge: Apply the knowledge of mathematics, science, engineering
PO1 fundamentals, and an engineering specialization to the solution of complex
engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze complex
PO2 engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
PO3
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and
PO4 research methods including design of experiments, analysis and interpretation of
data, and synthesis of the information to provide valid conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
PO5 engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge
PO6 to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
Environment and sustainability: Understand the impact of the professional engineering
PO7 solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and
PO8
responsibilities and norms of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member
PO9 or leader in diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
PO10
write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
PO11
member and leader in a team, to manage projects and in multidisciplinary
environments.
Life-long learning: Recognize the need for, and have the preparation and ability to
PO12 engage in independent and life-long learning in the broadest context of technological
change.
3. Develop 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 how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support
4.
for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, % (Remainder), ^
(Power) and alphanumeric operands.
Develop a Program in C for the following Stack Applications
5. a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^
b. Solving Tower of Hanoi problem with n disks
Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array
Implementation of Queue with maximum size MAX)
Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of
Employee Data with the fields: SSN, Name, Dept, Designation,Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
8. b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header
nodes
Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of Integers
.Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
10.
a. Traverse the BST in Inorder, Preorder and Post Order
b. Search the BST for a given element (KEY) and report the appropriate message
c. Exit
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in
file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as
the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers.
Develop a Program in C that uses Hash function H:
K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing.
● Experiment distribution
o For laboratories having only one part: Students are allowed to pick one experiment from
the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal opportunity.
● Change of experiment is allowed only once and marks allotted for procedure to be made zero of
the changed part only.
● Marks Distribution (Need to change in accordance with university regulations)
c) For laboratories having only one part – Procedure + Execution + Viva-Voce: 15+70+15 =
100 Marks
d) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks
DATA STRUCTURES LABORATORY BCSL305
#include <stdio.h>
#define MAX_DAY_NAME_LENGTH 20
struct Day {
char dayName[MAX_DAY_NAME_LENGTH];
int date;
char activityDescription[MAX_DESCRIPTION_LENGTH];
};
void read() {
scanf("%s", weekCalendar[i].dayName);
scanf("%d", &weekCalendar[i].date);
void display() {
int main() {
read();
display();
return 0;
ans[j]=str[c];
j++;c++;m=c;i=0;
}
}
ans[j]='\0';
strcopy(str,ans);
return flag;
int main()
{
char str[100],pat[20],rep[20];
int flag=0;
clrscr();
printf("Enter the string");
read(str);
printf("Enter the pattern");
read(pat);
printf("Enter the string to be replaced");
read(rep);
flag=matchnreplace(str,pat,rep);
if(flag==1)
printf("The string is found %s",str);
else
printf("The pattern is not found");
return 0;
}
3. Develop 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 how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations.
#include<stdio.h>
#define MAX 10
int top=-1;
int overflow()
{
if (top==MAX-1)
return 1;
else return 0;
}
int underflow()
{
if (top==-1)
return 1;
else return 0;
}
int main()
{
int stack[MAX],ch,i,j,ele,p;
char str[10];
while(1)
{
printf("\nEnter your choice:\n1:PUSH\n2:POP\n3:CHECK
PALINDROME\n4:DISPALY\n5.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to be pushed: n");
scanf("%d",&ele);
push(stack,ele);
break;
case 2: ele=pop(stack);
if(ele!=0)
printf("\nThe deleted element is: %d\n",ele);
break;
case 3:printf("\nEnter the string ");
fflush(stdin);
gets(str);
p=palin(str,stack);
if(p)
printf("\nThe given string is Palindrome");
else
printf("\nThe given string is not a palindrome");
break;
case 4:display(stack);
break;
default:exit(0);
}
}
return 0;
}
#include<stdio.h>
char stack[100];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
char ret;
ret=stack[top];
stack[top]='\0';
top--;
return ret;
}
int inper(char ch)
{
switch(ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
}
}
{
int n=0,i=0,j=0;
char ch;
ch=exprn[i++];
while(ch!='#')
{
switch(ch)
{
case '(':
push(ch);
break;
case ')': while(stack[top]!='(')
postfix[j++]=pop();
pop();
break;
case '+':
case '-':
case '*':
case '/': while(stper(stack[top])>=inper(ch))
postfix[j++]=pop();
push(ch);
break;
default:
postfix[j++]=ch;
break;
}
5 a)
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int stack[100];
int top=-1;
char exprn[20];
void push(int ele)
{
stack[++top]=ele;
}
int pop()
{
return (stack[top--]);
}
void compute(int opr1,char ch,int opr2)
{
switch(ch)
{
case '+' : push(opr1 + opr2); break;
case '-' : push(opr1 - opr2); break;
case '*' : push(opr1 * opr2); break;
case '/' : if(opr2!=0)
push(opr1/opr2);
else
{
printf("\nDivide by Zero Error");
exit(0);
}
break;
}
}
int eval()
{
int n=0,i=0,flag=1;
char ch;
int opr1,opr2;
ch=exprn[i++];
while(ch!='#')
{
Department of Computer Science and Engineering(IOT)
DATA STRUCTURES LABORATORY BCSL305
printf("\n%c",ch);
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
opr2=pop();
opr1=pop();
printf("\n%d %d %c",opr1,opr2,ch);
compute(opr1,ch,opr2);
break;
default:
push(ch-'0');
}
ch = exprn[i++];
}
return pop();
}
void main()
{
printf("\n Enter the postfix expn\t");
scanf("%s",exprn);
strcat(exprn,"#");
printf("\n After Evaluation %d",eval());
}
5 b)
#include<stdio.h>
#define MAX 10
typedef struct
{
int n;
int from;
int to;
int auxi;
int ret; // which case to return
}stack;
}
else
ret=2;
break;
case 2: push(n,beg,end,aux,3);
--n;
temp=end;
end=aux;
aux=temp;
ret=1;
break;
case 3: printf("\nMove %d: Move Disk %d from %c to %c",++nmoves,n,beg,end);
case 4: push(n,beg,end,aux,5);
--n;
temp=beg;
beg=aux;
aux=temp;
ret=1;
break;
case 5: if(empty())
return;
t=pop();
n=t.n;
beg=t.from;
aux=t.auxi;
end=t.to;
ret=t.ret;
break;
}
/*printf("\n Stack Content");
for(i=top;i>=0;i--)
printf("\n%d %d %d %d",st[i].n,st[i].from,st[i].to,st[i].auxi);*/
}
}
int main()
{
int n;
printf("\nEnter number of disks");
scanf("%d",&n);
hanoi(n,'A','C','B');
return 0;
}
6. Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 5
int Queue[MAX_SIZE];
int front = -1;
int rear = -1;
int count = 0;
int Qfull()
{
return count == MAX_SIZE;
}
int Qempty()
{
return count == 0;
}
void insert(int ele)
{
rear=(rear+1) % MAX_SIZE;
Queue[rear]=ele;
count++;
if(front == -1)
front++;
}
int deleteq()
{
int ret;
ret=Queue[front];
front=(front+1) % MAX_SIZE;
count--;
return(ret);
}
void display()
{
int i,j;
if(Qempty())
printf("\n Queue is empty\n");
else
{
j=front;
printf("\n Elements in the Queue\n");
for(i=0;i<count;i++)
{
printf(" %d(%d)",Queue[j],j);
j=(j+1) % MAX_SIZE;
}
}
}
void main()
{
int n=4,i,ch,ele,flag=1;
while(flag)
{
printf("\n Menu \n");
printf("\n1 Insert\n2 Delete\n3 Display\n4 Exit\n Enter the Choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(Qfull()) {
printf("\nQueue Full");
}
else
{
printf("\n Enter the value for insert\t");
scanf("%d",&ele);
insert(ele);
}
break;
case 2: if (!Qempty())
{
ele = deleteq();
printf("\n%d",ele);
}
else
printf("\nQueue Empty");
break;
case 3: display();
break;
default: flag = 0;
}
}
}
7. Develop a menu driven Program in C for the following operations on Singly Linked List (SLL)
of Student Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
#include<stdio.h>
#include<stdlib.h>
struct student
{
char USN[10];
char Name[20];
char Branch[15];
char Sem[3];
char PhNo[10];
};
typedef struct student STU;
struct node
{
STU info;
struct node *link;
};
typedef struct node* Node;
STU GetData()
{
STU temp;
printf("Enter the usn: ");
fflush(stdin);
gets(temp.USN);
printf("\nEnter the Name:");
fflush(stdin);
gets(temp.Name);
printf("\nEnter branch:");
fflush(stdin);
gets(temp.Branch);
printf("\nEnter sem:");
fflush(stdin);
gets(temp.Sem);
printf("\nEnter the phone number:");
fflush(stdin);
gets(temp.PhNo);
return temp;
}
return first;
}
Node Einsert(Node first,STU ele)
{
Node temp=Create(ele),cur;
if(first== NULL)
first=temp;
else
{
//for(cur=first;cur->link!=NULL;cur=cur->link);
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
}
return first;
}
Page | 20
DATA STRUCTURES LABORATORY BCSL305
return first;
}
Node Edelete(Node first)
{
Node temp=first,t;
if(first== NULL)
printf("\nList is empty");
else if(first->link==NULL)
{
printf("\nElement Deleted is\n");
printf("USN\t\tName\t\tBranch\tSem\tPhno\n");
displayRec(first->info);
first=NULL;
}
else
{
//for(temp=first;temp->link->link!=NULL;temp=temp->link);
while(temp->link->link!=NULL)
temp=temp->link;
printf("\nElement Deleted is\n");
printf("USN\t\tName\t\tBranch\tSem\tPhno\n");
displayRec(temp->link->info);
t = temp->link;
temp->link=NULL;
free(t);
}
return first;
}
void main()
{
Node first=NULL;
int flag=1,ch;
STU ele;
while(flag)
{
printf("\n Menu \n");
printf("\n1 Front Insert\n2 End Insert\n3 Front Delete\n4 End Delete\n5 Display\n6 No. of
Nodes in the list\n");
printf("7 Exit\n Enter the Choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the Student Details \t");
ele=GetData();
first=Finsert(first,ele);
break;
8. Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
#include<stdio.h>
#include<stdlib.h>
struct Employee
{
char SSN[10];
char Name[10];
char Branch[10];
char Des[10];
char sal[10];
char phone[10];
};
typedef struct Employee EMP;
struct node
{
EMP info;
struct node *lptr,*rptr;
};
typedef struct node* Node;
Node front=NULL,rear=NULL;
EMP GetRec()
{
EMP temp;
printf("Enter the SSN: ");
fflush(stdin);
gets(temp.SSN);
printf("Enter the Name:");
fflush(stdin);
gets(temp.Name);
printf("Enter branch:");
fflush(stdin);
gets(temp.Branch);
printf("Enter the designation: ");
fflush(stdin);
gets(temp.Des);
printf("Enter sal:");
fflush(stdin);
gets(temp.sal);
printf("Enter the phone number:");
fflush(stdin);
gets(temp.phone);
return temp;
}
void DispRec(EMP temp)
{
printf("%s\t",temp.SSN);
printf("%s\t",temp.Name);
printf("%s\t",temp.Branch);
printf("%s\t",temp.Des);
printf("%s\t",temp.sal);
printf("%s\n",temp.phone);
}
Node Create(EMP ele)
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
temp->info=ele;
temp->lptr=NULL;
temp->rptr=NULL;
return temp;
}
void Finsert(EMP ele)
{
Node temp=Create(ele);
if(front== NULL)
{
front=temp;
rear=temp;
}
else
{
temp->rptr=front;
front->lptr=temp;
front = temp;
}
}
}
else
{
rear->rptr=temp;
temp->lptr=rear;
rear=temp;
}
}
void Fdelete()
{
Node temp=front;
if(front == NULL)
printf("\nList is empty");
else
{
temp=front;
printf("\nDeleted Employee Records \n");
DispRec(temp->info);
front = front->rptr;
front->lptr= NULL;
if(front == NULL)
rear = NULL;
free(temp);
}
}
void Edelete()
{
Node temp=rear,t;
if(rear== NULL)
printf("\nList is empty");
else if(rear->lptr == NULL)
{
printf("\nDeleted Employee Records \n");
DispRec(rear->info);
front=rear=NULL;
free(temp);
}
else
{
rear = rear->lptr;
rear->rptr=NULL;
printf("\nDeleted Employee Records \n");
DispRec(temp->info);
free(temp);
}
}
Department of Computer Science and Engineering(IOT)
DATA STRUCTURES LABORATORY BCSL305
9. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z). Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct term
{
int coeff;
int pow_x;
int pow_y;
int pow_z;
};
typedef struct term TERM;
struct node
{
TERM info;
struct node* next;
};
typedef struct node* Node;
Node Create(TERM t)
{
Node temp = (Node)malloc(sizeof(struct node));
temp->info=t;
temp->next = temp;
return temp;
}
Node MKHeader()
{
TERM x;
Node temp;
x.coeff=-1;
x.pow_x=-1;
x.pow_y=-1;
x.pow_z=-1;
temp=Create(x);
return temp;
}
Node Insert(Node p,TERM t)
{
Node temp = Create(t),cur=p;
while(cur->next!=p)
cur=cur->next;
cur->next=temp;
temp->next=p;
return p;
}
double Compute(Node temp,int x,int y,int z)
{
double ret;
TERM t=temp->info;
ret=t.coeff * pow(x, t.pow_x) * pow(y, t.pow_y) * pow(z,t.pow_z);
return ret;
}
double Evaluate(Node p, int x, int y, int z)
{
Node po = p->next;
double sum = 0;
while (po!=p)
{
sum += Compute(po,x,y,z);
po = po->next;
}
return sum;
}
void DispTerm(TERM t)
{
printf(" %dx^%dy^%dz^%d ", t.coeff, t.pow_x, t.pow_y, t.pow_z);
}
void PrintPoly(Node p)
{
Node po = p->next;
while (po!=p)
{
DispTerm(po->info);
if(po->next!=p)
printf("+");
po = po->next;
}
printf("\n");
}
void ReadPoly(Node t1,int n)
{
TERM t;
int i;
for(i=1;i<=n;i++)
{
printf("Enter the value of coefficent and powers of x,y and z");
scanf("%d%d%d%d",&t.coeff,&t.pow_x,&t.pow_y,&t.pow_z);
t1 = Insert(t1, t);
}
PrintPoly(t1);
}
int ComparePower(TERM m,TERM n)
{
if(m.pow_x==n.pow_x && m.pow_y==n.pow_y && m.pow_z==n.pow_z)
return 1;
else
return 0;
}
{
t3=Newlist->next;
flag=1;
while(t3!=Newlist && flag)
{
if(ComparePower(t3->info,t4->info))
flag=0;
t3=t3->next;
}
if (flag==1)
Newlist=Insert(Newlist,t4->info);
t4=t4->next;
}
return Newlist;
}
int main()
{
int n,x,y,z,ch,i,coeff;
Node polysum;
Node poly1 = MKHeader();
Node poly2 = MKHeader();
while(1)
{
printf("\nMenu\n 1:Evaluate Polynomial \n 2:Add\n 3:Exit\n Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the terms in the polynomial");
scanf("%d",&n);
ReadPoly(poly1,n);
printf("Enter the values of x,y and z");
scanf("%d%d%d",&x,&y,&z);
printf("%.2f\n", Evaluate(poly1, x, y, z));
break;
case 2: printf("Enter the terms in the polynomial 2");
scanf("%d",&n);
ReadPoly(poly2,n);
polysum = AddPoly(poly1, poly2);
PrintPoly(polysum);
break;
case 3: exit(0);
}
}
return 0;
10. Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
#include<stdio.h>
struct node
{
int data;
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
Node insert(Node node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->data)
return node;
}
int search(Node root, int key)
{
if (root == NULL)
return -1;
if(root->data == key)
return 1;
if (root->data < key)
return search(root->right, key);
return search(root->left, key);
}
inorder(root->left);
printf("%d \t", root->data);
inorder(root->right);
}
}
{
printf("%d \t", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d \t", root->data);
}
}
int main()
{
int n,i,ch,ch1,key,pos;
Node root=NULL;
printf("Enterthe no of nodes in the BST\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the element to be iserted\n");
scanf("%d",&key);
root=insert(root,key);
}
while(1)
{
printf("\nEnter the choice\n1: Insert Node\n2: Traversal\n3: Search for key\n4: Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
break;
case 2:
printf("Enter your choice\n1: Preorder\n2: Inorder\n3:
Postorder\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
preorder(root);
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
default:
printf("\n Make Correct Choice");
}
break;
case 3:
printf("Enter the key to be searched\n");
scanf("%d",&key);
pos=search(root,key);
if (pos==-1)
return 0;
}
#include<stdio.h>
#include<conio.h>
int count=0;
void creategraph(int n,int a[10][10])
{
int i,j;
printf("enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
void bfs(int a[10][10], int n, int source,int s[])
{
int f,r,q[10],u,v,i;
printf("enter the source vertex\n");
scanf("%d",&source);
printf("the nodes reachable are\n");
for(i=1;i<=n;i++)
s[i]=0;
f=r=0;
q[r]=source;
s[source]=1;
while(f<=r)
{
u=q[f++];
for(v=1;v<=n;v++)
{
if(a[u][v]==1 && s[v]==0)
{
printf("\n%d",v);
s[v]=1;
q[++r]=v;
}
}
}
}
void dfs(int a[10][10],int n,int vis[],int v)
{
int i;
count++;
vis[v]=count;
for(i=1;i<=n;i++)
{
if(vis[i]==0 && a[v][i]==1)
dfs(a,n,vis,i);
}
}
int main()
{
int a[20][20],n,source,s[10],i,j,vis[10];
int ch;
while(1)
{ printf("\nEnter your choice\n1:Create graph\n2:Check Reachability\n3:Check
Connectivity\n4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of nodes:");
scanf("%d",&n);
creategraph(n,a);
break;
case 2: bfs(a,n,source,s);
break;
case 3:for(i=1;i<=n;i++)
vis[i]=0;
dfs(a,n,vis,1);
if(count==n)
printf("the graph is connected\n");
else
printf("the graph is not connected\n");
break;
case 4: exit(0);
}
}
return 0;
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys
in K and addresses in L are Integers. Develop a Program in C that uses Hash function H:
K →L as H(K)=K mod m (remainder 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>
#definre max 100
}
}
}
void display(int a[max])
{
int i;
printf(“\n the hash table is…\n”);
printf(“key/index\t employee id\n”);
printf(“ ........................................................... \n”);
for(i=10;i<max;i++)
printf(“\n %d %d\n”, i, a[i]);
}
int main()
{
int a[max],num,key,i;
int ans;
printf(“collision handling by linear probing\n”);
for(i=10;i<max;i++) a[i]=-1;
do
{
printf(“\n enter the four digit number:”);
scanf(“%d”,&num);
key=create(num);
linear_pron(a,keyt,num);
printf(“do you want to continue?\n”);
scanf(“%d”,&ans);
}while(ans==1);
display(a);
}
VIVA-VOICE QUESTIONS
Eg:int x =5,*p=&x,**q=&p;
A pointer which is not allowed to be altered to hold another address after it is holding one.
15. What is the difference between variable declaration and variable definition?
Declaration associates type to the variable whereas definition gives the value to the variable.
StackOperations:-
Push – Pushes (inserts) the element in the stack. The location is specified by the pointer. Pop
– Pulls (removes) the element out of the stack. The location is specified by the pointer Swap: –
the two top most elements of the stack can be swapped Peek: – Returns
the top element on the stack but does not remove it from the stack Rotate:- the topmost
(n) items can be moved on the stack in a rotating fashion A stack has a fixed location
in the memory. When a data element is pushed in the stack, the pointer pointsto the current element.
26. Which data structures is applied when dealing with a recursive function?
Recursion, which is basically a function that calls itself based on a terminating condition, makes use of
the stack. Using LIFO, a call to a recursive function saves the return address so that it knows how to return
to the calling function after the call terminates.
33. What is the minimum number of queues needed to implement the priority queue?Two. One
queue is used for the actual storing of data, and the other one is used for storing the priorities.
35. What are some of the applications for the tree data structure?
1-Manipulation of the arithmetic expressions.
2-Symbol table construction.
3- Syntax analysis.
53. Which data structures is applied when dealing with a recursive function?
Recursion, which is basically a function that calls itself based on a terminating condition, makes use of
the stack. Using LIFO, a call to a recursive function saves the return address so that it knows how to return
to the calling function after the call terminates.