DS Lab Manual-1
DS Lab Manual-1
Course objectives:
This laboratory course enables students to get practical experience in design, develop,
implement, analyse and evaluation/testing of
Asymptotic performance of algorithms.
Linear data structures and their applications such as Stacks, Queues and Lists
Non-Linear Data Structures and their Applications such as Trees and Graphs
Sorting and Searching Algorithms
Laboratory Experiments:
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. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position (POS)
e. Exit.
Support the program with functions for each of the above operations.
2. Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.
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 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
4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized expressions
with the operators: +, -, *, /, % (Remainder), ^(Power) and alphanumeric operands.
5. 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
6. Design, Develop and Implement 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.
7. Design, Develop and Implement a menu driven Program in C for the following operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, 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
8. Design, Develop and Implement 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
9. Design, Develop and Implement a Program in C for the following operations on 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.
10. 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: 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
e. Exit
11. 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 digraph using DFS/BFS method
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. Design and 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.
Course outcomes:
On the completion of this laboratory course, the students will be able to:
Analyse and Compare various linear and non-linear data structures
Code, debug and demonstrate the working nature of different types of data structures and
their applications
Implement, analyse and evaluate the searching and sorting algorithms
Choose the appropriate data structure for solving real world problems
#include <stdio.h>
#include<stdlib.h>
#define SIZE 10
int a[SIZE], n;
int Create ()
{
int i;
void Display ()
{ int i;
void Insert ()
{
int ele, pos, i;
a[pos-1] = ele;
void Delete ()
{
int pos, i;
int main ()
{
int choice, n, a[SIZE];
char ans = 'y';
while (1)
{
printf ("\n1. Create\n2. Display\n3. Insertion\n4. Deletion\n5. Exit\n ");
printf ("Enter your choice :");
scanf ("%d", &choice);
switch (choice)
{
case 1:
n = Create ();
break;
case 2:
Display ();
break;
case 3:
Insert ();
break;
case 4:
Delete ();
break;
case 5:
exit (0);
}
}
}
Output
Array Operations :
====================
1. Create
2. Display
3. Insertion
4. Deletion
5. Exit
Enter your choice :1
Enter number of elements : 5
Enter element #1 :1
Enter element #2 :2
Enter element #3 :3
Enter element #4 :4
Enter element #5 :5
1. Create
2. Display
3. Insertion
4. Deletion
5. Exit
Enter your choice :3
Enter the position to insert :3
#include <stdio.h>
#include<stdlib.h>
#define SIZE 50
int k = i-len;
int m = 0;
//Replacing main string character by character
while ( rep[m] != '\0')
{
str[k] = rep[m];
m++;
k++;
}
}
else
i++;
}
if (count1 == 0)
printf (" The total number of occurances %d\n", count1);
printf (" The main string after replacement is %s\n\n", str);
}
int main ()
{
int choice;
char str[SIZE], pat[SIZE], rep[SIZE];
char ans = 'y';
switch (choice)
{
case 1:
Create (str, pat, rep);
break;
case 2:
FindReplace (str, pat, rep);
break;
case 3:
exit(0);
}
}
}
Output:
Pattern Matching
==================
1. Read Strings
2. Find and Replace Pattern
3. Exit
Enter your choice : 1
Enter the main string :managenageagement
Enter the pattern string : age
Enter the replacement string : YYY
1. Read Strings
2. Find and Replace Pattern
3. Exit
Enter your choice : 3
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 how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
#include<stdio.h>
#include<stdlib.h>
#define MAX1 3
#define MAX2 10
int pop1()
{
int item;
if(top1==-1)
{
printf("\n Stack Underflow \n");
return;
}
else
{
item=s1[top1];
printf ("The deleted item is : %d", item);
top1--;
}
return item;
}
void display()
{
int i;
if(top1==-1)
{
printf("\n Empty Stack\n");
return;
}
else
{
printf("\n The elements of the stack are : ");
for(i=top1; i>=0; i--)
printf("\n %d",s1[i]);
}
}
int pop2()
{
int item;
if(top2==-1)
{
printf("\n Stack Underflow \n");
return;
}
else
{
item=s2[top2];
top2--;
}
return item;
}
while(n!=0)
{
rem=n%10;
push2 (rem);
n/=10;
}
while (n1 != 0)
{
rem = n1%10;
item = pop2 ();
if (rem !=item)
{
printf("Number is not palindrome");
return;
}
n1/=10;
}
printf("\n Number is palindrome");
}
void main()
{
int s[MAX1], top1 = -1, ch, item;
for(;;)
{
printf("\n\n1. Push 2. Pop 3. Check Palindrome 4. Exit\n");
switch(ch)
{
case 1:
printf("\n Enter the element to be inserted : ");
scanf("%d",&item);
push1(item);
display();
break;
case 2:
item=pop1();
display();
break;
case 3:
printf("\n Enter the element to be checked : ");
scanf("%d",&item);
check_pal(item);
break;
default: exit(1);
}
}
}
Output
Number is palindrome
Stack Underflow
Empty Stack
Stack Overflow
Number is palindrome
Stack Overflow
Stack Underflow
Empty Stack
#include<stdio.h>
#include<string.h>
int in_prec(char);
int stack_prec(char);
default : return 8;
}
}
s [++top]='#';
for (i = 0; in[i] != '\0'; i++)
{
sym = in[i];
while (stack_prec (s[top]) > in_prec(sym))
{
post[j++] = s[top--];
}
If (stack_prec(s[top]) != in_prec(sym))
s[++top]=sym;
else top--;
}
while (s[top] != '#')
post [j++]=s[top--];
post [j]='\0';
}
void main ()
{
char in[30], post[30];
Output
Enter a valid infix expression
a*(b+c)
Postfix expression is abc+*
#include <stdio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
switch (symbol )
{
case '+' : return op1 + op2;
case '-' : return op1 - op2;
case '*' : return op1 * op2;
case '/' : return op1 / op2;
}
}
void main ()
{
double s[20], res, op1, op2;
int top = -1, i;
char postfix[20], symbol;
Output
Enter postfix expression :23+5*
The expression is :23+5*
The result is 25.00
#include<stdio.h>
#include<ctype.h>
float pop()
{
float num;
num=stack[top];
top=top-1;
return(num);
}
float eval()
{
int i;
float op1,op2,r;
i=0;
while(postfix[i]!='\0')
{
if(isalpha(postfix[i]))
push(value[i]);
else
{
op2=pop();
op1=pop();
switch(postfix[i])
{
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
}
}
i++;
}
r=pop();
return(r);
}
void main()
{
int i;
float result;
Output
Enter postfix expression: abc+/
Enter value of a : 4
Enter value of b : 2
Enter value of c : 1
The value of the postfix expression is 1.33
Enter postfix expression: abc/+
Enter value of a : 4
Enter value of b : 2
Enter value of c : 1
The value of the postfix expression is 6.00
#include<stdio.h>
#include <stdlib.h>
#define SIZE 4
void InsertQ (int q[], int *count, int *r, int item)
{
if (*count == SIZE)
{
printf ("Queue Overflow \n");
return;
}
*r = (*r+1) % SIZE;
q[*r] = item;
(*count)++;
}
void DeleteQ (int q[], int *count, int *f, int *r)
{
if (*count == 0)
{
printf ("Empty Queue\n");
*f = 0, *r = -1;
return;
}
printf ("Item deleted is %d\n", q[*f]);
*f = (*f+1)% SIZE;
(*count)--;
return;
}
void Display (int q[], int *f, int *r, int *count)
{
int i;
if ( *count == 0)
{
printf ("Queue is Empty\n");
}
printf ("The contents of the Queue is : ");
for (i = *f; i<= *r; i++)
printf ("%d\t", q[i]);
}
void main()
{
int q[SIZE], item, option;
int f=0, r= -1, count=0;
switch (option)
{
case 1:
printf ("\nEnter item to be inserted :");
scanf ("%d", &item);
InsertQ (q, &count, &r, item);
if (count)
Display (q, &f, &r, &count);
break;
case 2:
DeleteQ (q, &count, &f, &r);
if (count)
Display (q, &f, &r, &count);
break;
default:
exit (0);
}
}
}
Output:
Queue Operations :
================
1: Insert 2: Delete 3: Exit
Enter option :2
Empty Queue
#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
char usn[10];
char name[20];
char branch[5];
int sem;
char phone[15];
struct node *link;
};
typedef struct node *NODE;
NODE start = NULL;
void FrontInsert ()
{
NODE z;
z->link = NULL;
if (start == NULL)
{
start = z;
return;
}
z->link = start;
start = z;
return;
}
void FrontDelete ()
{
NODE temp;
if (start == NULL)
{
printf("Empty List\n");
return;
}
else
{
temp = start;
start = start->link;
printf("Node Deleted");
printf ("\n%s %s %s %d %s", start->usn, start->name, start->branch, start-
>sem, start->phone);
free (temp);
}
return;
}
void EndInsert()
{
NODE z, temp;
z->link = NULL;
if (start == NULL)
{
start = z;
return;
}
temp = start;
while (temp->link != NULL)
temp = temp->link;
temp->link = z;
return;
}
void EndDelete()
{
NODE prev, cur;
if(start == NULL)
{
printf ("Empty List\n");
return;
}
if (start->link == NULL)
{
printf("Node Deleted");
printf ("\n%s %s %s %d %s", start->usn, start->name, start->branch, start-
>sem, start->phone);
start = NULL;
return;
}
prev = NULL;
cur = start;
void Display ()
{
NODE temp;
if (start == NULL)
{
printf ("List is empty\n");
return;
}
temp = start;
while (temp != NULL)
{
void main ()
{
int option, item, i, n;
switch (option)
{
case 1:
printf("\nEnter number of nodes : ");
scanf ("%d", &n);
Output
Singly Linked List Operations
-----------------------------
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :1
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :2
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :4
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :3
Node Deleted
300 chiru arch 7 7478974981
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :5
Node Deleted
500 arjun dip 3 349235439
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :3
Node Deleted
200 raj civ 8 9637468237
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :5
Node Deleted
100 hema cse 7 9467841912
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :4
The list is :
USN Name Branch Sem Phone
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :3
Node Deleted
900 yyy eee 6 784239753
The list is :
USN Name Branch Sem Phone
Enter Option :5
Node Deleted
900 yyy eee 6 784239753
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :5
Empty List
1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete
Enter Option :6
8. Design, Develop and Implement 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<string.h>
#include<malloc.h>
#include <stdlib.h>
int count=0;
struct node
{
struct node *prev;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void create()
{
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp->next = NULL;
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void DFrontInsert ()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
void DEndInsert ()
{
if(h==NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void Display ()
{
temp2 =h;
if(temp2 == NULL)
{
printf("List empty to display \n"); return;
}
printf("\n Employee Information : \n");
while (temp2!= NULL)
{
printf("%d %s %s %s %.2f %d\n", temp2->ssn, temp2->name,temp2-
>dept,temp2->desg,temp2->sal, temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
}
int DEndDelete ()
{
struct node *temp; temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL; return 0;
}
else
{
temp2=temp1->prev; temp2->next=NULL;
free(temp1);
}
count--;
return 0;
}
int DFrontDelete ()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
}
else
{
h=h->next;
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp-
>desg,temp->sal, temp->phno );
free(temp);
}
count--; return 0;
}
void main()
{
int ch,n,i; h=NULL;
default:
printf("Wrong Choice\n");
}
}
}
Output:
-----------------MENU--------------------
Enter choice : 1
Enter no of employees : 3
Enter choice : 2
Employee Information :
100 Hema CSE AssocProf 130000.00 998479823
200 Raj Civil HoD 100000.00 974894983
300 Chiru Arch Developer 50000.00 953897592
No of employees = 3
Enter choice : 4
300 Chiru Arch Developer 50000.000000 953897592
Enter choice : 6
100 Hema CSE AssocProf 130000.000000 998479823
Enter choice : 2
Employee Information :
200 Raj Civil HoD 100000.00 974894983
No of employees = 1
Enter choice : 5
Enter choice : 3
Enter choice : 2
Employee Information :
500 Arjun IT Engineer 55000.00 957875424
200 Raj Civil HoD 100000.00 974894983
No of employees = 3
Enter choice : 6
500 Arjun IT Engineer 55000.000000 957875424
Enter choice : 7
9. Design, Develop and Implement a Program in C for the following
operations on Singly Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P (x, y, z) = 6x y z-4yz +3x yz+2xy z-
2 2 5 3 5
2xyz 3
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<conio.h>
#include<alloc.h>
#include<process.h>
#include<math.h>
struct node
{
float cf;
int px,py,pz;
int flag;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE) malloc (sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
{
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
}
p2=p2->link;
}
return h3;
}
head=insert_rear(cf,px,py,pz,head);
}
return head;
}
void polysum()
{
NODE h1,h2,h3;
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
printf("enter the first polynominal\n");
h1=read_poly(h1);
printf("enter the second polynominal\n");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf(" the first polynominal is\n");
display(h1);
printf("second polynominal is\n");
display(h2);
printf("the sum of two polynominal is\n");
display(h3);
}
void represent_evaluate()
{
NODE e1,temp;
int x,y,z;
float sum=0.0;
e1=getnode();
e1->link=e1;
printf("enter the polynominal\n");
e1=read_poly(e1);
printf("polynominal i s \n");
display(e1);
printf("enter the values of coefficient\n");
scanf("%d%d%d",&x,&y,&z);
if(e1==NULL)
{
printf("list is empty");
}
else
{
temp=e1->link;
while(temp!=e1)
{
sum+=temp->cf*pow(x,temp->px)*pow(y,temp->py)*pow(z,temp-
>pz);
temp=temp->link;
}
// sum+=temp->cf*pow(x,temp->px)*pow(y,temp->py)*pow(z,temp->pz);
printf("the total sum is %f\n",sum);
}
return;
}
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n\n\n\t1.represent and evaluate...\t2.ADD TWO poly..\t3.Exit...");
printf("\n\n\n\tEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: represent_evaluate();break;
case 2:polysum();break;
case 3:exit(0);
default: printf("\n\n\n\tEnter proper Choice....");
}
}
}
10. 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: 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
e. Exit
# include <stdio.h>
# include <stdlib.h>
struct BST
{
int item;
struct BST *llink, *rlink;
};
typedef struct BST* NODE;
if (root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if (item < cur-> item)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item < prev->item)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
return root;
void main()
{
int choice, key;
NODE root = NULL, tmp, parent;
while(1)
{
printf("\n\n\n1.Create");
printf("\n2.Traverse the Tree in Preorder, Inorder, Postorder");
printf("\n3.Search");
printf("\n4.Delete an element from the Tree");
printf("\n5.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = insert(root);
break;
case 2:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key);
if(tmp == NULL)
printf("Element does not exist\n");
else
printf("\nThe element %d found", tmp->item);
break;
case 4: printf("\nEnter Element to be deleted : ");
scanf("%d", &key);
root = Delete(root, key);
break;
default: exit(0);
}
}
}
Output
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
Tree Is Not Created
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
Enter The Element : 24
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :3
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :3
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :4
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :3
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :4
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :4
Enter Element to be deleted : 5
The deleted item is : 5
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
The Inorder display : 2 6 7 8 9 14 15 24
The Preorder display : 6 2 9 8 7 15 14 24
The Postorder display : 2 7 8 14 24 15 9 6
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :6
11. 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 digraph
using DFS/BFS method
#include<stdio.h>
void bfs(int n,int a[10][10],int src)
{
int i,s[10],q[10],front,rear,u,v;
for(i=0;i<n;i++)
{
s[i]=0;
}
s[src]=1;
front=0;
rear=-1;
q[++rear]=src;
while(front<=rear)
{
u=q[front++];
for(v=0;v<n;v++)
{
if(a[u][v]==1&&s[v]==0)
{
s[v]=1;
q[++rear]=v;
}
}
}
for(i=0;i<n;i++)
{
if(s[i]==0)
{
printf("%d is not reachable \n",i);
}
else
{
printf("%d is reachable\n",i);
}
}
}
void dfs(int n,int a[10][10],int u,int s[10])
{
int v;
s[u]=1;
for(v=0;v<n;v++)
{
if(a[u][v]==1 && s[v]==0)
{
dfs(n,a,v,s);
}
}
}
void main()
{
int n,i,j,src,a[10][10], flag, s[10], source, connected;
printf("Enter the no. of nodes in graph: ");
scanf("%d",&n);
bfs(n,a,src);
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
s[i]=0;
dfs(n,a,j,s);
flag=0;
for(i=0;i<n;i++)
{
if(s[i]==0)
flag=1;
}
if(flag==0)
connected=1;
}
if(connected==1)
printf("graph connected\n");
else
printf("graph not connected\n");
}
Output 1:
Enter the no. of nodes in graph: 5
Enter source :
1
0 is reachable
1 is reachable
2 is reachable
3 is reachable
4 is reachable
graph connected
Output 2:
Enter the no. of nodes in graph: 3
Enter source :
1
0 is reachable
1 is reachable
2 is not reachable
graph not connected
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. Design and 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>
#define MAX_ADDR 5
struct employee
{
int emp_id, emp_age;
char emp_name[25];
}emp[MAX_ADDR];
void main()
{
int i, ch, count = 0, index, haddr, id, flag = 0;
for(;;)
{
printf (" Hash Function \n");
printf ("==============\n");
printf("1: Insert Record \n2: Search Record\n3: Exit\n");
printf ("Enter option : ");
scanf("%d", &ch);
switch(ch)
{
case 1: if(count == MAX_ADDR)
{
printf("No free address space\n");
break;
}
printf("Enter employee id : ");
scanf("%d", &id);
haddr = hash(id);
printf("HAome address is %d\n", haddr);
for(i = 0; i<MAX_ADDR; i++)
{
index = (haddr + i) % MAX_ADDR;
if(emp[index].emp_id == 0)
{
emp[index].emp_id = id;
printf("Enter the employee name: ");
scanf("%s", emp[index].emp_name);
printf("Enter the employee age: ");
scanf("%d", &emp[index].emp_age);
count++;
printf ("Successfully inserted at Actual Address
%d:\n\n", index);
break;
}
}
break;
case 2: printf("Enter employee id to be searched: ");
scanf("%d", &id);
haddr = hash(id);
for(i=0; i<MAX_ADDR; i++)
{
index = (haddr+i)%MAX_ADDR;
if(emp[index].emp_id == 0)
{
flag = 1;
break;
}
else if(emp[index].emp_id == id)
{
printf("Employee id is: %d\n", emp[index].emp_id);
printf("Employee name is: %s\n",
emp[index].emp_name);
printf("Employee age is: %d\n",
emp[index].emp_age);
printf("Search Length is: %d\n", ++i);
break;
}
}
if(flag == 1 || i == MAX_ADDR)
{
printf("Key not present\n");
}
break;
default: exit(0);
}
}
}
Output
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 1
Enter employee id : 100
Home address is 0
Enter the employee name: hema
Enter the employee age: 45
Successfully inserted at Actual Address 0:
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 1
Enter employee id : 200
Home address is 0
Enter the employee name: raj
Enter the employee age: 47
Successfully inserted at Actual Address 1:
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 1
Enter employee id : 300
Home address is 0
Enter the employee name: chiru
Enter the employee age: 10
Successfully inserted at Actual Address 2:
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 2
Enter employee id to be searched: 200
Employee id is: 200
Employee name is: raj
Employee age is: 47
Search Length is: 2
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 2
Enter employee id to be searched: 400
Key not present
Hash Function
==============
1: Insert Record
2: Search Record
3: Exit
Enter option : 3