Data Structures Lab Manual
Data Structures Lab Manual
Prepared By
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
6 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
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.
7 b. Display the status of SLL and count the number of nodes in it c. Perform Insertion and Deletion at
End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK and QUEUE
f. Exit
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.
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
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
9
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
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,
10 Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Delete an element(ELEM) from BST
a. e. Exit
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.
11 b. Print all the nodes reachable from a given starting node in a digraph using BFS method b. c.
Check whether a given graph is connected or not using DFS method.
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
12
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.
EXPERIMENT - 01
Design, Develop and Implement a menu driven program in C for the following Array operations
a) Creating 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.
PROGRAM CODE
#include<stdio.h>
#include<conio.h>
/* Global variables declaration */
int a[4], n, elem, i, pos;
}
a[pos] = elem;
n = n+1;
} //end of insert()
void main()
{
int ch; clrscr(); do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Exit\n");
printf("-------------------------------"); printf("\nEnter your choice: "); scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: del();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}while(ch!=5);
getch();
}// end of main
SAMPLE OUTPUT:
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 1
Enter the size of the array elements: 5
Enter the elements for the array:
10 20 30 40 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 2
--------Menu-----------
Department of Computer Science & Engineering A.I.E.T, Moodbiri
7 DATA STRUCTURES WITH C LABORATORY (15CSL38)
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 4
Enter the position of the element to be deleted: 5
The deleted element is = 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 2
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 5
EXPERIMENT - 02
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
withREP if PAT exists in STR. Repost suitable messages in case PAT does not exist in STR.
Support the program with functions for each of the above operations. Dont use built-in
functions.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
//Declarations
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c] ! = '\0')
{
if(str[m] = = pat[i]) // ...... matching
{
i++; m++;
if(pat[i] = = '\0') //.....found occurrences.
{
flag = 1;
//.... copy replace string in ans string.
for(k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
} // if ends.
else //... mismatch
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}//else ends
} //end of while ans[j] = '\0';
} //end stringmatch()
Department of Computer Science & Engineering A.I.E.T, Moodbiri
9 DATA STRUCTURES WITH C LABORATORY (15CSL38)
void main()
{
clrscr();
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");
flushall();
gets(pat);
printf("\nEnter a replace string \n");
flushall();
gets(rep);
stringmatch();
if(flag = = 1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\nPattern string NOT found\n");
getch();
} // end of main
EXPERIMENT - 03
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.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 4
int stack[MAX], item;
int ch, top = -1, count = 0, status = 0;
/*PUSH FUNCTION*/
void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop(int stack[])
{
int ret;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
ret = stack[top--];
status--;
printf("\nPopped element is %d", ret);
}
return ret;
Department of Computer Science & Engineering A.I.E.T, Moodbiri
11 DATA STRUCTURES WITH C LABORATORY (15CSL38)
else{
for(i=top; i>=0; i--)
printf("\n ------\n| %d |", stack[i]);
printf("\n");
}
}
/*MAIN PROGRAM*/
void main()
{
clrscr();
do{
printf("\n\n----MAIN MENU----\n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch){
Department of Computer Science & Engineering A.I.E.T, Moodbiri
12 DATA STRUCTURES WITH C LABORATORY (15CSL38)
SAMPLE OUTPUT:
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 2
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter a element to be pushed: 1
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 3
Stack contents are not Palindrome
EXPERIMENT - 04
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.
PROGRAM CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char infix[20], postfix[20];
clrscr();
printf("\nEnter a valid infix expression\n");
flushall();
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix);
getch();
}
SAMPLE OUTPUT:
Enter a valid infix expression
(a+(b-c)*d)
The infix expression is: (a+(b-c)*d)
The postfix expression is: abc-d*+
Department of Computer Science & Engineering A.I.E.T, Moodbiri
17 DATA STRUCTURES WITH C LABORATORY (15CSL38)
EXPERIMENT - 05
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.
void main()
{
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
clrscr();
printf("\nEnter the postfix expression:\n");
flushall();
gets(postfix);
top=-1;
for(i=0; <strlen(postfix); i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol - '0';
else
{
op2 = s[top--];
op1 = s[top--];
Department of Computer Science & Engineering A.I.E.T, Moodbiri
18 DATA STRUCTURES WITH C LABORATORY (15CSL38)
SAMPLE OUTPUT:
RUN1:
Enter the postfix expression:
23+
The result is: 5.000000
RUN2:
Enter the postfix expression:
23+7*
The result is: 35.000000
PROGRAM 5B:
#include<stdio.h>
#include<conio.h>
void tower(int n, int source, int temp,int destination)
{
if(n == 0)
return;
tower(n-1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n-1, temp, source, destination);
}
void main()
{
int n;
clrscr();
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d",
(int)pow(2,n)-1);
getch();
}
SAMPLE OUTPUT:
EXPERIMENT - 06
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
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 4
int ch, front = 0, rear = -1, count=0;
char q[MAX], item;
void insert()
{
if(count == MAX)
printf("\nQueue is Full");
else {
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf("\nQueue is Empty");
else
{
if(front > rear && rear==MAX-1)
{
front=0;
rear=-1;
count=0;
}
else
{
item=q[front];
Department of Computer Science & Engineering A.I.E.T, Moodbiri
21 DATA STRUCTURES WITH C LABORATORY (15CSL38)
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n");
for(i=f; i<=r; i++)
{
printf("%c\t", q[i]);
f = (f + 1) % MAX;
}
}
}
void main()
{
clrscr();
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
flushall();
switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: ");
scanf("%c", &item);
insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
}
Department of Computer Science & Engineering A.I.E.T, Moodbiri
22 DATA STRUCTURES WITH C LABORATORY (15CSL38)
}while(ch!=4);
getch();
}
SAMPLE OUTPUT:
1. Insert 2. Delete 3. Display 4. Exit
Enter the choice: 1
Enter the character / item to be inserted: A
Queue is Full
1. Insert 2. Delete
Enter the choice: 3
EXPERIMENT - 07
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 and Deletion at End of SLL
d) Perform Insertion and Deletion at Front of SLL
e) Demonstrate how this SLL can be used as STACK and QUEUE
f) Exit
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int MAX=4, count;
struct student
{
char usn[10], name[30], branch[5], phno[10];
int sem;
struct student *next; //Self referential pointer.
};
gets(newnode->usn);
flushall();
gets(newnode->name);
flushall();
gets(newnode->branch);
scanf("%d",&(newnode->sem));
flushall();
gets(newnode->phno);
newnode->next=NULL; //Set next to NULL... head=newnode;
return head;
}
head=newnode;
}
return head;
}
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
}
return head;
}
{
printf("\n1.Insert at Front(First)\t2.Insert at End(Rear/Last)\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_front(head);
break;
case 2: head=insert_rear(head);
break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
void main()
{
int ch, i, n; NODE *head; head=NULL; clrscr();
printf("\n*----------Studednt Database-----------*");
do
{
printf("\n 1.Create\t 2.Display\t 3.Insert\t 4.Delete\t 5.Stack\t 6.Queue\t
7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
head=create(head);//Call to Create NODE...
break;
case 2: head=display(head); //Call to Display...
break;
case 3: head=insert(head); //Call to Insert...
break;
case 4: head=del(head); //Call to delete
break;
Department of Computer Science & Engineering A.I.E.T, Moodbiri
30 DATA STRUCTURES WITH C LABORATORY (15CSL38)
case 5: head=stack(head);
break;
case 6: head=queue(head);
break;
case 7: exit(); //Exit...
}
}while(ch!=7);
}
SAMPLE OUTPUT:
----STUDENT DATA----
----STUDENT DATA----
----STUDENT DATA----
----STUDENT DATA----
USN
1kt12is002 NAME
ravi BRANCH
is SEM
3 Ph.NO.
9900099111
1kt12cs001 kumar cs 3 9900099000
1kt12is004 naresh is 3 99000993333
----STUDENT DATA----
----STUDENT DATA----
EXPERIMENT - 08
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
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int MAX=4, count;
struct emp
{
int ssn;
char name[20]; char dept[10]; char desig[15];
int sal;
char phno[10];
struct emp *left;
struct emp *right;
};
p=head;
printf("\n----EMPLOYEE DATA----\n");
printf("\nSSN\tNAME\tDEPT\tDESINGATION\tSAL\t\tPh.NO.");
while(p!=NULL)
{
` p->sal, p->phno);
}
printf("\n%d\t%s\t%s\t%s\t\t%d\t\t%s", p->ssn, p->name, p->dept, p->desig);
p = p->right; //Go to next node...
printf("\nThe no. of nodes in list is: %d",countnodes(head));
}
return head;
}
else
{
newnode=getnode(head);
while(p->right!=NULL)
{
p=p->right;
}
p->right=newnode;
newnode->left=p;
}
return head;
}
q=p->left;
q->right=NULL;
p->left=NULL;
free(p);//Delete last node...
printf("\nLast(end) entry is deleted");
return head;
}
{
case 1: head=insert_end(head);
break;
case 2: head=delete_front(head);
break;
case 3: break;
}
}while(ch1!=3);
break;
case 2: do{
printf("\n1.Insert at Front\t2.Delete from Rear\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch2);
switch(ch2)
{
case 1: head=insert_front(head);
break;
case 2: head=delete_end(head);
break;
case 3: break;
}
}while(ch2!=3);
break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
void main()
{
int ch, i, n; NODE *head; head=NULL; clrscr();
printf("\n----------Employee Database-----------");
do
{
printf("\n1.Create\t2.Display\t3.Insert\t4.Delete\t5.Queue\t6.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many employees data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
Department of Computer Science & Engineering A.I.E.T, Moodbiri
40 DATA STRUCTURES WITH C LABORATORY (15CSL38)
SAMPLE OUTPUT:
----------Employee Database-----------
1. Create 2. Display 3. Insert 4. Delete 5. Queue 7. Exit
Enter your choice: 1
----EMPLOYEE DATA----
SSN NAME DEPT DESINGATION SAL Ph.NO.
1 KUMAR CSE INSTRUCTOR 8000 900099000
2 RAVI ISE INSTRUCTOR 9000 900099111
----EMPLOYEE DATA----
EXPERIMENT - 09
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
PROGRAM CODE:
#include<stdio.h>
#include<alloc.h>
#include<math.h>
struct node
{
int cf, px, py, pz; int flag;
struct node *link;
};
typedef struct node NODE;
NODE* getnode()
{
NODE *x; x=(NODE*)malloc(sizeof(NODE));
if(x==NULL)
{
printf("Insufficient memory\n"); exit(0);
}
return x;
}
temp=temp->link;
}
printf("\n");
}
float result=0.0;
head=h1;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
while(h1->link != head)
{
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) * pow(z,h1->pz));
h1=h1->link;
}
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) * pow(z,h1->pz));
printf("\nPolynomial result is: %f", result);
}
void main()
{
NODE *h1,*h2,*h3;
int ch;
clrscr();
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
while(1)
{
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\n3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter polynomial to evaluate:\n");
h1=read_poly(h1);
display(h1);
evaluate(h1);
break;
case 2: printf("\nEnter the first polynomial:");
h1=read_poly(h1);
printf("\nEnter the second polynomial:"); h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
Department of Computer Science & Engineering A.I.E.T, Moodbiri
46 DATA STRUCTURES WITH C LABORATORY (15CSL38)
display(h3);
break;
case 3:exit(0);
break;
default:printf("\nInvalid entry");
break;
} getch();
}
}
SAMPLE OUTPUT:
Polynomial is:
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
Enter coeff: 4
Enter x, y, z powers (0-indiacate NO term: 2 2 2
Enter coeff: 6
Polynomial is:
1st Polynomial is:
4 x^2 y^2 z^2 + 3 x^1 y^1 z^2
EXPERIMENT - 10
Design, Develop and Implement a menu driven Program in C for the following operations on
a) Binary Search Tree (BST) of Integers
b) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
c) Traverse the BST in Inorder, Preorder and Post Order
d) Search the BST for a given element (KEY) and report the appropriate message
e) Delete an element (ELEM) from BST
f) Exit
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;
}
void main()
{
int data, ch, i, n; NODE *root=NULL; clrscr();
while (1)
{
printf("\n1.Insertion in Binary Search Tree");
printf("\n2.Search Element in Binary Search Tree");
printf("\n3.Delete Element in Binary Search Tree");
printf("\n4.Inorder\n5.Preorder\n6.Postorder\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like
(6,9,5,2,8,15,24,14,7,8,5,2)\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("\nEnter the element to delete: ");
scanf("%d", &data);
root=del(root, data);
break;
case 4: printf("\nInorder Traversal: \n");
inorder(root);
break;
case 5: printf("\nPreorder Traversal: \n");
Department of Computer Science & Engineering A.I.E.T, Moodbiri
52 DATA STRUCTURES WITH C LABORATORY (15CSL38)
preorder(root);
break;
case 6: printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 7: exit(0);
default:printf("\nWrong option");
break;
}
}
getch();
}
SAMPLE OUTPUT:
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree 3. Delete Element in Binary Search Tree 4. Inorder
5. Preorder
6.
Postorder
7. Exit
Enter your choice: 1
Enter N value: 12
Enter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)
6 9 5 2 8 15 24 14 7 8 5 2
Inorder Traversal:
2 5 6 7 8 9 14 15 24
Preorder Traversal:
6 5 2 9 8 7 15 14 24
Postorder Traversal:
2 5 7 8 14 24 15 9 6
EXPERIMENT - 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 BFS method
c) Check whether a given graph is connected or not using DFS method.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source); q[++rear] = source; visited[source] = 1;
printf("\nThe reachable vertices are: ");
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
}
Department of Computer Science & Engineering A.I.E.T, Moodbiri
55 DATA STRUCTURES WITH C LABORATORY (15CSL38)
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.Check graph connected or
not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3: printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
if(b[i]==0)
m=0;
}
Department of Computer Science & Engineering A.I.E.T, Moodbiri
56 DATA STRUCTURES WITH C LABORATORY (15CSL38)
if(m==1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}
SAMPLE OUTPUT:
Graph is Connected
EXPERIMENT - 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.
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct employee
{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[MAX];
int a[MAX];
void display()
{
int i, ch;
Department of Computer Science & Engineering A.I.E.T, Moodbiri
58 DATA STRUCTURES WITH C LABORATORY (15CSL38)
void main()
{
int num, key, I, ans = 1;
clrscr();
printf("\nCollision handling by linear probing: ");
for (i=0; i < MAX; i++)
a[i] = -1;
do
{
printf("\nEnter the data: ");
scanf("%d", &num);
key=create(num);
linear_prob(key,num);
printf("\nDo you wish to continue? (1/0): ");
scanf("%d",&ans);
}while(ans);
display(emp);
getch();
}
SAMPLE OUTPUT:
RUN1:
Enter the data: 2
Enter emp id: 100
Enter emp name: Anand
1.Display ALL
2.Filtered Display Enter the choice: 1
EmpName
Anand
Kumar
RUN2:
Enter the data: 2
Enter emp id: 100
Enter emp name: Anand