100% found this document useful (1 vote)
280 views63 pages

DS Lab Manual-1

The document provides details of the Data Structures laboratory course for semester 3 including 12 experiments on various data structures and algorithms. The experiments cover topics like arrays, strings, stacks, queues, linked lists, trees, graphs and hashing. The objectives of the course are to enable students to design, develop, analyze and evaluate various linear and non-linear data structures and their applications including sorting and searching algorithms.

Uploaded by

Amogh Sk
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
100% found this document useful (1 vote)
280 views63 pages

DS Lab Manual-1

The document provides details of the Data Structures laboratory course for semester 3 including 12 experiments on various data structures and algorithms. The experiments cover topics like arrays, strings, stacks, queues, linked lists, trees, graphs and hashing. The objectives of the course are to enable students to design, develop, analyze and evaluate various linear and non-linear data structures and their applications including sorting and searching algorithms.

Uploaded by

Amogh Sk
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/ 63

LAB MANUAL

Staff: Mrs. HemaMalini B H


Associate Professor,
Dept of CSE
BMSIT & Management

DATA STRUCTURES LABORATORY


[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2015 -2016)
SEMESTER - III
Laboratory Code 15CSL38 IA Marks 20
Number of Lecture Hours/Week 01I + 02P Exam Marks 80
Total Number of Lecture Hours 40 Exam Hours 03
CREDITS 02

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

Descriptions (if any)


Implement all the experiments in C Language under Linux / Windows environment.

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

Graduate Attributes (as per NBA)


1. Engineering Knowledge
2. Problem Analysis
3. Design/Development of Solutions
4. Modern Tool Usage

Conduction of Practical Examination:


1. All laboratory experiments (TWELVE nos) are to be included for practical examination.
2. Students are allowed to pick one experiment from the lot.
3. Strictly follow the instructions as printed on the cover page of answer script
4. Marks distribution: Procedure + Conduction + Viva:20 + 50 +10 (80)
5. Change of experiment is allowed only once and marks allotted to the procedure part
to be made zero.
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.

#include <stdio.h>
#include<stdlib.h>
#define SIZE 10

int a[SIZE], n;

int Create ()
{
int i;

printf ("Enter number of elements : ");


scanf ("%d", &n);

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


{
printf ("Enter element #%d :", i+1);
scanf ("%d", &a[i]);
}
return n;
}

void Display ()
{ int i;

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


{
printf ("\nElement #%d : ", i+1);
printf ("%d", a[i]);
}
}

void Insert ()
{
int ele, pos, i;

printf ("Enter the position to insert :");


scanf ("%d", &pos);
printf ("\nEnter the new element :");
scanf ("%d", &ele);

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


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

a[pos-1] = ele;

printf ("Resultant array is...\n");


for (i = 0; i < n+1; i++)
printf ("%d\t", a[i]);
}

void Delete ()
{
int pos, i;

printf ("Enter the location of the element to be deleted :");


scanf ("%d", &pos);

if (pos >= n+1)


printf ("The element cannot be deleted \n");
else
{
for (i = pos-1; i < n-1; i++)
a[i] = a[i+1];
}
printf ("Resultant array is ...\n");
for (i = 0; i < n-1; i++)
printf ("%d\t", a[i]);
}

int main ()
{
int choice, n, a[SIZE];
char ans = 'y';

printf ("\nArray Operations :\n");


printf ("====================\n");

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

Enter the new element :22


Resultant array is...
1 2 22 3 4 5
1. Create
2. Display
3. Insertion
4. Deletion
5. Exit
Enter your choice :4
Enter the location of the element to be deleted :3
Resultant array is ...
1 2 3 4
1. Create
2. Display
3. Insertion
4. Deletion
5. Exit
Enter your choice :5
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.

#include <stdio.h>
#include<stdlib.h>
#define SIZE 50

void Create (char str[SIZE], char pat[SIZE], char rep[SIZE])


{
int i;
char ch;

printf ("Enter the main string :");


scanf ("%s", str);

printf ("Enter the pattern string : ");


scanf ("%s", pat);

printf ("Enter the replacement string : ");


scanf ("%s", rep);

printf ("\nThe main string is : %s\n", str);


printf ("The pattern string is : %s\n", pat);
printf ("The replacement string is : %s\n", rep);
}

void FindReplace (char str[SIZE], char pat[SIZE], char rep[SIZE])


{
int i = 0, count = 0, count1 = 0, slen = 0, len = 0, j;

for (i = 0; pat[i] != '\0'; i++);


len = i;

for (i = 0; str[i] != '\0'; i++);


slen = i;

for (i = 0; i < slen;)


{
j = 0;
count = 0;
while ((str[i] == pat[j]) && (str[i] != '\0' ))
{
count ++;
i++;
j++;
}
if (count == len)
{
count1++; // Counter for number of occurances
count = 0; // Copy and replace the pattern by new string

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

printf ("\nPattern Matching\n");


printf("==================\n\n");
while (1)
{
printf ("1. Read Strings\n2. Find and Replace Pattern\n3. Exit");
printf ("\nEnter your choice : ");
scanf ("%d", &choice);

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

The main string is : managenageagement


The pattern string is : age
The replacement string is : YYY
1. Read Strings
2. Find and Replace Pattern
3. Exit
Enter your choice : 2
The main string after replacement is manYYYnYYYYYYment

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 s1[MAX1], s2[MAX2], top1 = -1, top2 = -1;

void push1(int item)


{
if(top1==MAX1-1)
{
printf("\n Stack Overflow \n");
return;
}
else
{
top1++;
s1[top1]=item;
}
return;
}

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

void push2(int item)


{
if(top2==MAX2-1)
{
printf("\n Stack Overflow \n");
return;
}
else
{
top2++;
s2[top2]=item;
}
return;
}

int pop2()
{
int item;

if(top2==-1)
{
printf("\n Stack Underflow \n");
return;
}
else
{
item=s2[top2];
top2--;
}
return item;
}

void check_pal (int num)


{
int n, n1, rem, dig, item;
n = n1 = num;

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

printf("Enter choice : ");


scanf("%d",&ch);

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

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 12321
student@student-virtual-machine:~/Hema$ ./a.out

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 3

Enter the element to be checked : 1234321

Number is palindrome

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 3

Enter the element to be checked : 123


Number is not palindrome

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 2

Stack Underflow

Empty Stack

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 10

The elements of the stack are :


10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 20

The elements of the stack are :


20
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 2
The deleted item is : 20
The elements of the stack are :
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 30

The elements of the stack are :


30
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 40

The elements of the stack are :


40
30
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 50

Stack Overflow

The elements of the stack are :


40
30
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 3

Enter the element to be checked : 121

Number is palindrome

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 3

Enter the element to be checked : 453


Number is not palindrome

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 1

Enter the element to be inserted : 2

Stack Overflow

The elements of the stack are :


40
30
10
1. Push 2. Pop 3. Check Palindrome 4. Exit
Enter choice : 2
The deleted item is : 40
The elements of the stack are :
30
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 2
The deleted item is : 30
The elements of the stack are :
10

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 2
The deleted item is : 10
Empty Stack

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 2

Stack Underflow

Empty Stack

1. Push 2. Pop 3. Check Palindrome 4. Exit


Enter choice : 4
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.

#include<stdio.h>
#include<string.h>

int in_prec(char);
int stack_prec(char);

int in_prec (char sym)


{
switch (sym)
{
case '+':
case'-':return 1;
case '*':
case '/': return 3;
case '$':
case '^': return 6;
case '(': return 9;
case ')': return 0;
default: return 7;
}
}

int stack_prec (char sym)


{
Switch (sym)
{
case '+':
case'-': return 2;
case '*':
case '/': return 4;
case '$':
case '^': return 5;
case '(': return 0;
case '#': return-1;

default : return 8;
}
}

void convert (char *in, char *post)


{
char s[30], sym;
int top = -1, i, j=0;

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

printf ("Enter a valid infix expression\n");


scanf ("%s", in);

convert (in, post);


printf ("Postfix expression is %s", post);
}

Output
Enter a valid infix expression
a*(b+c)
Postfix expression is abc+*

Enter a valid infix expression


a*b+c
Postfix expression is ab*c+

Enter a valid infix expression


a*(b+c)-d/e
Postfix expression is abc+*de/-
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:
+, -, *, /, %, ^

#include <stdio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>

double compute (char symbol, double op1, double op2)


{
double res;

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;

printf ("Enter postfix expression :");


scanf ("%s", postfix);
printf ("The expression is :%s\n", postfix);

for (i = 0; i < strlen (postfix); i++)


{
symbol = postfix [i];
if (isdigit (symbol))
s[++top] = symbol-'0';
else
{
op2 = s[top--];
op1 = s[top--];
res = compute (symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf ("The result is %.2f\n", res);
}

Output
Enter postfix expression :23+5*
The expression is :23+5*
The result is 25.00

Enter postfix expression :27*5-6+


The expression is :27*5-6+
The result is 15.00
OR

#include<stdio.h>
#include<ctype.h>

float stack[20], value[20];


int top = -1;
char postfix[20];

void push(float num)


{
top=top+1;
stack[top]=num;
return;
}

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;

printf("\nEnter postfix expression: ");


scanf("%s",postfix);
i=0;
while(postfix[i]!='\0')
{
if(isalpha(postfix[i]))
{
printf("Enter value of %c : ",postfix[i]);
scanf("%f",&value[i]);
}
i++;
}
result=eval();
printf("\nThe value of the postfix expression is %7.2f\n",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

Enter postfix expression: ab/c+


Enter value of a : 4
Enter value of b : 2
Enter value of c : 1
The value of the postfix expression is 3.00
b. Solving Tower of Hanoi problem with n disks
#include <stdio.h>
void tower (int n, char src, char temp, char dst)
{
if (n == 0) return;
tower (n-1, src, dst, temp);
printf ("Move disc %d from %c to %c\n", n, src, dst);
tower (n-1, temp, src, dst);
}
void main ()
{
int n;

printf ("Enter the number of discs : ");


scanf ("%d", &n);

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


}
Output
Enter the number of discs : 3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C

Enter the number of discs : 4


Move disc 1 from A to B
Move disc 2 from A to C
Move disc 1 from B to C
Move disc 3 from A to B
Move disc 1 from C to A
Move disc 2 from C to B
Move disc 1 from A to B
Move disc 4 from A to C
Move disc 1 from B to C
Move disc 2 from B to A
Move disc 1 from C to A
Move disc 3 from B to C
Move disc 1 from A to B
Move disc 2 from A to C
Move disc 1 from B to C
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.

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

printf("\nQueue Operations :");


printf("\n================ \n");
while (1)
{
printf ("\n1: Insert 2: Delete 3: Exit \n");
printf ("Enter option :");
scanf ("%d", &option);

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

1: Insert 2: Delete 3: Exit


Enter option :1

Enter item to be inserted :1


The contents of the Queue is : 1
1: Insert 2: Delete 3: Exit
Enter option :1

Enter item to be inserted :2


The contents of the Queue is : 1 2
1: Insert 2: Delete 3: Exit
Enter option :1

Enter item to be inserted :3


The contents of the Queue is : 1 2 3
1: Insert 2: Delete 3: Exit
Enter option :2
Item deleted is 1

The contents of the Queue is : 2 3


1: Insert 2: Delete 3: Exit
Enter option :2
Item deleted is 2
The contents of the Queue is : 3

1: Insert 2: Delete 3: Exit


Enter option :2
Item deleted is 3

1: Insert 2: Delete 3: Exit


Enter option :2
Empty Queue

1: Insert 2: Delete 3: Exit


Enter option :1

Enter item to be inserted :4


The contents of the Queue is : 4
1: Insert 2: Delete 3: Exit
Enter option :1

Enter item to be inserted :5


The contents of the Queue is : 4 5
1: Insert 2: Delete 3: Exit
Enter option :2
Item deleted is 4
The contents of the Queue is : 5

1: Insert 2: Delete 3: Exit


Enter option :2
Item deleted is 5

1: Insert 2: Delete 3: Exit


Enter option :2
Empty Queue

1: Insert 2: Delete 3: Exit


Enter option :1

Enter item to be inserted :4


The contents of the Queue is : 4
1: Insert 2: Delete 3: Exit
Enter option :2
Item deleted is 4

1: Insert 2: Delete 3: Exit


Enter option :3
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

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

#define MALLOC(p, n, type) \


p = (type*) malloc (n*sizeof(type)); \
if (p == NULL) \
{\
printf("insufficient Memory\n");\
exit(0); \
}

void FrontInsert ()
{
NODE z;

MALLOC(z, 1, struct node);

printf ("\nEnter USN of student : ");


scanf ("%s", z->usn);

printf ("Enter Name of student : ");


scanf ("%s", z->name);

printf ("Enter Branch of student : ");


scanf ("%s", z->branch);

printf ("Enter sem of student : ");


scanf ("%d", &z->sem);

printf ("Enter phone of student : ");


scanf ("%s", z->phone);
printf ("\nStudent Details: %10s %20s %5s %d %s \n", z->usn, z->name, z-
>branch, z->sem, z->phone);

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;

MALLOC(z, 1, struct node);

printf ("\nEnter USN of student : ");


scanf ("%s", z->usn);

printf ("Enter Name of student : ");


scanf ("%s", z->name);

printf ("Enter Branch of student : ");


scanf ("%s", z->branch);

printf ("Enter sem of student : ");


scanf ("%d", &z->sem);
printf ("Enter phone of student : ");
scanf ("%s", z->phone);

printf ("\nStudent Details: %10s %20s %5s %d %s \n", z->usn, z->name, z-


>branch, z->sem, z->phone);

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;

while (cur->link != NULL)


{
prev = cur;
cur = cur->link;
}
prev->link = NULL;
printf("Node Deleted");
printf ("\n%s %s %s %d %s", cur->usn, cur->name, cur->branch, cur->sem, cur-
>phone);
free (cur);
return;
}

void Display ()
{
NODE temp;

if (start == NULL)
{
printf ("List is empty\n");
return;
}

printf ("\nThe list is : \n");


printf (" USN Name Branch Sem Phone\n");

temp = start;
while (temp != NULL)
{

printf ("\n%s %s %s %d %s", temp->usn, temp->name, temp->branch, temp-


>sem, temp->phone);
temp = temp->link;
}
return;
}

void main ()
{
int option, item, i, n;

printf ("Singly Linked List Operations\n");


printf ("-----------------------------\n");
while (1)
{
printf ("\n\n1: Create List\n");
printf ("2: Front Insert\n3: Front Delete\n");
printf ("4: End Insert\n5: End Delete\n");

printf ("\nEnter Option :");


scanf ("%d", &option);

switch (option)
{
case 1:
printf("\nEnter number of nodes : ");
scanf ("%d", &n);

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


FrontInsert ();
Display();
break;
case 2:
FrontInsert ();
Display ();
break;
case 3:
FrontDelete ();
if (start)
Display ();
break;
case 4:
EndInsert ();
Display ();
break;
case 5:
EndDelete ();
if (start)
Display ();
break;
default:
exit(0);
}
}
}

Output
Singly Linked List Operations
-----------------------------

1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete

Enter Option :1

Enter number of nodes : 3

Enter USN of student : 100


Enter Name of student : hema
Enter Branch of student : cse
Enter sem of student : 7
Enter phone of student : 9467841912

Student Details: 100 hema cse 7 9467841912

Enter USN of student : 200


Enter Name of student : raj
Enter Branch of student : civ
Enter sem of student : 8
Enter phone of student : 9637468237

Student Details: 200 raj civ 8 9637468237


Enter USN of student : 300
Enter Name of student : chiru
Enter Branch of student : arch
Enter sem of student : 7
Enter phone of student : 7478974981

Student Details: 300 chiru arch 7 7478974981

The list is :
USN Name Branch Sem Phone

300 chiru arch 7 7478974981


200 raj civ 8 9637468237
100 hema cse 7 9467841912

1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete

Enter Option :2

Enter USN of student : 400


Enter Name of student : chethan
Enter Branch of student : me
Enter sem of student : 8
Enter phone of student : 8572385932

Student Details: 400 chethan me 8 8572385932

The list is :
USN Name Branch Sem Phone

400 chethan me 8 8572385932


300 chiru arch 7 7478974981
200 raj civ 8 9637468237
100 hema cse 7 9467841912

1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete

Enter Option :4

Enter USN of student : 500


Enter Name of student : arjun
Enter Branch of student : dip
Enter sem of student : 3
Enter phone of student : 349235439
Student Details: 500 arjun dip 3 349235439

The list is :
USN Name Branch Sem Phone

400 chethan me 8 8572385932


300 chiru arch 7 7478974981
200 raj civ 8 9637468237
100 hema cse 7 9467841912
500 arjun dip 3 349235439

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

300 chiru arch 7 7478974981


200 raj civ 8 9637468237
100 hema cse 7 9467841912
500 arjun dip 3 349235439

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

300 chiru arch 7 7478974981


200 raj civ 8 9637468237
100 hema cse 7 9467841912

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

200 raj civ 8 9637468237


100 hema cse 7 9467841912

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

200 raj civ 8 9637468237

1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete

Enter Option :4

Enter USN of student : 900


Enter Name of student : yyy
Enter Branch of student : eee
Enter sem of student : 6
Enter phone of student : 784239753

Student Details: 900 yyy eee 6 784239753

The list is :
USN Name Branch Sem Phone

200 raj civ 8 9637468237


900 yyy eee 6 784239753

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

900 yyy eee 6 784239753


1: Create List
2: Front Insert
3: Front Delete
4: End Insert
5: End Delete

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 =(struct node *) malloc (sizeof(struct node));


temp->prev = NULL;

temp->next = NULL;

printf("\n Enter ssn,name,department, designation, salary and phno of employee : \n");


scanf("%d %s %s %s %f %d", &ssn, name,dept,desg,&sal, &phno);

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;

printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,


temp1->desg,temp1->sal, temp1->phno );

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;

temp = temp1 = NULL;


printf("-----------------MENU--------------------\n");
printf("\n 1 - Create a DLL of n Employees : ");
printf("\n 2 - Display ....");
printf("\n 3 - End Insert");
printf("\n 4 - End delete");
printf("\n 5 - Front Insert");
printf("\n 6 - Front Delete");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
DEndInsert ();
break;
case 2:
Display ();
break;
case 3:
DEndInsert ();
break;
case 4:
DEndDelete ();
break;
case 5:
DFrontInsert ();
break;
case 6:
DFrontDelete ();
break;
case 7:
exit(0);

default:
printf("Wrong Choice\n");
}
}
}

Output:
-----------------MENU--------------------

1 - Create a DLL of n Employees :


2 - Display ....
3 - End Insert
4 - End delete
5 - Front Insert
6 - Front Delete
7 - exit
------------------------------------------

Enter choice : 1
Enter no of employees : 3

Enter ssn,name,department, designation, salary and phno of employee :


100
Hema
CSE
AssocProf
130000
998479823

Enter ssn,name,department, designation, salary and phno of employee :


200
Raj
Civil
HoD
100000
974894983

Enter ssn,name,department, designation, salary and phno of employee :


300
Chiru
Arch
Developer
50000
953897592

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 ssn,name,department, designation, salary and phno of employee :


500
Arjun
IT
Engineer
55000
957875424

Enter choice : 3

Enter ssn,name,department, designation, salary and phno of employee :


600
Chethan
Mech
DesignEngineer
54000
957897458

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

NODE insert_rear(float cf,float x , float y, float z,NODE head)


{
NODE temp,cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
temp->flag=0;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}

void display(NODE head)


{
NODE temp;
if(head->link==head)
{
printf("polynominal doesn't exsits\n");
return;
}
temp=head->link;
while(temp!=head)
{
printf("+ % 5.2fx^%dy^%dz^%d",temp->cf,temp->px,temp->py,temp->pz);
temp=temp->link;
}
printf("\n");
}
NODE add_poly(NODE h1,NODE h2, NODE h3)
{
NODE p1,p2;
int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;
p1=h1->link;
while(p1!=h1)
{
x1=p1->px;
y1=p1->py;
z1=p1->pz;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
z2=p2->pz;
cf2=p2->cf;
if(x1==x2 && y1==y2 && z1==z2) break;
p2=p2->link;
}
if(p2!=h2)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert_rear(cf,x1,y1,z1,h3);
}
else
h3=insert_rear(cf1,x1,y1,z1,h3);
p1=p1->link;
}

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

NODE read_poly(NODE head)


{
int i;
int px,py,pz;
float cf;
printf("enter the coeffecient as -999 to end the polynominal\n");
for(i=1;;i++)
{
printf("enter the %d term\n",i);
printf("coeff=");
scanf("%f",&cf);
if(cf==-999) break;
printf("pow x=");
scanf("%d",&px);
printf("pow y=");
scanf("%d",&py);
printf("pow z=");
scanf("%d",&pz);

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;

/* This function is for creating a binary search tree */


NODE insert(NODE root)
{
NODE temp, cur, prev;
int item;

printf("\nEnter The Element : ");


scanf("%d", &item);

temp = (NODE) malloc(sizeof(struct BST));


temp->llink = NULL;
temp->rlink = NULL;
temp->item = item;

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

/* This function displays the tree in inorder fashion */


void inorder(NODE root)
{
if (root != NULL)
{
inorder(root->llink);
printf("\t%d", root->item);
inorder(root->rlink);
}
}

/* This function displays the tree in preorder fashion */


void preorder(NODE root)
{
if (root != NULL)
{
printf("%d\t", root->item);
preorder(root->llink);
preorder(root->rlink);
}
}

/* This function displays the tree in postorder fashion */


void postorder (NODE root)
{
if (root != NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t", root->item);
}
}

NODE search (NODE root, int key)


{
NODE cur;
if(root == NULL)
return NULL;
cur = root;
while(cur != NULL)
{
if (key == cur->item)
return cur;
if (key < cur->item)
cur = cur->llink;
else
cur = cur->rlink;
}
return NULL;
}

NODE Delete (NODE root, int item)


{
NODE cur, parent= NULL, suc,q;
if(root == NULL)
{
printf("Tree is empty,Element does not exist\n");
return root;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->item) break;
parent=cur;
cur=(item<cur->item)?cur->llink:cur->rlink;
}
if(cur==NULL)
{
printf("Item not found\n");
return root;
}
if(cur->llink ==NULL)
q=cur->rlink;
else if(cur->rlink==NULL)
q=cur->llink;
else
{
suc = cur->rlink;
while(suc->llink != NULL)
suc = suc -> llink;
suc->llink = cur->llink;
q = cur->rlink;
}
if(parent == NULL)
return q;
if(cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
printf ("The deleted item is : %d", cur->item);
free(cur);

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

Enter The Element : 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 :1

Enter The Element : 9

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 : 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 :1

Enter The Element : 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 :1

Enter The Element : 8

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 : 15

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

Enter The Element : 14


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 : 7

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 : 8

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 : 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 :1

Enter The Element : 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

The Inorder display : 2 2 5 5 6 7 8 89 14 15


24
The Preorder display : 6 5 2 2 5 9 8 78 15 14
24
The Postorder display : 2 2 5 5 7 8 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 :3

Enter Element to be searched :10


Element does not exist

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

Enter Element to be searched :8

The element 8 found

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 : 2


The deleted item is : 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

Enter Element to be searched :24

The element 24 found

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 5 5 6 7 8 8 914 15 24


The Preorder display : 6 5 2 5 9 8 7 815 14 24

The Postorder display : 2 5 5 7 8 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 :4

Enter Element to be deleted : 8


The deleted item is : 8

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 5 5 6 7 8 9 14 15 24


The Preorder display : 6 5 2 5 9 8 7 15 14 24
The Postorder display : 2 5 5 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 :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 5 6 7 8 9 14 15 24


The Preorder display : 6 5 2 9 8 7 15 14 24
The Postorder display : 2 5 7 8 14 24 15 96

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

printf("\nEnter the adj. matrix\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter source :\n ");
scanf("%d",&src);

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 the adj. matrix


01010
10101
01010
10101
01010

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 the adj. matrix


010
100
000

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

int hash (int key)


{
return key % MAX_ADDR;
}

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

You might also like