0% found this document useful (0 votes)
93 views

Aditya Institute of Technology and Management: Tekkali

The document provides information about the Data Structures & Algorithms Lab course at Aditya Institute of Technology and Management. It includes the course details, objectives, outcomes, scheme of course work, guidelines for students, and list of experiments. The course aims to develop skills in designing and analyzing linear and non-linear data structures. It enables students to identify suitable data structures for problems and gain practical knowledge of data structures. The lab experiments cover recursive solutions, searching and sorting algorithms, linked lists, stacks, queues, binary trees, and graph algorithms.

Uploaded by

chalapathit520
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Aditya Institute of Technology and Management: Tekkali

The document provides information about the Data Structures & Algorithms Lab course at Aditya Institute of Technology and Management. It includes the course details, objectives, outcomes, scheme of course work, guidelines for students, and list of experiments. The course aims to develop skills in designing and analyzing linear and non-linear data structures. It enables students to identify suitable data structures for problems and gain practical knowledge of data structures. The lab experiments cover recursive solutions, searching and sorting algorithms, linked lists, stacks, queues, binary trees, and graph algorithms.

Uploaded by

chalapathit520
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

ADITYA INSTITUTE OF TECHNOLOGY AND MANAGEMENT

(An Autonomous Institution)

TEKKALI

Department of Computer Science and Engineering

LAB MANUAL

Data Structures & Algorithms Lab


Prepared by:

Sri. R.Srinivas,
Assistant Professor, Dept. of. CSE, AITAM
INDEX

S.NO CONTENTS PAGE NO.

1 Scheme of Course Work 3

2 Introduction About Lab 6

3 Guidelines to the Students 7

4 List of Experiments 8

5 Solutions 10

7 References 63

2
Scheme of Course Work
Course Details:

Course Title : DATA STRUCTURES & ALGORITHMS LAB


Course Code : 20CSL101 L T P C 0 0 3 1.5
Program: I B.Tech
Specialization: Computer Science and Engineering
Semester: II
Prerequisites:
Basic of any one programming language
Courses to which it
is a Prerequisite? Design and Analysis of Algorithms

Course Objectivities:

The course is designed to develop skills to design and analyze simple linear and nonlinear
1 data structures. It strengthens the ability to the students to identify and apply the suitable data
structure for the given real-world problem. It enables them to gain knowledge in practical
applications of data structures.

Course Outcomes:

1 Develop Programs as recursive solutions for problems


2 Implements different strategies to solve the common searching and sorting algorithms.

3 Illustrate the use of dynamic memory allocation through linked list operations.

4 Design programs for linear data structures such as Stacks, and Queues.

5 Develop Programs for implementing various operations on Binary Trees and Binary Search Trees.

6 Apply the fundamental graph algorithms to solve problems using Depth-First and Breadth- First
Search.

Program Outcomes (POs):


At the end of the programme, the students in will be able to:

PO1 Apply the knowledge of mathematics, science, engineering fundamentals, and an


engineering specialization to the solution of complex engineering problems.
PO2 Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

3
PO3 Design solutions for complex engineering problems and design system components
or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
PO4 Use research-based knowledge and research methods including design of
experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
PO5 Create, select, and apply appropriate techniques, resources, and modern engineering
and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO6 Apply reasoning informed by the contextual knowledge to assess societal, health,
safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO7 Understand the impact of the professional engineering solutions in societal and
environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
PO8 Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9 Function effectively as an individual, and as a member or leader in diverse teams,
and in multidisciplinary settings.
PO10 Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
PO11 Demonstrate knowledge and understanding of the engineering and management
principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
PO12 Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

Program Specific Outcomes (PSOs):

By the completion of Information Technology program the student will be able to:
PSO1 Execute the mathematical foundations, algorithmic principles of engineering to
identify, formulate, design and investigate the intricate problems of Information
Technology.
PSO2 Comprehend the principles and applications of the software and hardware facets of
computer systems.
PSO3 Apply knowledge in various domains to identify research gaps and provide solutions.

4
Course Outcomes versus Program Outcomes versus Program Specific Outcomes:

CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3 3 3 2 3 3 2 3 2 1
CO2 3 3 3 2 3 3 2 3 2 1
CO3 3 3 3 2 3 3 3 3 2 1
CO4 3 3 3 2 3 3 3 3 2 1
CO5 3 3 3 2 3 3 3 3 2 1
CO6 3 3 3 2 3 3 3 3 2 1

Note: Enter correlation levels 1: Slight (Low) 2: Moderate (Medium) 3: Substantial (High)

If there is no correlation, put “-”

Assessment Methods: Observation /record /Internal Lab Exam / End Lab Exam
Internal Lab Exam: 40Marks - Day to Day work (25) + Record (5) + Test (10)

External Lab Exam: 60 Marks - Description & Program (30) + Output (20) + Viva-Voice (10)

5
Introduction About Lab

There are 33 systems installed in this Lab. This lab is very useful for students because they are
familiar with different Operating Systems so that they can execute their programs in different
programming environments. Each student has a separate login because students can save their work in
their own accounts. Each student work is safe and secure from other students. Systems are provided for
students in the 1:1 ratio. Systems are assigned numbers and same system is allotted for students when
they do the lab. The hardware and software configurations are as follows:

System:
Component Details
Manufacturer & Model : HP DX7400
Processor : Intel Pentium Dual Core 2.0GHZ

Memory (RAM) : 1 GB DDR2

Hard Disk : 1600 GB SATA

Mouse : HP Optical Mouse

Keyboard : HP PS/2

Monitor : 17” LCD


Network Interface Card : 10/100/1000 Mbps - Broadcom NetLink (TM) Gigabit Ethernet

Software’s:
 System Software: Windows XP (32-bit Operating System)
 MS Office 2007
 .NET Framework 4.7.2
 Antivirus: Symantec Endpoint Protection 11.0
 Adobe Family(Adobe Reader 11)
 Macromedia Flash 8
Open Source Software’s:

 JDK 8
 Dev C++ 5.1
 Code Blocks 16
 Star UML 5.0
 Python 2.7.9, 3.6 and IDLE
 Strawberry Perl
 WinRAR
 Players: Windows Media Player 11, VLC Player
 Browsers: Chrome, Firefox

6
Guidelines to the Students

1. Attendance in the lab is MANDATORY

2. Use of any sort of digital gadget is strictly PROHIBITED during lab sessions
3. Students should come with thorough preparation for the experiment to be
conducted
4. Students will not be permitted to attend the laboratory unless they bring the
practical record fully completed in all respects pertaining to the experiment
conducted in the previous class.
5. Practical record should be neatly maintained.
6. They should obtain the signature of the staff-in-charge in the observation book
after completing each experiment.
7. Theory regarding each experiment should be written in the practical record before
procedure in your own words.
8. Ask lab technician for assistance if you have any problem.
9. Do not download or install software without the assistance of the laboratory
technician.
10. Do not alter the configuration of the system.
11. Turnoff the systems after use.

7
List of Experiments

1. a) Write a C program to generate a Fibonacci series using recursive function.


b) Write a C program to find the GCD of given numbers using recursive function.
c) Write a C program to solve the Towers of Hanoi problem using recursive function.

2. a) Design, Develop and Implement a C program to perform linear search for a key value in a given
list.
b) Design, Develop and Implement a C program to perform Binary search for a key value in a given
list.

3. 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.

4. a) Design, Develop and Implement a C program that implement Selection Sort to sort a given list of
integers.
b) Design, Develop and Implement a C program that implement Bubble Sort to sort a given list of
integers.

5 a) Design, Develop and Implement C program that implement Quick Sort to sort a given list of
integers.
b) Design, Develop and Implement C program that implement Merge Sort to sort a given list of
integers.

6. 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

7. 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
8
Support the program with appropriate functions for each of the above operations

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

9.Design, Develop and Implement a menu driven Program in C for the following operations
on QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element into QUEUE
b. Delete an Element from QUEUE
c. Demonstrate Overflow and Underflow situations on QUEUE
d. Display the status of QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

10 a) Design, Develop and Implement a C program to implement Binary tree traversals using
iterative functions.
b) Design, Develop and Implement a C program to implement Binary tree traversals using
recursive functions.

11. 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, 15, 22, 8, 45, 24, 18, 71, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit

12. 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.

9
1. a) Write a C program to generate a Fibonacci series using recursive function.

#include <stdio.h>
int fibonacci(int);
void main( )
{
int n,i;
printf("Enter n value: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("%d ", fibonacci(i));
}
}
int fibonacci(int num)
{
if(num==0 || num==1)
{
return num;
}
else
{
return fibonacci(num-1) + fibonacci(num-2);
}
}

Output:
Enter n value: 8
0 1 1 2 3 5 8 13

10
b) Write a C program to find the GCD of given numbers using recursive function.

#include <stdio.h>
int GCD(int num1, int num2);
void main()
{
int num1, num2;
printf( " Enter any two positive numbers \n");
scanf("%d %d", &num1, &num2);
printf(" GCD of two numbers %d and %d is %d", num1, num2, GCD(num1, num2));

}
int GCD (int num1, int num2)
{
if (num2!= 0)
{
return GCD(num2, num1 % num2);
}
else
{
return num1;
}
}

Output:
Enter any two positive numbers
84 79
GCD of two numbers 84 and 79 is 1

11
c) Write a C program to solve the Towers of Hanoi problem using recursive function.

#include <stdio.h>
void towerOfHanoi(int,char,char,char);
void main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d",&num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towerOfHanoi(num,'A','C','B');

}
void towerOfHanoi(int n,char from_rod,char to_rod, char aux_rod)
{
if (n==1)
{
printf("Move disk 1 from rod %c to rod %c \n",from_rod,to_rod);
return;
}
towerOfHanoi(n-1, from_rod,aux_rod,to_rod);
printf("Move disk %d from rod %c to rod %c\
n",n,from_rod,to_rod); towerOfHanoi(n-1, aux_rod, to_rod,
} from_rod);

Output:
Enter the number of disks: 5
The sequence of moves involved in the Tower of Hanoi are:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
Move disk 4 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 2 from rod C to rod A
Move disk 1 from rod B to rod A

12
Move disk 3 from rod C to rod B
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 5 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
Move disk 3 from rod B to rod A
Move disk 1 from rod C to rod B
Move disk 2 from rod C to rod A
Move disk 1 from rod B to rod A
Move disk 4 from rod B to rod C
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C

13
2 a) Design, Develop and Implement a C program to perform linear search for a key value in a
given list.

#include <stdio.h>
void main()
{
int a[20],i,n,key,flag=0;
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;++i)
{
printf("\nEnter the array elements: ");
scanf("%d",&a[i]);
}
printf("Enter the search element :\n");
scanf("%d",&key);
for(i=0; i<n; i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("\nThe Search element %d is found",key);
else
printf("\nThe Search element %d is not found",key);
}

Output:
Enter the value of n:
5
Enter the elements:
Enter the array elements: 23
Enter the array elements: 12
Enter the array elements: 112
Enter the array elements: 98
Enter the array elements: 75
Enter the search element:
112
The Search element 112 is found
14
2b) Design, Develop and Implement a C program to perform Binary search for a key value in
a given list.

#include <stdio.h>
void main()
{
int a[20],i,n,key,first,last,mid;
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;++i)
{
printf("\nEnter the array elements: ");
scanf("%d",&a[i]);
}
printf("Enter the search element :\n");
scanf("%d",&key);
first=0;
last=n-1;
mid=(first+last)/2;
while (first<=last)
{
if(a[mid]<key)
first = mid+1;
else if(a[mid]==key)
{
printf("%d found at location %d.\n", key, mid+1);
break;
}
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
printf("%d isn't present in the list.\n",key);
}

Output 1:
Enter the value of n:
6
Enter the elements in ascending order:
Enter the array elements: 24
Enter the array elements: 42
Enter the array elements: 78

15
Enter the array elements: 124
Enter the array elements: 139
Enter the array elements: 456
Enter the search element:
139
139 found at location 5.

Output 2:
Enter the value of n:
5
Enter the elements in ascending order:
Enter the array elements: 45
Enter the array elements: 73
Enter the array elements: 89
Enter the array elements: 95
Enter the array elements: 135
Enter the search element:
157
157 isn't present in the list.

16
3 ) 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>

int key[20],n,m;
int *ht,index;
int count = 0;

void insert (int key)


{
index = key % m;
while(ht[index] != -1)
{
index = (index+1) % m;
}
ht[index] = key;
count++;
}

void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for(i=0; i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}

void main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *) malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
17
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for (i=0; i<n; i++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\nHash table is full. Cannot insert the record %d key",i+1);
break;
}
insert(key[i]);
}
display();

Output:
Enter the number of employee records (N): 7
Enter the two digit memory locations (m) for hash table: 15
Enter the four digit key values (K) for N Employee Records:
1234
2345
1678
2674
1297
2478
3498
Hash Table contents are:
T[0] --> -1
T[1] --> -1
T[2] --> -1
T[3] --> 2478
T[4] --> 1234
T[5] --> 2345
T[6] --> 2674
T[7] --> 1297
T[8] --> 3498

18
T[9] --> -1
T[10] --> -1
T[11] --> -1
T[12] --> -1
T[13] --> 1678
T[14] --> -1

19
4a) Design, Develop and Implement a C program that implement Selection Sort to sort a given
list of integers.

#include <stdio.h>
void main()
{
int temp,i,j,a[20],n,pos;
printf("Enter the number of elements:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element of arrays\n");
scanf("%d",&a[i]);
}
printf("\nThe list before sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[pos])
{
pos=j;
}
}
if(pos!=i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("\nThe list after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Output:
Enter the number of elements:
10
Enter the element of arrays
67
Enter the element of arrays
98
20
Enter the element of arrays 12
Enter the element of arrays 89
Enter the element of arrays 47
Enter the element of arrays 115
Enter the element of arrays 76
Enter the element of arrays 34
Enter the element of arrays 7
Enter the element of arrays 57
The list before sorting
679812894711576347

57
The list after sorting
71234475767768998115

21
4b) Design, Develop and Implement a C program that implement Bubble Sort to sort a given
list of integers.

#include <stdio.h>
void main()
{
int temp, i, j, a[20],n;
printf("Enter the number of elements:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element of arrays\n");
scanf("%d",&a[i]);
}
printf("\nThe list before sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe list after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Output:
Enter the number of elements:
10
Enter the element of arrays
43
Enter the element of arrays
99
Enter the element of arrays
73
Enter the element of arrays
22
19
Enter the element of arrays 34
Enter the element of arrays 4
Enter the element of arrays 48
Enter the element of arrays 67
Enter the element of arrays 134
Enter the element of arrays 59
The list before sorting
43997319344

486713459
The list after sorting
4 1934434859677399134

23
5a) Design, Develop and Implement C program that implement Quick Sort to sort a given list
of integers.

#include <stdio.h>
int partition(int a[],int first,int last)
{
int i,j,pivot,temp;
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
return j;
}

void quicksort(int a[], int f, int l)


{
int j;
if(f<l)
{
j=partition(a,f,l);
quicksort(a,f,j-1);
quicksort(a,j+1,l);
}
}

void main()
{
int i,n,a[25];
printf("Enter n value \n");
scanf("%d",&n);
printf("Enter elements into array:");
for(i=0;i<n;i++)
{
printf("\n Enter array element\n");
24
scanf("%d",&a[i]);
}
printf("Before Sorting:\n");
for(i=0;i<n;i++)
printf(" %d\t",a[i]);
quicksort(a,0,n-1); printf("\
nAfter sorting :\n");
for(i=0;i<n;i++)
printf(" %d\t",a[i]);
}
Output:
Enter n value 10

Enter elements into array:


Enter array element 57
Enter array element 94
Enter array element 63
Enter array element 21
Enter array element 89
Enter array element 192
Enter array element 17
Enter array element 104
Enter array element 9
Enter array element 43

Before Sorting:
5794632189192171049

43
After sorting:
917214357638994104192

25
5b) Design, Develop and Implement C program that implement Merge Sort to sort a given
list of integers.

#include <stdio.h>
void mergeSubArray(int a[],int lb,int lr,int rb,int rr)
{
int na,nb,nc,k,c[25];
na=lb;
nb=rb;
nc=lb;
while((na<=lr)&&(nb<=rr))
{
if(a[na]<a[nb])
c[nc]=a[na++];
else
c[nc]=a[nb++];
nc++;
}
if(na>lr)
{
while(nb<=rr)
c[nc++]=a[nb++];
}
else
{
while(na<=lr)
c[nc++]=a[na++];
}
for(k=lb;k<=rr;k++)
a[k]=c[k];
}

void mergesort(int a[],int beg,int end)


{
int mid;
if(beg<end)
{
mid=(beg+end)/2;
mergesort(a,beg,mid);
mergesort(a,mid+1,end);
mergeSubArray(a,beg,mid,mid+1,end);
}
}

void main ()
{
int i, n, a[25];
printf("Enter n value \n");
scanf("%d",&n);
26
printf("Enter elements into array:");
for(i=0;i<n;i++)
{
printf("\n Enter array element\n");
scanf("%d",&a[i]);
}
printf("Before Sorting:\n");
for(i=0;i<n;i++)
printf(" %d\t",a[i]);
mergesort(a,0,n-1); printf("\
nAfter sorting :\n");
for(i=0;i<n;i++)
printf(" %d\t",a[i]);

Output:
Enter n value
9

Enter elements into array:


Enter array element
96
Enter array element
37
Enter array element
58
Enter array element
186
Enter array element
12
Enter array element
25
Enter array element
91
Enter array element
19
Enter array element
59

Before Sorting:
96 37 58 186 12 25 91 19 59

After Sorting:
12 19 25 37 58 59 91 96 186

27
6) 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>
struct node
{
char usn[25],name[25],branch[25];
int sem;
long int phone;
struct node *link;
}*temp,*first=NULL,*last=NULL;
int count;
void create()
{

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


printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",temp->usn, temp->name,temp->branch,
&temp->sem,&temp->phone);
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->link=temp;
last=temp;
}
temp->link=NULL;
}
void insertfront()
{

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


printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",temp->usn, temp->name,temp->branch,
&temp->sem,&temp->phone);
if(first==NULL)
{
first=temp;
last=temp;
temp->link=NULL;
28
}
else
{
temp->link = first;
first=temp;
}
}
void deletefront()
{

if(first==NULL)
{
printf("\nLinked list is empty");
}
else
{
temp=first;
first=first->link;
free(temp);
printf("Node deleted\n");
}
}

void insertend()
{

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


printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",temp->usn, temp->name,temp->branch,
&temp->sem,&temp->phone);
if(first==NULL)
{
first=temp;
last=temp;
temp->link=NULL;
}
else
{
temp->link=last->link;
last->link=temp;
last=temp;
}
}

void deleteend()
{
struct node *prev;
temp=first;
if(temp==NULL)
29
{
printf("\nLinked List is empty");
}
else if(temp->link==NULL)
{
free(temp);
first=NULL;
printf("Node deleted\n");
}
else
{
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
free(temp);
prev->link = NULL;
last=prev;
printf("Node deleted\n");
}
}

void display()
{
temp=first;
if(first==NULL)
{
printf("\nSLL list is empty\n");
}
else
{
count=0;
while(temp!=NULL)
{
printf("\n USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|",
temp->usn,temp->name,temp->branch, temp->sem,temp->phone);
temp=temp->link;
count++;
}
printf("\n No of student nodes is %d \n",count);
}
}

void stackdemo()
{
int ch;
while(1)
{
30
printf("\nStack Demo using SLL\n");
printf("\n1:Push operation 2: Pop operation 3: Display 4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);
switch(ch)
{
case 1: insertfront();
break;
case 2: deletefront();
break;
case 3: display();
break;
default : return;
}
}
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n1:Create SLL of Student Nodes 2. Display status 3. InsertAtEnd
4. DeleteAtEnd 5.StackDemo 6. Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
break;
case 2: display();
break;
case 3: insertend();
break;
case 4: deleteend();
break;
case 5: stackdemo();
break;
case 6: exit(0);
default: printf("\nPlease enter the valid choice");
}
}
}

Output:
1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.
Exit

31
Enter your choice:1
Enter the usn, Name, Branch, sem, PhoneNo of the student:20it101
xyz
it
1
92465
1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.
Exit

Enter your choice:2

USN:20it101| Name:xyz| Branch:it| Sem:1| Ph:92465|


No of student nodes is 1
1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.
Exit
Enter your choice:3

Enter the usn,Name,Branch, sem,PhoneNo of the student:20it205


mns
it
2
12345

1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.


Exit
Enter your choice:2

USN:20it101| Name:xyz| Branch:it| Sem:1| Ph:92465|


USN:20it205| Name:mns| Branch:it| Sem:2| Ph:12345|
No of student nodes is 2
1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.
Exit

32
Enter your choice:4
Node deleted

1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.


Exit

Enter your choice:2


USN:20it101| Name:xyz| Branch:it| Sem:1| Ph:92465|
No of student nodes is 1

1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.


Exit
Enter your choice:5

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo1
Enter the usn, Name, Branch, sem, PhoneNo of the student:20it207
pqr
it
1
34567

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo3
USN:20it207| Name:pqr| Branch:it| Sem:1| Ph:34567|
USN:20it101| Name:xyz| Branch:it| Sem:1| Ph:92465|
No of student nodes is 2

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit

33
Enter your choice for stack demo3
USN:20it207| Name:pqr| Branch:it| Sem:1| Ph:34567|
USN:20it101| Name:xyz| Branch:it| Sem:1|
Ph:92465| No of student nodes is 2

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit

Enter your choice for stack demo2


Node deleted
Stack Demo using SLL
1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo3
USN:20it101| Name:xyz| Branch:it| Sem:1| Ph:92465|
No of student nodes is 1

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo2
Node deleted
Stack Demo using SLL
1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo2
Linked list is empty

Stack Demo using SLL


1: Push operation 2: Pop operation 3: Display 4: Exit
Enter your choice for stack demo4
1: Create SLL of Student Nodes 2. Display status 3. InsertAtEnd 4. DeleteAtEnd 5. StackDemo 6.
Exit
Enter your choice:6

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
int top=-1,i;
int s[MAX];
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;

}
void push()
{
int item;
if(isFull())
printf("Stack is Full\n");
else
{
printf("Enter element\n");
scanf("%d",&item); top+
+;
s[top]=item;
}
}

void pop()
{
if(isEmpty())
printf("Stack is Empty\n");
35
else
{
top--;
}
}
void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("%d\t", s[i]);

printf("\nReverse of stack content are:\n");


for(i=0; i<=top; i++)
printf("%d\t", s[i]);
for(i=0; i<=top/2; i++)
{
if( s[i] != s[top-i] )
{
flag = 0;
break;
}
}
if(flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}
void display()
{
if(isEmpty())
printf("Stack is Empty\n");
else
{
for(i=top;i>=0;i--)
printf("%d\t",s[i]);
}
}

void main()
{
int choice;
while (1)
{
printf("\n1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit\n");
36
printf("enter your choice\n");
scanf("%d",&choice);
if(choice==1)
push();
else if(choice==2)
pop();
else if(choice==3)
display();
else if(choice==4)
palindrome();
else if(choice==5)
exit(5);
else
printf("Invalid Symbol\n");
}
}

Output:
1: Push 2. Pop 3. Display 4. Check Palindrome 5. Exit
enter your choice
1
Enter element
10

1: Push 2. Pop 3. Display 4.Check Palindrome 5. Exit


enter your choice
1
Enter element
20

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
1
Enter element
30

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice

37
1
Enter element
40

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
1
Enter element
50

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
1
Stack is Full

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
3
50 40 30 20 10

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
4
Stack content are:
50 40 30 20 10
Reverse of stack content
are: 10 20 30 40
50
It is not a palindrome number

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice

38
3

39
50 40 30 20 10

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
3
20 10

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
2
Stack is Empty

40
1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit
enter your choice
1
Enter element
101

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
4
Stack content are:
101
Reverse of stack content are:
101
It is palindrome number

1:Push 2. Pop 3.Display 4.Check Palindrome 5. Exit


enter your choice
5

41
8) 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 <ctype.h>
#include<stdlib.h>
char s[50];
int top = -1;
void push(char elem)
{
s[++top] = elem;
}
char pop()
{
return (s[top--]);
}
int precedence(char elem)
{
switch (elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
default : return -1;
}
}
int isoperator(char elem)
{
switch(elem)
{
case '(':
case '+':
case '-':
case '*':
case '/': return 1;
default : return 0;
}
}
void main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nEnter the Infix Expression : ");
scanf("%s", infx);
push('#');
42
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
pop();
}
else
{
if(isoperator(ch)==0)
{
printf("\n Expression contain invalid operator\n");
exit(0);
}
else
{
while (precedence(s[top]) >= precedence(ch))
pofx[k++] = pop();
push(ch);
}
}
}
while (s[top] != '#')
pofx[k++] = pop();
pofx[k] = '\0';
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}

Output:
Enter the Infix Expression : (a+b)*(c-d)/(d*f)+f/g

Given Infix Expn: (a+b)*(c-d)/(d*f)+f/g Postfix Expn: ab+cd-*df*/fg/+

43
9) Design, Develop and Implement a menu driven Program in C for the following operations
on QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element into QUEUE
b. Delete an Element from QUEUE
c. Demonstrate Overflow and Underflow situations on QUEUE
d. Display the status of QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

#include <stdio.h>
#include<stdlib.h>
#define MAX 5
int q[MAX];
int front=-1,rear=-1;
void insert()
{
int item;
if(rear==MAX-1)
{
printf("Queue Overflow\n");
}
else
{
printf("Enter item to insert into queue\n");
scanf("%d",&item);
if(front==-1)
{
front=0;
rear = 0;
}
else
{
rear++;
}
q[rear]=item;
}
}

void del()
{
int item;
if(front==-1)
{
printf("Queue Underflow\n");
}
else
{
item=q[front];
44
printf("Deleted element from the queue is: %d",item );

if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
}
void display ()
{
int i ;
if(front==-1)
{
printf("Queue Empty\n");
}
else
{
printf("Queue elements are:\n");
for(i=front;i<=rear;i++)
{
printf(" %d", q[i]);
}
}
}
void main()
{
int ch;
while(1)
{
printf("\n1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3.
Display 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid choice");
}
}
}
45
Output:
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Enter item to insert into queue
10

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Enter item to insert into queue
20

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Enter item to insert into queue
30

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Enter item to insert into queue
40

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Enter item to insert into queue
50

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 1
Queue Overflow

1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit

46
Enter Your Choice: 3
Queue elements are:
10 20 30 40 50
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 10
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 3
Queue elements are:
20 30 40 50
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 20
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 30
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 40
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 3
Queue elements are:
50
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 50
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 2
Queue Underflow
1.Insertion and Overflow Demo 2. Deletion and Underflow Demo 3. Display 4. Exit
Enter Your Choice: 4

47
10a) Design, Develop and Implement a C program to implement Binary tree traversals
without using recursive functions.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node* create()
{
struct node *temp;
int x;
printf("Enter data\n");
scanf("%d",&x);
if(x==-1)
return NULL;
temp=(struct node*)malloc(sizeof(struct
node)); temp->data=x;
printf("Enter left child of %d:\n",x);
temp->left=create();
printf("Enter right child of %d:\n",x);
temp->right=create();
return temp;
}
void non_inorder( struct node *r)
{
int top=0;
struct node *s[20],*pt=r;
s[0]=NULL;
while(pt != NULL)
{
s[++top] = pt;
pt = pt->left;
}
pt = s[top--];
while(pt != NULL)
{
printf("%d\t",pt->data);
if(pt->right != NULL)
{
pt = pt->right;
48
while(pt != NULL)
{
s[++top] = pt;
pt = pt->left;
}
}
pt = s[top--];
}
}
void main ()
{
struct node *root;
root=create();
printf("\nThe Inorder traversal of tree is:\n");
non_inorder(root);
}
Output:
Enter data
23

Enter left child of 23:


Enter data
45

Enter left child of 45:


Enter data
57

Enter left child of 57:


Enter data
49

Enter left child of 49:


Enter data
-1
Enter right child of 49:

49
Enter data
34

Enter left child of 34:


Enter data
-1

Enter right child of 34:


Enter data
-1

Enter right child of 57:


Enter data
93

Enter left child of 93:


Enter data
-1

Enter right child of 93:


Enter data
-1

Enter right child of 45:


Enter data
93

Enter left child of 93:


Enter data
-1

Enter right child of 93:

50
Enter data
-1

Enter right child of 23:


Enter data
67

Enter left child of 67:


Enter data
-1

Enter right child of 67:


Enter data
-1

The Inorder traversal of tree is:


49 34 57 93 45 93 23 67

51
10b) Design, Develop and Implement a C program to implement Binary tree traversals
using recursive functions.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};

struct node* create()


{
struct node *temp;
int x;
printf("Enter data\n");
scanf("%d",&x);
if(x==-1)
return NULL;
temp=(struct node*)malloc(sizeof(struct
node)); temp->data=x;
printf("Enter left child of %d:\n",x);
temp->left=create();
printf("Enter right child of %d:\n",x);
temp->right=create();
return temp;
}
void preorder(struct node *t)
{
if(t!=NULL)
{
printf("%d\t",t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct node *t)
{
if(t!=NULL)
{
postorder(t->left);

52
postorder(t->right);
printf("%d\t",t->data);
}
}
void Inorder(struct node *t)
{
if(t!=NULL)
{
Inorder(t->left);
printf("%d\t",t->data);
Inorder(t->right);
}
}
void main()
{
struct node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
printf("\nThe postorder traversal of tree is:\n");
postorder(root);
printf("\nThe Inorder traversal of tree is:\n");
Inorder(root);
}

Output:
Enter data
78
Enter left child of 78:
Enter data
56
Enter left child of 56:
Enter data
98
Enter left child of 98:
Enter data
12

53
Enter left child of 12:
Enter data
34
Enter left child of 34:
Enter data
-1
Enter right child of 34:
Enter data
57
Enter left child of 57:
Enter data
-1
Enter right child of 57:
Enter data
-1
Enter right child of 12:
Enter data
43
Enter left child of 43:
Enter data
-1
Enter right child of 43:
Enter data
2
Enter left child of 2:
Enter data
-1
Enter right child of 2:
Enter data
-1
Enter right child of 98:
Enter data

54
52
Enter left child of 52:
Enter data
-1
Enter right child of 52:
Enter data
-1
Enter right child of 56:
Enter data
-1
Enter right child of 78:
Enter data
19
Enter left child of 19:
Enter data
-1
Enter right child of 19:
Enter data
-1

The preorder traversal of tree is:


78 56 98 12 34 57 43 2 52 19
The postorder traversal of tree is:
57 34 2 43 12 52 98 56 19 78
The Inorder traversal of tree is:
34 57 12 43 -2 98 52 56 78 19

55
11) 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, 15, 22, 8, 45, 24, 18, 71, 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 node
{
int data;
struct node *lchild;
struct node *rchild;
};
struct node *root;
struct node* create()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter The value: \n");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert(struct node *root,struct node *temp)
{
if(temp->data<root->data)
{
if(root->lchild==NULL)
root->lchild=temp;
else
insert(root->lchild,temp);
}
if(temp->data>root->data)
{
if(root->rchild==NULL)
root->rchild=temp;
else
insert(root->rchild,temp);
}
}

56
void search(struct node *root)
{
int key;
struct node *temp;
if(root==NULL)
{
printf("Binary Search Tree is empty\n");
return;
}
else
{
printf("Enter Element to be searched: \n");
scanf("%d", &key);
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\nKey element %d is present in Binary Search Tree", key);
return;
}
else if(key<temp->data)
temp=temp->lchild;
else
temp=temp->rchild;
}
printf("\nKey element %d is not found in the BST",key);
}
}
void inorder(struct node *t)
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%d\t",t->data);
inorder(t->rchild);
}
}
void preorder(struct node *t)
{
if(t!=NULL)
{
printf("%d\t",t->data);
57
preorder(t->lchild);
preorder(t->rchild);
}
}
void postorder(struct node *t)
{
if (t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%d\t",t->data);
}
}
void main()
{
int ch,key,val,i,n;
struct node *temp;
while(1)
{
printf("\n1.Create a BST 2.BST Traversal 3. Search 4.Exit \n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the number of elements:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
}
break;
case 2:if(root==NULL)
printf("\nBinary Search Tree is empty\n");
else
{
printf("Inorder is.....\n");
inorder(root); printf("\
nPostorder is................\n");
postorder(root);
58
printf("\nPreorder is.. . .\n");
preorder(root);

}
break;
case 3:search(root);
break;

case 4:exit(0);
}
}
}

Output:
1.Create a BST 2.BST Traversal 3. Search 4. Exit
Enter your choice:
1
Enter the number of elements:
11
Enter The value:
97
Enter The value:
43
Enter The value:
24
Enter The value:
57
Enter The value:
47
Enter The value:
63
Enter The value:
112
Enter The value:

59
104
Enter The value:
134
Enter The value:
123
Enter The value:
157
1.Create a BST 2.BST Traversal 3. Search 4.Exit
Enter your choice:
2
Inorder is......
24 43 47 57 63 97 104 112 123 134 157
Postorder is....
24 47 63 57 43 104 123 157 134 112 97
Preorder is.....
97 43 24 57 47 63 112 104 134 123 157
1.Create a BST 2.BST Traversal 3. Search 4. Exit
Enter your choice:
3
Enter Element to be searched:
134

Key element 134 is present in Binary Search Tree


1.Create a BST 2.BST Traversal 3. Search 4. Exit
Enter your choice:
4

60
12) 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>
#include<stdlib.h>

int a[50][50],n,visited[50];
int q[20],front=-1,rear=-1;
int s[20], top=-1,count=0;

void bfs(int v)
{
int i, cur;
visited[v]=1; q[+
+rear]=v;
while(front!=rear)
{
cur=q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear]=i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}

void dfs(int v)
{
int i;
visited[v]=1; s[+
+top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
61
dfs(i);
}
}
}
void main()
{

int ch, start, i,j;


printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
printf("\n1.Print all nodes reachable from a given starting node using BFS");
printf("\n2.Print all nodes reachable from a given starting node using DFS");
printf("\n3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;
case 2: printf("\nNodes reachable from starting vertex %d are:\n",start);
dfs(start);
break;
case 3:
exit(0);
default: printf("\nPlease enter valid choice:");
}
}
62
Output:
Enter the number of vertices in graph: 5
Enter the adjacency matrix:
0
1
1
0
0
1
0
1
1
1
1
1
0
1
0
0
1
1
0
1
0
1
0
1
0

Enter the starting vertex: 1

1.Print all nodes reachable from a given starting node using BFS
2.Print all nodes reachable from a given starting node using
DFS 3:Exit
Enter your choice: 1
Nodes reachable from starting vertex 1 are: 2 3 4 5

63
References

1. Mark Allen Weiss , Structures and Fourth Edition , Pearson.


2. Ellis Horowitz, Sartaj Sahni, Fundamentals of Data Structures Computer Science Press.
3. Michel T. Goodrich, Roberto Tamassia, David Mount, Structures and Algorithm 2nd
Edition, John Wiley & Sons, Inc.
4. Debasis Samanta, Data Second Edition, PHI, 2012, New Delhi, India.

64

You might also like