0% found this document useful (0 votes)
43 views81 pages

DS Manual - 22scheme - CSBS

DS Manual 3rd year

Uploaded by

tanushree9663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views81 pages

DS Manual - 22scheme - CSBS

DS Manual 3rd year

Uploaded by

tanushree9663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 81

K.

S SCHOOL OF ENGINEERING & MANAGEMENT,


BANGALORE - 109

“DATA STRUCTURES LABORATORY”


Subject code: BCSL305

Prepared by: -

Mrs. Jayashree Krishna Murthy Mr. Prashanth


Assistant Professor Assistant Professor
Dept. of CS& BS, KSSEM Dept. of CS& BS, KSSEM

Department of Computer Science & Business Systems


K.S SCHOOL OF ENGINEERING & MANAGEMENT, BANGALORE - 109
K.S. SCHOOL OF ENGINEERING AND MANAGEMENT
# 15, Mallasandra, off Kanakapura Road, Bengaluru-560109
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS

Course: Data Structures Laboratory


Type: Core Course Academic Year: 2023-2024
Code:BCSL305
No. of Hours per week
Theory Practical/Field Work/Allied Total/Week Total teaching hours
(Lecture Class) Activities
0 2 2 28 Hours
Marks
Internal Assessment Examination Total Credits
50 50 100 1

Aim/Objective of the Course:


1. Explain fundamentals of data structures and their applications essential for programming/problem
solving.
2. Illustrate linear representation of data structures: Stack, Queues, Lists, Trees and Graphs.
3. Demonstrate sorting and searching algorithms.
4. Find suitable data structure during application development/Problem Solving.

Course Learning Outcomes:


After completing the course, the students will be able to

CO1 Analyze dynamic memory management Applying (K3)

CO2 Design & demonstrate linear data structures and their applications such as Applying (K3)
stacks, queues and lists
CO3 Analyze and implement non-Linear data structures and their Applying (K3)
applications such as trees and graphs
Institution Vision & Mission

VISION: “To impart quality education in engineering and management to meet


technological, business and societal needs through holistic education and research”

MISSION:

K.S. School of Engineering and Management shall,


l Establish state-of-art infrastructure to facilitate effective dissemination of
technical and Managerial knowledge.
l Provide comprehensive educational experience through a combination of
curricular and experiential learning, strengthened by industry-institute-
interaction.
l Pursue socially relevant research and disseminate knowledge.
l Inculcate leadership skills and foster entrepreneurial spirit among students.

Department Vision & Mission


VISION: “To produce quality Computer Science professional, possessing excellent
technical knowledge, skills, personality through education and research.”

MISSION:
Department of Computer Science and Engineering shall,
l Provide good infrastructure and facilitate learning to become competent
engineers who meet global challenges.
l Encourages industry institute interaction to give an edge to the students.
l Facilitates experimental learning through interdisciplinary projects.
l Strengthen soft skill to address global challenges.
K.S. SCHOOL OF ENGINEERING AND MANAGEMENT
# 15, Mallasandra, off Kanakapura Road, Bengaluru-560109
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS

Data Structures Laboratory


(BCSL305)

INDEX
Sl No Programs List Page
No.
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having three fields.
1
The first field is the name of the Day (A dynamically allocated String), The second field
is the date of the Day (A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data from the
keyboard and to print weeks activity details report on screen.
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 5
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 8
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
14
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: +, -, *, /,
%,^ 17
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 21
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,
Programme, Sem, PhNo
a) Create a SLL of N Students Data by using front insertion. 26
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 34
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 43
theresult 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 52
c. Search the BST for a given element (KEY) and report the appropriate message
d. 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. 61
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
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 65
Hashfunction 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.
13. Viva Questions 70
DATA STRUCTURES LABORATORY
SEMESTER – III

Course Code BCSL305 CIE Marks 50


Number of Contact Hours/Week 0:0:2 SEE Marks 50
Total Number of Lab Contact Hours 28 Exam Hours 03
Credits – 1
Course Learning Objectives: This course (BCSL305) will enable students to:
This laboratory course enables students to get practical experience in design, develop, implement, analyze and
evaluation/testing of
● Dynamic memory management
● Linear data structures and their applications such as stacks, queues and lists
● Non-Linear data structures and their applications such as trees and graphs

Descriptions (if any):


 Implement all the programs in ‘C ’ Programming Language and Linux OS.
Programs List:
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7
days of a week. Each Element of the array is a structure having three fields. The first field is
the name of the Day (A dynamically allocated String), The second field is the date of the Day
(A integer), the third field is the description of the activity for a particular day (A dynamically
allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data from
the keyboard and to print weeks activity details report on screen.

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, Programme, Sem,
PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
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 operationson Singly Circular
Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
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
d. 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.
Laboratory Outcomes: The student should be able to:
 Analyze and Compare various linear and non-linear data structures
 Demonstrate the working nature of different types of data structures and their applications.
 Use appropriate searching and sorting algorithms for the give scenario.
 Apply the appropriate data structure for solving real world problems

Conduct of Practical Examination:


 Experiment distribution
o For laboratories having only one part: Students are allowed to pick one experiment
from the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal
opportunity.
 Change of experiment is allowed only once and marks allotted for procedure to be made zero
of the changed part only.
 Marks Distribution (Coursed to change in accordance with university regulations)
c) For laboratories having only one part – Procedure + Execution + Viva-Voce: 15+70+15
= 100 Marks
d) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks
DATA STRUCTURES LABORATORY BCSL305

Laboratory Experiments:
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7 days of a
week. Each Element of the array is a structure having three fields. The first field is the name of the Day
(A dynamically allocated String), The second field is the date of the Day (A integer), the third field is
the description of the activity for a particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data from the
keyboard and to print weeks activity details report on screen.

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

// Structure to represent a day in the calendar


struct Day {
char *dayName;
int date;
char *activity;
};

// Function to create a day in the calendar


struct Day create() {
struct Day day;
day.dayName = (char *)malloc(20 * sizeof(char));
day.activity = (char *)malloc(100 * sizeof(char));

printf("Enter the day name: ");


scanf("%s", day.dayName);
printf("Enter the date: ");
scanf("%d", &day.date);
printf("Enter the activity: ");
scanf(" %[^\n]s", day.activity);

return day;
}

// Function to read data for all seven days


void read(struct Day calendar[7]) {
for (int i = 0; i < 7; i++) {
printf("\nDay %d:\n", i + 1);
calendar[i] = create();
}
}
// Function to display the calendar
void display(struct Day calendar[7]) {

Dept of CS&BS, KSSEM Page 1


DATA STRUCTURES LABORATORY BCSL305

Dept of CS&BS, KSSEM Page 2


DATA STRUCTURES LABORATORY BCSL305

1st Program Output- Calendar

Please enter data for each day of the week:

Day 1:
Enter the day name: sunday
Enter the date: 21
Enter the activity: singing

Day 2:
Enter the day name: monday
Enter the date: 22
Enter the activity: dancing

Day 3:
Enter the day name: tuesday
Enter the date: 23
Enter the activity: drama

Day 4:
Enter the day name: wednesday
Enter the date: 24
Enter the activity: group singing

Day 5:
Enter the day name: thursday
Enter the date: 25
Enter the activity: group dancing

Day 6:
Enter the day name: friday
Enter the date: 26
Enter the activity: quiz

Day 7:
Enter the day name: saturday
Enter the date: 27
Enter the activity: sports

Weekly Calendar:

Day 1:
Name: sunday
Date: 21
Activity: singing

Day 2:
Name: monday
Date: 22
Activity: dancing

Day 3:

Dept of CS&BS,KSSEM Page 3


DATA STRUCTURES LABORATORY BCSL305
Name: tuesday
Date: 23
Activity: drama

Day 4:
Name: wednesday
Date: 24
Activity: group singing

Day 5:
Name: thursday
Date: 25
Activity: group dancing

Day 6:
Name: friday
Date: 26
Activity: quiz

Day 7:
Name: saturday
Date: 27
Activity: sports

Dept of CS&BS,KSSEM Page 4


DATA STRUCTURES LABORATORY BCSL305

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>

void readstr(void);
void readsearchstr(void);
void readreplacestr(void);
void matching(char str[100]);
char mainstr[100],pat[100],replace[100],ans[100];
void main()
{
readstr();
readsearchstr();
readreplacestr();
matching(mainstr);
}
void readstr(void)
{
printf("\n enter a string\n");
gets(mainstr);
}
void readsearchstr(void)
{
printf("enter a search string\n");
gets(pat);
}
void readreplacestr(void)
{
printf("\n enter a replace string\n");
gets(replace);
}
void matching(char mainstr[100])
{
char ans[100];
int i,j,c,m,k,flag;
i=m=c=j=flag=0;
while(mainstr[c]!='\0')
{
if(mainstr[m]==pat[i])
{
i++;
m++;
if(pat[i]=='\0')
Dept of CS&BS,KSSEM Page 5
DATA STRUCTURES LABORATORY BCSL305
{
for(k=0;replace[k]!='\0';k++,j++)
ans[j]=replace[k];
i=0;
c=m;
}
}
else
{
flag=1;
ans[j]=mainstr[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
if(flag==0)
{
printf("search string is not present\n");
printf("the resultant string is %s",ans);
}
else
printf("the resultant string is %s\n",ans);
}

2nd Program Output - String

#Run
enter a string
kammavari sangah is the institution name.
enter a search string
kssem

enter a replace string


KSSEM
the resultant string is kammavari sangah is the institution name.

#Run
enter a string
ks group are starting new colleges in bangalore and hyderabad
enter a search string
hyderabad

enter a replace string


sikandarabad
Dept of CS&BS,KSSEM Page 6
DATA STRUCTURES LABORATORY BCSL305
the resultant string is ks group are starting new colleges in bangalore and sikandarabad
#Run
enter a string
ks group are starting new colleges in bangalore and hyderabad
enter a search string
bangalore

enter a replace string


bengaluru and belagavi
the resultant string is ks group are starting new colleges in bengaluru and belagavi and hyderabad

Dept of CS&BS,KSSEM Page 7


DATA STRUCTURES LABORATORY BCSL305

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 5
int stack[MAX],top=-1;
char stack1[MAX],top1=-1;

void push()
{
int ele;
if(top<MAX-1)
{
printf("enter the value to be inserted into the stack:\n");
scanf("%d",&ele);
stack[++top]=ele;
}
else
printf("\n stack is full\n");
return;
}

void pop()
{
if(top!=-1)
{
printf("\n the element deleted from the stack is:%d\n",stack[top--]);
}
else
printf("\n stack is empty\n");
return;
}

void palindrome()
{
int i,count=0,len;
char str[100];
printf("\n entertring to check whether it is palindrome or not:");

scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
Dept of CS&BS, KSSEM Page 8
DATA STRUCTURES LABORATORY BCSL305
{
stack1[++top1]=str[i];
}
for(i=0;i<len;i++)
{
if(str[i]==stack1[top1--]) count++;
}
if(count==len)
printf("\n %s is a palindrome string\n",str);
else
printf("\n %s is not a palindrome string\n",str);
return;
}

void check()
{
if(top>=MAX-1)
printf("stack is overflow\n");
else if(top==-1)
printf("stack is underflow\n");
else
printf("stack operation can be performed\n");
}

void display()
{
int i;
if(top==-1)
{
printf("\n stack is empty\n");
}
else
{
printf("\n elements in the stack are\n");
for(i=top;i>=0;--i)
printf("|%d|\n",stack[i]);
}
return;
}

void main()
{
int choice;
while(1)
{
printf("\n STACK OPERATIONS\n");
printf("\n 1:push\t2:pop\t3:palindrome\t4:check\t5:display\t6:exit\n");
printf("\n enter your choice[1/2/3/4/5/6]:");
scanf("%d",&choice); switch(choice)
{
case 1: push();break;
case 2: pop();break;
case 3: palindrome();break;
case 4: check();break;
Dept of CS&BS, KSSEM Page 9
DATA STRUCTURES LABORATORY BCSL305
case 5: display();break;
case 6: exit(0);
default : printf("invaild choice\n");
}
}
return;
}

Dept of CS&BS, KSSEM Page 10


DATA STRUCTURES LABORATORY BCSL305

3rd Program Output - Stack


STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:3

entertring to check whether it is palindrome or not:mam

mam is a palindrome string

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:3

entertring to check whether it is palindrome or not:abc

abc is not a palindrome string

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
34

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
37

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
39

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
43

STACK OPERATIONS
Dept of CS&BS, KSSEM Page 11
DATA STRUCTURES LABORATORY BCSL305

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
47

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1

stack is full

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

the element deleted from the stack is:47

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

the element deleted from the stack is:43

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

the element deleted from the stack is:39

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

the element deleted from the stack is:37

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

the element deleted from the stack is:34

Dept of CS&BS, KSSEM Page 12


DATA STRUCTURES LABORATORY BCSL305
STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:2

stack is empty

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:5

stack is empty

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
34

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:1


enter the value to be inserted into the stack:
37

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:5

elements in the stack are


|37|
|34|

STACK OPERATIONS

1:push 2:pop 3:palindrome 4:check 5:display 6:exit

enter your choice[1/2/3/4/5/6]:6

Dept of CS&BS, KSSEM Page 13


DATA STRUCTURES LABORATORY BCSL305

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.

#define SIZE 50 /* Size of Stack*/


#include <ctype.h>

#include <stdio.h>
char s[SIZE];

int top = -1; /* Global declarations */

push(char elem) /* Function for PUSH operation */


{
s[++top] = elem;
}

char pop() /* Function for POP operation */


{
return (s[top--]);
}

int pr(char elem) /* Function for precedence */


{
switch (elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 3;
case '^': return 4;
}
}

void main() /* Main Program */


{
char infx[50], pofx[50], ch, elem; int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? "); scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(') push(ch);
else if (isalnum(ch)) pofx[k++] = ch;
else if (ch == ')')
Dept of CS&BS, KSSEM Page 14
DATA STRUCTURES LABORATORY BCSL305
{
while (s[top] != '(') pofx[k++] = pop();
elem = pop(); /* Remove */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch)) pofx[k++] = pop(); push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */ pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}

Dept of CS&BS, KSSEM Page 15


DATA STRUCTURES LABORATORY 18CSL38
4th Program Output – Infix to Postfix
Read the Infix Expression ? a+b/c*d^e+f

Given Infix Expn: a+b/c*d^e+f Postfix Expn: abc/de^*+f+

Read the Infix Expression ? a+b^c*(E+F)

Given Infix Expn: a+b^c*(E+F) Postfix Expn: abc^EF+*+

Read the Infix Expression ? (a*b)+c-d^e%f/h

Given Infix Expn: (a*b)+c-d^e%f/h Postfix Expn: ab*c+de^f%h/-

Read the Infix Expression ? ((a-(b+c))*d)^(e+f)

Given Infix Expn: ((a-(b+c))*d)^(e+f) Postfix Expn: abc+-d*ef+^

Dept of CS&BS, KSSEM Page 16


DATA STRUCTURES LABORATORY 18CSL38

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<ctype.h>
int top=-1;
float s[50];

float operate(float opr1,float opr2,char symbol )


{
switch(symbol)
{
case '+' :return(opr1+opr2);
case '-' :return(opr1-opr2);
case '*' :return(opr1*opr2);
case '/' :return(opr1/opr2);
case '^' :return(pow(opr1,opr2));
}
}

void push(float symbol)


{
s[++top]= symbol;
}

float pop()
{
return(s[top--]);
}

float eval(char postfix[50])


{
int i;
char symbol;
float opr1,opr2,value,res;
for(i=0;postfix[i]!='\0';i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(symbol-48);
else if(isalpha(symbol))
{
printf("\n enter the value for %c ",symbol);
scanf("%f",&value);
push(value);
}
else
{
opr2=pop();
opr1=pop();
res=operate(opr1,opr2,symbol);
push(res);
Dept of CS&BS, KSSEM Page 17
DATA STRUCTURES LABORATORY 18CSL38
}
}
return(pop());
}

void main()
{
char postfix[80];
float res;
printf("\n Enter The Postfix Expression \n");
gets(postfix);
printf("The Given Expression is : ");
puts(postfix);
res=eval(postfix);
printf("\nThe value of the expression is %f\n" ,res);
}

5a Program Output : Suffix expression

Enter The Postfix


Expression
63+
The Given Expression is :
63+
The value of the expression
is 9.000000
Enter The Postfix
Expression
abcd*+%
The Given Expression is :
abcd*+%
enter the value for a 2
enter the value for b 3
enter the value for c 4
enter the value for d 2
The value of the expression
is 2.000000

Dept of CS&BS, KSSEM Page 18


DATA STRUCTURES LABORATORY 18CSBSL3
8
5) b. Solving Tower of Hanoi problem with n disks

#include<stdio.h>
void towers(int n,char source,char dest,char auxilary);

void main()
{
int n;
printf("enter the number of disk\");
scanf("%d",&n);
towers(n,'A','C','B');
}
void towers(int n,char source,char dest,char auxilary)
{
if(n==0)
return;
towers(n-1,source,auxilary,dest);
printf("move disk %d from %c to %c \",n,source,dest);
towers(n-1,auxilary,dest,source);
}

5b Program Output - Tower of Hanoi

enter the number of disk


3
move disk 1 from A to C
move disk 2 from A to B
move disk 1 from C to B
move disk 3 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C

#Run
enter the number of disk
5
move disk 1 from A to C
move disk 2 from A to B
move disk 1 from C to B
move disk 3 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C
move disk 4 from A to B
move disk 1 from C to B
move disk 2 from C to A
move disk 1 from B to A
move disk 3 from C to B
move disk 1 from A to C
move disk 2 from A to B
Dept of CS&BS, KSSEM Page 19
DATA STRUCTURES LABORATORY 18CSBSL3
move disk 1 from C to B 8
move disk 5 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C
move disk 3 from B to A
move disk 1 from C to B
move disk 2 from C to A
move disk 1 from B to A
move disk 4 from B to C
move disk 1 from A to C
move disk 2 from A to B
move disk 1 from C to B
move disk 3 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C

Dept of CS&BS, KSSEM Page 20


DATA STRUCTURES LABORATORY 18CSL38

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 5

int CQ[size],f=-1,r=-1;
int CQfull()
{
if((f==r+1)||(f==0&&r==size-1))
return 1;
return 0;
}

int CQempty()
{
if(f==-1)
return 1;
return 0;
}

CQinsert(int ele)
{
if(CQfull())
printf("\n overflow!!!\n");
else
{
if(f==-1)
f=0;
r=(r+1)%size;
CQ[r]=ele;
}
}

int CQdelete()
{
int ele;
if(CQempty())
{
printf("\n underflow!!!\n");
return(-1);
}
else
{
ele=CQ[f];
Dept of CS&BS, KSSEM Page 21
DATA STRUCTURES LABORATORY 18CSL38
if(f==r)
{
f=-1;
r=-1;
}
else
f=(f+1)%size;
return(ele);
}
}

display()
{
int i;
if(CQempty())
printf("\n empty queue\n");
else
{
printf("front[%d]->",f);
for(i=f;i!=r;i=(i+1)%size)
printf(" %d ",CQ[i]);
printf(" %d ",CQ[i]);
printf("<-[%d]rear",r);
}
}
void main()
{
int opn,ele;
do
{
printf("\n \n Circular queue operations\n");
printf("\n press 1-insert,2-delete,3-display,4-exit\n"); printf("\n your option? ");
scanf("%d",&opn); switch(opn)
{
case 1:printf("\n read the element to be inserted ?\n");
scanf("%d",&ele);
CQinsert(ele);
break;
case 2:ele=CQdelete();
if(ele!=-1)
printf("\n delete element is %d\n",ele); break;
case 3:printf("\n status of circular queue \n\n");
display();
break;
case 4:exit(0);
default:printf("invalid option\n");
}
}
while(opn!=4);
}

Dept of CS&BS, KSSEM Page 22


DATA STRUCTURES LABORATORY 18CSL38

6th Program Output - Circular QUEUE

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 3

status of circular queue

empty queue

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


34

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


37

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


39

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


43

Circular queue operations

Dept of CS&BS, KSSEM Page 23


DATA STRUCTURES LABORATORY 18CSL38
press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


47

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 1

read the element to be inserted ?


49

overflow!!!

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 3

status of circular queue

front[0]-> 34 37 39 43 47 <-[4]rear

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 2

delete element is 34

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 2

delete element is 37

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 2

delete element is 39

Circular queue operations

Dept of CS&BS, KSSEM Page 24


DATA STRUCTURES LABORATORY 18CSL38
press 1-insert,2-delete,3-display,4-exit

your option? 2

delete element is 43

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 2

delete element is 47

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 2

underflow!!!

Circular queue operations

press 1-insert,2-delete,3-display,4-exit

your option? 4

Dept of CS&BS, KSSEM Page 25


DATA STRUCTURES LABORATORY 18CSL38

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<string.h>

typedef struct stud


{
char usn[15],name[25],branch[30],phone[10];
int sem;
struct stud *next;
}
node;

node *start=NULL;

node* getnode()
{
node *new1;
new1=(node *)malloc(sizeof(node));
printf("Enter the student \nUsn \nName \nBranch \nSemester \nPhone\n");
scanf("%s %s %s %d %s",new1->usn,new1->name,new1->branch,&new1->sem,new1-
>phone); new1->next=NULL;
return(new1);
}

void display()
{
node *temp=start;
int count=0;
if(start==NULL)
{
printf("List Empty\n");
return;
}
printf("\n Usn:\t Name:\t Branch:\t Sem: \tPhone: \n");
while(temp!=NULL)
{
printf("%s\t%s\t%s\t\t%d\t%s\n",temp->usn,temp->name,temp->
branch,temp->sem,temp->phone);
temp=temp->next;
count++;

}
printf("The number of nodes are:%d\n ",count);
}

Dept of CS&BS, KSSEM Page 26


DATA STRUCTURES LABORATORY 18CSL38
void insert_front()
{
node *new1;
new1=getnode();
if(start==NULL)
{
start=new1;
return;
}
new1->next=start;
start=new1;
}

void createSLL()
{
int n,i=0;
printf("Enter the number of students to be created\
n"); scanf("%d",&n);
while(i<n)
{ i++;
insert_front(); // or call insert_end();
}
return;
}

void insert_end()
{
node *new1,*temp=start;
new1=getnode();
if(start==NULL)
{
start=new1;
return;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new1;
}

void del_front()
{
node *temp;
if(start==NULL)
{
printf("List is empty\
n"); return;
}
temp=start;
start=start->next;
printf("The node of student usn %s is deleted\n",temp->usn);

Dept of CS&BS, KSSEM Page 27


DATA STRUCTURES LABORATORY 18CSL38
free(temp);
}

void del_end()
{
node *cur,*pre;
if(start==NULL)
{
printf("List is empty\
n"); return;
}

cur=start;
pre=NULL;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
if(pre==NULL)
start=NULL;
else
pre->next=NULL;
printf("The node of student usn %s is deleted\n",cur->usn);
free(cur);
}
void main()
{
int
ch; for(;;)
{
printf("\n 1: Create SLL \n 2. Insert Front \n 3: Insert End \n 4: Delete Front \n 5: Delete End \n
6:Display\n 7: Exit \n");
printf("Enter you are
choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1: createSLL();break;
case 2:insert_front();break;
case 3: insert_end(); break;
case 4: del_front();break;
case 5: del_end();break;
case 6: display();break;
case 7: exit(0);
default : printf("Invalid option\
n"); exit(0);
}
}
}

Dept of CS&BS, KSSEM Page 28


DATA STRUCTURES LABORATORY 18CSL38

7th Program Output - Singly Linked List (SLL)

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
List Empty

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
1
Enter the number of students to be created
3
Enter the student
Usn
Name
Branch
Semester
Phone
43
Rama
CS&BS
3
66221234
Enter the student
Usn
Name
Branch
Semester
Phone
45
Raj
CS&BS
3
66221234
Enter the student
Usn
Name
Branch
Semester
Dept of CS&BS, KSSEM Page 29
DATA STRUCTURES LABORATORY 18CSL38
Phone
46
Prince
CS&BS
3
66221234

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6

Usn: Name: Branch: Sem: Phone:


46 Prince CS&BS 3 66221234
45 Raj CS&BS 3 66221234
43 Rama CS&BS 3 66221234
The number of nodes are:3

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
3
Enter the student
Usn
Name
Branch
Semester
Phone
42
Ved
CS&BS
3
66221234

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice

Dept of CS&BS, KSSEM Page 30


DATA STRUCTURES LABORATORY 18CSL38
6

Usn: Name: Branch: Sem: Phone:


46 Prince CS&BS 3 66221234
45 Raj CS&BS 3 66221234
43 Rama CS&BS 3 66221234
42 Ved CS&BS 3 66221234
The number of nodes are:4

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
2
Enter the student
Usn
Name
Branch
Semester
Phone
47
Paru
CS&BS
3
66221234

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6

Usn: Name: Branch: Sem: Phone:


47 Paru CS&BS 3 66221234
46 Prince CS&BS 3 66221234
45 Raj CS&BS 3 66221234
43 Rama CS&BS 3 66221234
42 Ved CS&BS 3 66221234
The number of nodes are:5

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front

Dept of CS&BS, KSSEM Page 31


DATA STRUCTURES LABORATORY 18CSL38
5: Delete End
6:Display
7: Exit
Enter you are choice
5
The node of student usn 42 is deleted

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6

Usn: Name: Branch: Sem: Phone:


47 Paru CS&BS 3 66221234
46 Prince CS&BS 3 66221234
45 Raj CS&BS 3 66221234
43 Rama CS&BS 3 66221234
The number of nodes are:4

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
4
The node of student usn 47 is deleted

1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6

Usn: Name: Branch: Sem: Phone:


46 Prince CS&BS 3 66221234
45 Raj CS&BS 3 66221234
43 Rama CS&BS 3 66221234
The number of nodes are:3

1: Create SLL

Dept of CS&BS, KSSEM Page 32


DATA STRUCTURES LABORATORY 18CSL38
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
7

Dept of CS&BS, KSSEM Page 33


DATA STRUCTURES LABORATORY 18CSL38

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<stdlib.h>

struct node
{
char ssn[15],name[15],dept[15],desg[15],phno[15];
long int sal;
struct node *llink,*rlink;
};

typedef struct node *NODE;

NODE temp, first=NULL, end=NULL;

NODE getnode(void)
{
NODE x=(NODE)malloc(sizeof(struct node));
return x;
}

void read(void)
{
temp=getnode();
temp->llink=temp->rlink=NULL;
printf("Enter SSN:");
scanf("%s",temp->ssn);
printf("Enter Name:");
scanf("%s",temp->name);
printf("Enter Deptartment:");
scanf("%s",temp->dept);
printf("Enter Designation:");
scanf("%s",temp->desg);
printf("Enter PhoneNo:");
scanf("%s",temp->phno);
printf("Enter Salary:");
scanf("%ld",&temp->sal);
}

Dept of CS&BS, KSSEM Page 34


DATA STRUCTURES LABORATORY 18CSL38
void create_dll(void)
{
int n,i=0;
printf("Enter the number of Employees:");
scanf("%d",&n);
while(i!=n)
{
i++;
printf("Enter details\n");
read();
if(first==NULL)
first=end=temp;
else
{
end->rlink=temp;
temp->llink=end;
end=temp;
}
}
printf("[+] DONE\n");
}

void displaycount(void)
{
NODE temp1=first;
int count=1;
if(temp1==NULL)
printf("[!] List is empty\n");
else
{
printf("SSN\tName\tDEPT\tDESIG\tPhone\tSalary\n");
printf(" -\t- \t -\t-\t \t \n");
while(temp1!=end)
{
count++;
printf("%s\t%s\t%s\t%s\t%s\t%ld\n",temp1->ssn,temp1->name, temp1->dept,temp1->desg,temp1->phno,
temp1->sal);
temp1=temp1->rlink;
}
printf("%s\t%s\t%s\t%s\t%s\t%ld\n",temp1->ssn,temp1->name,temp1->dept,temp1->desg,temp1->phno,
temp1->sal);
printf("\nCount:%d\n",count);
}
}

void insertfront(void)
{
printf("Enter Details\n");
Dept of CS&BS, KSSEM Page 35
DATA STRUCTURES LABORATORY 18CSL38
read();
if(first==NULL)
first=temp;
else
{
temp->rlink = first;
first->llink = temp;
first=temp;
}
}

void insertend(void)
{
printf("Enter Details\n");
temp=getnode();
temp->llink = temp->rlink = NULL;
read();
if(first==NULL)
first = end = temp;
else
{
end->rlink = temp;
temp->llink = end;
end = temp;
}
}

void deletefront(void)
{
NODE temp2;
if(first==NULL)
printf("[!] List is empty\n");
else if(first == end)
{
temp2 = first;
printf("[+] Deleted record %s\n",temp2->ssn);
first=end=NULL;
}
else
{
temp2= first;
printf("[+] Deleted record %s\n",temp2->ssn);
first = first->rlink;
temp2->llink = NULL;
free(temp2);
}
}

Dept of CS&BS, KSSEM Page 36


DATA STRUCTURES LABORATORY 18CSL38
void deleteend(void)
{
NODE temp2 = end;
if(temp2 == NULL)
printf("[!] List is empty\n");
else if(first==end)
{
printf("[+] Deleted record %s\n", temp2->ssn);
first = end =NULL;
}
else
{
printf("[+] Deleted record %s\n", temp2->ssn);
end=end->llink;
end->rlink=NULL;
free(temp2);
}
}

void main(void)
{
int ch;
while(1)
{
printf("\nMENU\n \n[1] Create list\n[2] Display list\n[3] Insert front\n[4] Insert end\n[5] Deleted front\n[6]
Delete rear\n[7] Exit\n");
printf("\nEnter the choice: ");
scanf("%d",&ch); switch(ch)
{
case 1: create_dll();break;
case 2: displaycount(); break;
case 3: insertfront();break;
case 4: insertend();break;
case 5: deletefront(); break;
case 6: deleteend();break;
case 7: exit(0);
default:printf("invalid choice\n");
}
}
}

8th Program Output – Doubly Linked List

Dept of CS&BS, KSSEM Page 37


DATA STRUCTURES LABORATORY 18CSL38

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 5


[!] List is empty

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 6


[!] List is empty

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 1


Enter the number of Employees: 3
Enter details
Enter SSN:34
Enter Name:rakesh
Enter Deptartment:cs&bs
Enter Designation:developer
Enter PhoneNo:66221234
Enter Salary:12345
Enter details
Enter SSN:35
Enter Name:sakshi
Enter Deptartment:cs&bs
Enter Designation:developer
Enter PhoneNo:66221234
Enter Salary:23456
Dept of CS&BS, KSSEM Page 38
DATA STRUCTURES LABORATORY 18CSL38
Enter details
Enter SSN:37
Enter Name:yash
Enter Deptartment:cs&bs
Enter Designation:developer
Enter PhoneNo:66221234
Enter Salary:21345
[+] DONE

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 2


SSN Name DEPT DESIG Phone Salary
- - - -
34 rakesh cs&bs developer 66221234 12345
35 sakshi cs&bs developer 66221234 23456
37 yash cs&bs developer 66221234 21345

Count:3

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 6


[+] Deleted record 37

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 2


SSN Name DEPT DESIG Phone Salary
- - - -
Dept of CS&BS, KSSEM Page 39
DATA STRUCTURES LABORATORY 18CSL38
34 rakesh cs&bs developer 66221234 12345
35 sakshi cs&bs developer 66221234 23456

Count:2

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 5


[+] Deleted record 34

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 2


SSN Name DEPT DESIG Phone Salary
- - - -
35 sakshi cs&bs developer 66221234 23456

Count:1

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 3


Enter Details
Enter SSN:36
Enter Name:Paru
Enter Deptartment:cs&bs
Enter Designation:developer
Enter PhoneNo:66221234
Enter Salary:21356

Dept of CS&BS, KSSEM Page 40


DATA STRUCTURES LABORATORY 18CSL38
MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 2


SSN Name DEPT DESIG Phone Salary
- - - -
36 Paru cs&bs developer 66221234 21356
35 sakshi cs&bs developer 66221234 23456

Count:2

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 4


Enter Details
Enter SSN:33
Enter Name:josh
Enter Deptartment:cs&bs
Enter Designation:developer
Enter PhoneNo:66221234
Enter Salary:12457

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 2


SSN Name DEPT DESIG Phone Salary
- - - -
36 Paru cs&bs developer 66221234 21356
35 sakshi cs&bs developer 66221234 23456
33 josh cs&bs developer 66221234 12457

Dept of CS&BS, KSSEM Page 41


DATA STRUCTURES LABORATORY 18CSL38
Count:3

MENU

[1] Create list


[2] Display list
[3] Insert front
[4] Insert end
[5] Deleted front
[6] Delete rear
[7] Exit

Enter the choice: 7

Dept of CS&BS, KSSEM Page 42


DATA STRUCTURES LABORATORY 18CSL38
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

#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.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)

Dept of CS&BS, KSSEM Page 43


DATA STRUCTURES LABORATORY 18CSL38
{
NODE temp;
if(head->link==head)
{
printf("polynomial doesn't exist\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;
Dept of CS&BS, KSSEM Page 44
DATA STRUCTURES LABORATORY 18CSL38
}
return h3;
}

NODE read_poly(NODE head)


{
int i;
int px,py,pz;
float cf;
printf("enter the coefficient as -999 to end the polynomial\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 polynomial\n");


h1=read_poly(h1);
printf("enter the second polynomial\n");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf("the first polynomial is\n");
display(h1);
printf("secomd polynomail is\n");
display(h2);
printf("the sum of two polynomial is\n");
display(h3);
}

void represent_evaluate()
{
NODE e1,temp;
Dept of CS&BS, KSSEM Page 45
DATA STRUCTURES LABORATORY 18CSL38
int x,y,z;
float sum=0.0;
e1=getnode();
e1->link=e1;
printf("enter the polynomail\n");
e1=read_poly(e1);
printf("polynomial is\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;
}
printf("the total sum is %f\n",sum);
}
return;
}

void main()
{
int choice;
while(1)
{

printf("\n\n\n\t1.represent & evaluate...\t2.add two poly...\t3.exit...");


printf("\n\n\n\t enter 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\t enter proper choice..");
}
}
}

Dept of CS&BS, KSSEM Page 46


DATA STRUCTURES LABORATORY 18CSL38

9th Program output: Singly Circular Linked List (SCLL) Polynomials

#Run 1

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:1


enter the polynomail
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=4
pow x=3
pow y=2
pow z=1
enter the 2 term
coeff=4
pow x=2
pow y=3
pow z=2
enter the 3 term
coeff=-9
pow x=2
pow y=3
pow z=5
enter the 4 term
coeff=2
pow x=2
pow y=4
pow z=1
enter the 5 term
coeff=-3
pow x=3
pow y=3
pow z=2
enter the 6 term
coeff=-999
polynomial is
4.00x^3y^2z^1+ 4.00x^2y^3z^2+ -9.00x^2y^3z^5+ 2.00x^2y^4z^1+ -3.00x^3y^3z^2+
enter the values of coefficient
3
2
1
the total sum is -288.000000

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:1


enter the polynomail
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=4
pow x=3
Dept of CS&BS, KSSEM Page No.47
DATA STRUCTURES LABORATORY 18CSL38
pow y=2
pow z=1
enter the 2 term
coeff=4
pow x=2
pow y=3
pow z=2
enter the 3 term
coeff=-2
pow x=2
pow y=3
pow z=5
enter the 4 term
coeff=2
pow x=2
pow y=4
pow z=1
enter the 5 term
coeff=-1
pow x=3
pow y=3
pow z=2
enter the 6 term
coeff=-999
polynomial is
4.00x^3y^2z^1+ 4.00x^2y^3z^2+ -2.00x^2y^3z^5+ 2.00x^2y^4z^1+ -1.00x^3y^3z^2+
enter the values of coefficient
3
2
1
the total sum is 648.000000

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:2


enter the first polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=3
pow z=1
enter the 2 term
coeff=-999
enter the second polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=3
pow z=1
enter the 2 term
coeff=-999
the first polynomial is
3.00x^2y^3z^1+
Dept of CS&BS, KSSEM Page No.48
DATA STRUCTURES LABORATORY 18CSL38
secomd polynomail is
3.00x^2y^3z^1+
the sum of two polynomial is
6.00x^2y^3z^1+

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:3

#Run

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:1


enter the polynomail
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=6
pow x=2
pow y=2
pow z=1
enter the 2 term
coeff=-4
pow x=0
pow y=1
pow z=5
enter the 3 term
coeff=3
pow x=3
pow y=1
pow z=1
enter the 4 term
coeff=2
pow x=1
pow y=5
pow z=1
enter the 5 term
coeff=-2
pow x=1
pow y=1
pow z=3
enter the 6 term
coeff=-999
polynomial is
6.00x^2y^2z^1+ -4.00x^0y^1z^5+ 3.00x^3y^1z^1+ 2.00x^1y^5z^1+ -2.00x^1y^1z^3+
enter the values of coefficient
3
2
1
the total sum is 550.000000

1.represent & evaluate... 2.add two poly... 3.exit...

Dept of CS&BS, KSSEM Page No.49


DATA STRUCTURES LABORATORY 18CSL38
enter your choice:2
enter the first polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=1
pow z=1
enter the 2 term
coeff=4
pow x=1
pow y=2
pow z=1
enter the 3 term
coeff=-999
enter the second polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=1
pow z=1
enter the 2 term
coeff=4
pow x=1
pow y=2
pow z=1
enter the 3 term
coeff=-999
the first polynomial is
3.00x^2y^1z^1+ 4.00x^1y^2z^1+
secomd polynomail is
3.00x^2y^1z^1+ 4.00x^1y^2z^1+
the sum of two polynomial is
6.00x^2y^1z^1+ 8.00x^1y^2z^1+

1.represent & evaluate... 2.add two poly... 3.exit...

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:2


enter the first polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=3
pow z=1
enter the 2 term
coeff=4
pow x=3

Dept of CS&BS, KSSEM Page No.50


DATA STRUCTURES LABORATORY 18CSL38
pow y=2
pow z=1
enter the 3 term
coeff=-999
enter the second polynomial
enter the coefficient as -999 to end the polynomial
enter the 1 term
coeff=3
pow x=2
pow y=3
pow z=1
enter the 2 term
coeff=4
pow x=1
pow y=1
pow z=1
enter the 3 term
coeff=-999
the first polynomial is
3.00x^2y^3z^1+ 4.00x^3y^2z^1+
secomd polynomail is
3.00x^2y^3z^1+ 4.00x^1y^1z^1+
the sum of two polynomial is
6.00x^2y^3z^1+ 4.00x^3y^2z^1+ 4.00x^1y^1z^1+

1.represent & evaluate... 2.add two poly... 3.exit...

enter your choice:3

Dept of CS&BS, KSSEM Page No.51


DATA STRUCTURES LABORATORY 18CSL38

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>
#define MAX 10
struct node
{
int info;
struct node *llink;
struct node *rlink;
};

typedef struct node


*NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of mem");exit(0);

}
return x;
}

NODE insert(int item,NODE root)


{
NODE temp,cur,prev;
temp=getnode();
temp->info=item;
temp->llink=temp->rlink=NULL;
if(root==NULL)
{
root=temp;return root;

}
else
{
prev=NULL;
cur=root;

while(cur!=NULL)
{
prev=cur;
cur=(temp->info<cur->info)?cur->llink:cur->rlink;
}
Dept of CS&BS, KSSEM Page No.52
DATA STRUCTURES LABORATORY 18CSL38
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
}
void pre(NODE PRE)
{
if(PRE!=NULL)
{
printf("%d\t",PRE->info);pre(PRE->llink); pre(PRE->rlink);

}
return;
}
void in(NODE IN)
{
if(IN!=NULL)
{
in(IN->llink); printf("%d\t",IN->info);in(IN->rlink);

}
return;
}
void post(NODE POST)
{
if(POST!=NULL)
{
post(POST->llink);
post(POST->rlink);
printf("%d\t",POST->info);
}
return;
}
void traversal(NODE root)
{
NODE IN,PRE,POST;
IN=root;
PRE=root;
POST=root;
if(root==NULL)
{
printf("emty tree");
return;
}
printf("preorder traversal:
"); pre(PRE);
printf("\ninorder traversal: ");

Dept of CS&BS, KSSEM Page No.53


DATA STRUCTURES LABORATORY 18CSL38
in(IN);
printf("\npost order traversal:
"); post(POST);
return;
}
void search(NODE root)
{
int item,flag=0;
NODE cur;
printf("enter element to be searched:
"); scanf("%d",&item);
if(root==NULL)
{
printf("empty ree");
return;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->info)
flag=1;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(flag==1)
printf("key found\n");
else
printf("key not found\n");
return;
}
void main()
{
int choice,item;
NODE root=NULL;
while(1)
{
printf("\n1 create bst\n2 traverse\n3 search\n4
exit"); printf("\nEnter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter item to be inserted: ");
scanf("%d",&item);
root=insert(item,root);break;
case 2: traversal(root);break;
case 3: search(root);break;
case 4: exit(0);
default: printf("enter proper choice");
}
}
}

Dept of CS&BS, KSSEM Page No.54


DATA STRUCTURES LABORATORY 18CSL38

10th Program Output - Binary Search Tree (BST)

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 2

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 8

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 15

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 24

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 5

Dept of CS&BS, KSSEM Page No.55


DATA STRUCTURES LABORATORY 18CSL38
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 2

1 create bst
2 traverse
3 search
4 exit
Enter choice: 2
preorder traversal: 2 8 5 2 15 24
inorder traversal: 2 2 5 8 15 24
post order traversal: 2 5 24 15 8 2
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 14

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 7

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 8

1 create bst
2 traverse
Dept of CS&BS, KSSEM Page No.56
DATA STRUCTURES LABORATORY 18CSL38
3 search
4 exit
Enter choice: 2
preorder traversal: 2 8 5 2 7 15 14 8 24
inorder traversal: 2 2 5 7 8 8 14 15 24
post order traversal: 2 7 5 8 14 24 15 8 2
1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 24
key found

1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 3
key not found

1 create bst
2 traverse
3 search
4 exit
Enter choice: 4

#Run

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 34

Dept of CS&BS, KSSEM Page No.57


DATA STRUCTURES LABORATORY 18CSL38
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 9

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 15

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 8

1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 5

1 create bst
2 traverse
3 search
4 exit
Enter choice: 2
preorder traversal: 34 9 8 5 15
inorder traversal: 5 8 9 15 34
post order traversal: 5 8 15 9 34
1 create bst
2 traverse
Dept of CS&BS, KSSEM Page No.58
DATA STRUCTURES LABORATORY 18CSL38
3 search
4 exit
Enter choice: 3
enter element to be searched: 15
key found

1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 2
key not found

1 create bst
2 traverse
3 search
4 exit
Enter choice: 4

Dept of CS&BS, KSSEM Page No.59


DATA STRUCTURES LABORATORY 18CSL38

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>
#include<math.h>
#include<stdlib.h>
#define MAX 20
int G[MAX][MAX], n,visited[10];

void Creation_Graph()
{
int i,j;
printf("Enter the Number of Cities\
n"); scanf("%d", &n);
printf("Enter the edges for the Cities\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&G[i][j]);
}
}
printf("edges 0f the Cities are\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",G[i][j]);
}
printf("\n");
}
printf("Adjacency matrix for the Cities is created succesfully\n");
}
void dfs(int source)
{
int v;
visited[source]=1;
for(v=0;v<n;v++)
if(G[source][v]==1&&visited[v]==0)
{
dfs(v);
}
return;
}

void reachabilitycheck( int source)


{
int i;
for(i=0;i<n;i++)
visited[i]=0;
dfs(source);
Dept of CS&BS, KSSEM Page No.60
DATA STRUCTURES LABORATORY 18CSL38
for(i=0;i<n;i++)
{
if(visited[i]==1)
printf("vertex %d is reachable\n",i);
else
printf("vertex %d is not reachable\n",i);
}
}

void main()
{
int choice,source;
while(1)
{
printf("\n\n\n\t1.create a graph .\t2.Test for connectivity......\t4.Exit...");
printf("\n\n\n\tEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:Creation_Graph();break;

case 2: printf(" enter the source between 0 to %d\n",n-1 );


scanf("%d",&source);
printf("source is %d\n",source);
reachabilitycheck(source);break;

case 3:exit(0);

default: printf("\n\n\n\tEnter proper Choice. . .");


}
}
}

11th Program Output - Graph(G)

Dept of CS&BS, KSSEM Page No.61


DATA STRUCTURES LABORATORY 18CSL38

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 1


Enter the Number of Cities
4
Enter the edges for the Cities
0
1
1
0
0
0
0
1
0
0
0
1
0
0
0
0
edges 0f the Cities are
0 1 1 0
0 0 0 1
0 0 0 1
0 0 0 0
Adjacency matrix for the Cities is created succesfully

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 2


enter the source between 0 to 3
0
source is 0
vertex 0 is reachable
vertex 1 is reachable
vertex 2 is reachable
vertex 3 is reachable

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 2


enter the source between 0 to 3
2
source is 2
Dept of CS&BS, KSSEM Page No.62
DATA STRUCTURES LABORATORY 18CSL38
vertex 0 is not reachable
vertex 1 is not reachable
vertex 2 is reachable
vertex 3 is reachable

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 2


enter the source between 0 to 3
3
source is 3
vertex 0 is not reachable
vertex 1 is not reachable
vertex 2 is not reachable
vertex 3 is reachable

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 2


enter the source between 0 to 3
1
source is 1
vertex 0 is not reachable
vertex 1 is reachable
vertex 2 is not reachable
vertex 3 is reachable

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 2


enter the source between 0 to 3
2
source is 2
vertex 0 is not reachable
vertex 1 is not reachable
vertex 2 is reachable
vertex 3 is reachable

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 4

Enter proper Choice

1.create a graph . 2.Test for connectivity...... 4.Exit...

Enter Your Choice: 3

Dept of CS&BS, KSSEM Page No.63


DATA STRUCTURES LABORATORY 18CSL38
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 ofmemory 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
int hash (int key);
struct employee
{
int emp_id, emp_age;
char emp_name[25];
}emp[MAX_ADDR];

void main()
{
int i, ch, count = 0, index, haddr, id ;
for(;;)
{
printf("Enter 1 to insert record \n 2. to display record\n");
scanf("%d", &ch);
switch(ch)
{
case 1: if(count == MAX_ADDR)
{
printf("No free addres space\
n"); break;
}
printf("Enter employee id\
n"); scanf("%d", &id);
haddr = hash(id);
printf("Home 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\
n"); scanf("%s",
emp[index].emp_name);
printf("Enter the employee age\n");
scanf("%d", &emp[index].emp_age);

count++;
printf("Successfuly inserted at Actual Address: %d\n\n", index);
break;
}
}
break;
Dept of CS&BS, KSSEM Page No.64
DATA STRUCTURES LABORATORY 18CSL38
case 2: printf("?Employee details are\n");

Dept of CS&BS, KSSEM Page No.65


DATA STRUCTURES LABORATORY 18CSL38
for(index=0; index<MAX_ADDR; index++)
{
if(emp[index].emp_id !=NULL)
{
printf("Employee id is %d\t", emp[index].emp_id);
printf("Employee name is %s\t",emp[index].emp_name);
printf("Employee age is %d\n", emp[index].emp_age);
}
}
break;
default: exit(0);
}
}
}

Dept of CS&BS, KSSEM Page No.66


DATA STRUCTURES LABORATORY 18CSL38
int hash (int key)
{
return key % MAX_ADDR;
}

12th Program Output - Hash function H: K ->L

Enter 1 to insert record


2. to display record
1
Enter employee id
1023
Home address is 3
Enter the employee name
Rama
Enter the employee age
21
Successfuly inserted at Actual Address: 3

Enter 1 to insert record


2. to display record
2
Employee details are
Employee id is 1023 Employee name is Rama Employee age is 21
Enter 1 to insert record
2. to display record
1
Enter employee id
1024
Home address is 4
Enter the employee name
Raj
Enter the employee age
20
Successfuly inserted at Actual Address: 4

Enter 1 to insert record


2. to display record
2
Employee details are
Employee id is 1023 Employee name is Rama Employee age is 21
Employee id is 1024 Employee name is Raj Employee age is 20
Enter 1 to insert record
2. to display record
1
Enter employee id
1026
Home address is 1
Dept of CS&BS, KSSEM Page No.67
DATA STRUCTURES LABORATORY 18CSL38
Enter the employee name
Max
Enter the employee age
21
Successfuly inserted at Actual Address: 1

Enter 1 to insert record


2. to display record
2
Employee details are
Employee id is 1026 Employee name is Max Employee age is 21
Employee id is 1023 Employee name is Rama Employee age is 21
Employee id is 1024 Employee name is Raj Employee age is 20
Enter 1 to insert record
2. to display record
1
Enter employee id
1022
Home address is 2
Enter the employee name
Depu
Enter the employee age
20
Successfuly inserted at Actual Address: 2

Enter 1 to insert record


2. to display record
2
Employee details are
Employee id is 1026 Employee name is Max Employee age is 21
Employee id is 1022 Employee name is Depu Employee age is 20
Employee id is 1023 Employee name is Rama Employee age is 21
Employee id is 1024 Employee name is Raj Employee age is 20
Enter 1 to insert record
2. to display record
1
Enter employee id
1025
Home address is 0
Enter the employee name
Virat
Enter the employee age
21
Successfuly inserted at Actual Address: 0

Enter 1 to insert record


2. to display record
2
Employee details are
Employee id is 1025 Employee name is Virat Employee age is 21
Dept of CS&BS, KSSEM Page No.68
DATA STRUCTURES LABORATORY 18CSL38
Employee id is 1026 Employee name is Max Employee age is 21
Employee id is 1022 Employee name is Depu Employee age is 20
Employee id is 1023 Employee name is Rama Employee age is 21
Employee id is 1024 Employee name is Raj Employee age is 20
Enter 1 to insert record
2. to display record
1
No free addres space
Enter 1 to insert record
2. to display record
3

Dept of CS&BS, KSSEM Page No.69


DATA STRUCTURES LABORATORY 18CSBSL38

VIVA QUESTIONS

1. What is data structure and explain its types as well as its operations?
It is a way of organizing data that considers not only the items stored, but also their relationship to each
other. General data structure types include the array, the file, the record, the table, the tree, and so on. Any data
structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in
appropriate ways. Data Structure is also a way of collecting and organizing data in such a way that we can
perform operations on these data in an effective way.

Linear data structure: a data structure which is sequential and continues in nature i.e. access the data in
sequential manner. Linear data structures are: array, linked list, stack and queue. Non-linear data structure: a data
structure which is not sequential. Non-linear data structures are: tree and graph.

The data in the data structures are processed by certain operations. The particular data structure chosen
largely depends on the frequency of the operation that needs to be performed on the data structure. Basic
Operations: Traversing, Searching, Insertion, Deletion, Sorting, Merging

2. List out the areas in which data structures are applied extensively?

(i) Compiler Design,


(ii) Operating System,
(iii) Database Management System,
(iv) Statistical analysis package,
(v) Numerical Analysis,
(vi) Graphics,
(vii) Artificial Intelligence,
(viii) Simulation

3. What are the major data structures used in the following areas :

RDBMS, Network data model & Hierarchical data model.


(i) RDBMS – Array (i.e. Array of structures)
(ii) Network data model – Graph
(iii) Hierarchical data model – Trees

4. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?
The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect
them. As ordinary pointers cannot be used for this. So we go for void pointer. Void pointer is capable of storing
pointer to any type as it is a generic pointer type.

5. Minimum number of queues needed to implement the priority queue?


Two. One queue is used for actual storing of data and another for storing priorities.

6. What is the data structures used to perform recursion?


Stack. Because of its LIFO (Last In First Out) property as it remembers its ‘caller’ so knows whom to return
when the function has to return. Recursion makes use of system stack for storing the return addresses of the
function calls.
Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative
procedures are written, explicit stack is to be used.

Dept of CS&BS, KSSEM Page No.70


DATA STRUCTURES LABORATORY 18CSL38

7. What are the notations used in Evaluation of Arithmetic Expressions


using prefix and postfix forms?
Polish and Reverse Polish notations.

8. Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix


notations. Prefix Notation: ^ - * +ABC - DE + FG
Postfix Notation: AB + C * DE - - FG + ^

9. Sorting is not possible by using which of the following methods?


(a) Insertion (b) Selection (c) Exchange (d) Deletion

(d) Deletion.
Using insertion we can perform insertion sort, using selection we can perform selection sort, using exchange we
can perform the bubble sort (sorting methods). But no sorting method can be done just using deletion.

10.A binary tree with 20 nodes has null


branches? 21 null branches
Let us take a tree with 5 nodes (n=5)
It will have only 6 (ie,5+1) null branches. In general,
A binary tree with n nodes has exactly n+1 null
nodes.

11. What are the methods available in storing sequential files ?


(i) Straight merging,
(ii) Natural merging,
(iii) Polyphase sort,
(iv) Distribution of Initial runs.

12. How many different trees are possible with 10 nodes ?


10 different Trees. If there are n nodes, there exist 2n-n different trees.

13. List out few of the Application of tree data-structure?


(i) The manipulation of Arithmetic expression,
(ii) Symbol Table construction,
(iii) Syntax analysis.

14. List out few of the applications that make use of Multilinked Structures?
(i) Sparse matrix (ii) Index generation.

15. In tree construction which is the suitable efficient data structure?


(a) Array (b) Linked list (c) Stack (d) Queue (e) none

(b) Linked list

16. What is the type of the algorithm used in solving the 8 Queens
problem? Backtracking and Branch Bound.

17. In an AVL tree, at what condition the balancing is to be done?


If the ‘pivotal value’ or the ‘Height factor’ is greater than 1 or less than –1.

18. What is the bucket size, when the overlapping and collision occur at same time?
One. If there is only one entry possible in the bucket, when the collision occurs, there is no way to
accommodate the colliding value. This results in the overlapping of values.

Dept of CS&BS, KSSEM Page No.71


DATA STRUCTURES LABORATORY 18CSL38

19 .What is a Register?
A register is a small amount of memory within the CPU that is used to temporarily store instructions and data.

20. An ABSTRACT data type is a keyword of a programming language that specifies the amount of memory
needed to store data and the kind of data that will be stored in that memory location?

21. What are the different Abstract Data Type Groups?


Integer, Floating-Type, Character & Boolean are the four different data type groups

22.Which of the following abstract data types are NOT used by Integer
Abstract Data type group?
(i) short
(ii) int
(iii) float
(iv) long

23 .There are 8, 15, 13, and 14 nodes in four different trees. Which one of
them can form a full binary tree?
The answer is the tree with 15 nodes. In general, there are 2^n-1 nodes in a full
binary tree.

24. What is Strictly Binary Tree?


A tree which has exactly Two or Zero child is called Strictly Binary Tree.

25. Which are the Different Types of Tree Traversing Techniques?


(i) Preorder Traversing
(ii) Inorder Traversing
(iii) Postorder TraversingViva Questions:

26. Hash Table(HT) linear probing: In hash table, the data is stored in an array format where each data value has
its own unique index value. Access of data becomes very fast, if we know the index of the desired data.

Hashing : Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going
to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the
following items are to be stored. Item are in the (key,value) format.

Linear Probing : As we can see, it may happen that the hashing technique is used to create an already used index
of the array. In such a case, we can search the next empty location in the array by looking into the next cell until
we find an empty cell. This technique is called linear probing.

Dept of CS&BS, KSSEM Page No.72


DATA STRUCTURES LABORATORY 18CSL38

27. Difference between array and structure?

Dept of CS&BS, KSSEM Page No.73

You might also like