0% found this document useful (0 votes)
66 views78 pages

DS Manual 22scheme BCSL305

Uploaded by

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

DS Manual 22scheme BCSL305

Uploaded by

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

Kammavari Sangham(R) 1952,

K. S. Group of Institutions
K. S. SCHOOL OF ENGINEERING & MANAGEMENT
Mallasandra, Off Kanakapura Road, Bengaluru - 560 109

Department of
Computer Science & Business Systems

Data Structures Laboratory


(BCSL305)
LABORATORY MANUAL
K.S SCHOOL OF ENGINEERING & MANAGEMENT,
BANGALORE - 109

“DATA STRUCTURES LABORATORY”


Subject code: BCSL305

Prepared by: -

Mrs. Jayashree Krishna Murthy Mr. Prashanth Koparde


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,


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

Department Vision & Mission


VISION: “To provide competent learning ecosystem to develop the understanding
of technology and business to produce innovative, principled and insightful leaders
to meet the societal demands.”

MISSION:
⚫ To deliver high-quality education in the fields of technology and business
through effective teaching-learning practices and a conducive learning
environment.
⚫ To create the center of excellence through collaborations with industries and
various entities, addressing the evolving demands of society.
⚫ To foster an environment that promotes innovation, multidisciplinary research,
skill enhancement and entrepreneurship.
⚫ To uphold and advocate for elevated standards of professional ethics and
transparency.
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 PageNo.
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
1
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 withREP 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
STACKof 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 7
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 12
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 15

6. Design, Develop and Implement a menu driven Program in C for the following operations
on CircularQUEUE 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
18
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 SinglyLinked 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. 23
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 DoublyLinked 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 32
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 CircularLinked 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 41
the result inPOLYSUM(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 BinarySearch 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 50
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. 57
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 Hash 61
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.
13. Viva Questions 66
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 onCircular
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 SinglyCircular
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 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 onBinary
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/BFSmethod

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 theirapplications.
• 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]) {
printf("\nWeekly Calendar:\n");
Dept of CS&BS, KSSEM Page 1
DATA STRUCTURES LABORATORY BCSL305

for (int i = 0; i < 7; i++) {


printf("\nDay %d:\n", i + 1);
printf("Name: %s\n", calendar[i].dayName);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
}
}

int main() {
struct Day calendar[7];
printf("Please enter data for each day of the week:\n");
read(calendar);
display(calendar);
// Free dynamically allocated memory
for (int i = 0; i < 7; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}

return 0;
}

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:
Name: tuesday
Dept of CS&BS,KSSEM Page 3
DATA STRUCTURES LABORATORY BCSL305
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>
#include <string.h>
int strcmp_from_specific_pos(char p[], char t[], int pos)
{
int i = 0;
while (p[i] != '\0' && t[pos + i] != '\0')
{
if (p[i] != t[pos + i]) {
return 0; // Strings are not equal
}
i++;
}
if (p[i] == '\0')
{
return -1; // Pattern found
}
else
{
return 0; // Pattern not found
}
}

void my_search_replace(char p[], char t[], char r[], char d[])


{
int i, j, k, m, n, z;
i = j = k = 0;
m = strlen(t);
n = strlen(p);
while (i <= m - n)
{
if (strcmp_from_specific_pos(p, t, i) == -1)
{
while (j < i)
d[k++] = t[j++];
z = 0;
while (r[z] != '\0')
d[k++] = r[z++];
i = i + n; // Use n instead of strlen(p)
j = i;
}
else
Dept of CS&BS,KSSEM Page 5
DATA STRUCTURES LABORATORY BCSL305
i++;
}
while (j < m)

d[k++] = t[j++];
d[k] = '\0';
}

int main()
{
char t[40], p[10], r[10], d[40];
printf("Enter the text string t: ");
fgets(t, sizeof(t), stdin);
printf("Enter the pattern string p: ");
fgets(p, sizeof(p), stdin);
printf("Enter the replace string r: ");
fgets(r, sizeof(r), stdin);
// Remove newline characters from inputs
t[strcspn(t, "\n")] = '\0';
p[strcspn(p, "\n")] = '\0';
r[strcspn(r, "\n")] = '\0';
my_search_replace(p, t, r, d);
printf("The final string after replacing: %s\n", d);
return 0;
}

2nd Program Output - String

#Run
Enter the text string t : I am Studying in Medical College
Enter the pattern String p : Medical
Enter the replace String r : Engineering
The final string after replacing : I am Studying in Engineering College

Dept of CS&BS,KSSEM Page 6


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 STACK_SIZE 5
int top, s[10], item, flag;
char str[20];
void push() {
if (top == STACK_SIZE - 1) {
printf("stack overflow\n");
return;
}
top = top + 1;
s[top] = item;
}
int pop() {
int item_deleted;
if (top == -1)
return 0;
item_deleted = s[top--];
return item_deleted;
}
void display() {
int i;
if (top == -1) {

printf("stack is empty\n");
return;

}
printf("contents of stack\n");
for (i = 0; i <= top; i++)
printf("%d\n", s[i]);
}
int is_palindrome(char str[]) {
int i;
int top;
char s[20], stk_item;
top = -1;
for (i = 0; i < strlen(str); i++)
s[++top] = str[i];
for (i = 0; i < strlen(str); i++) {
Dept of CS&BS, KSSEM Page 7
DATA STRUCTURES LABORATORY BCSL305
stk_item = s[top--];
if (str[i] != stk_item)
return 0;
}
return 1;
}
int main() {
int choice;
top = -1;
for (;;) {
printf("1:PUSH 2:POP\n");
printf("3:DISPLAY 4:PALINDROME\n");
printf("5:CHECK 6:EXIT\n");
printf("enter the choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("enter the item you want to insert: ");
scanf("%d", &item);
push();
break;
case 2:
item = pop();
if (item == 0)
printf("stack is empty\n");
else

printf("item deleted=%d\n", item);


break;
case 3:
display();
break;

case 4:
printf("enter the string: ");
scanf("%s", str);
flag = is_palindrome(str);

if (flag == 0)
printf("the string is not a palindrome\n");
else
printf("the string is a palindrome\n");
break;
case 5:
if (top == STACK_SIZE - 1)
printf("stack overflow\n");
else if (top == -1)
printf("stack underflow\n");
else
printf("Operations can be performed.\n");
break;
default:
exit(0);
}
Dept of CS&BS, KSSEM Page 8
DATA STRUCTURES LABORATORY BCSL305
}
return 0;
}

Dept of CS&BS, KSSEM Page 9


DATA STRUCTURES LABORATORY BCSL305

3rd Program Output - Stack


1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 4
enter the string: mam
the string is a palindrome
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 4
enter the string: abc
the string is not a palindrome
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 34
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 37
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 39
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 43
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 47
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 5
stack overflow
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 49
stack overflow
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 2
item deleted=47
1:PUSH 2:POP
Dept of CS&BS, KSSEM Page 10
DATA STRUCTURES LABORATORY BCSL305
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 2
item deleted=43
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 2
item deleted=39
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 2
item deleted=37
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 2
item deleted=34
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 3
stack is empty
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 5
stack underflow
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 34
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 1
enter the item you want to insert: 37
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 5
Operations can be performed.
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 3
contents of stack
34
37
1:PUSH 2:POP
3:DISPLAY 4:PALINDROME
5:CHECK 6:EXIT
enter the choice: 6

Dept of CS&BS, KSSEM Page 11


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.

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

int top = -1;


char s[30];
int j;
int i;
char symbol;

int F(char symbol)


{
switch(symbol)
{
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 3;
case '$':
case '^':
return 6;
case '(':
return 0;
case '#':
return -1;
default:
return 8;
}}

int G(char symbol) {


switch(symbol) {
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 4;
case '$':
case '^':
Dept of CS&BS, KSSEM Page 12
DATA STRUCTURES LABORATORY BCSL305
return 5;
case '(':
return 9;
case ')':
return 0;
default:
return 7;
}
}

void infix_postfix(char infix[], char postfix[])


{
printf("Enter a valid infix expression:\n");
scanf("%s",infix);
s[++top] = '#';
j = 0;

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


{
symbol = infix[i];
while (F(s[top]) > G(symbol))
{
postfix[j] = s[top--];
j++;
}
if (F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while (s[top] != '#')
{
postfix[j++] = s[top--];
}

postfix[j] = '\0';

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


}

int main()
{
char infix[20];
char postfix[20];

infix_postfix(infix,postfix);

return 0;
}

Dept of CS&BS, KSSEM Page 13


DATA STRUCTURES LABORATORY BCSL305

4th Program Output – Infix to Postfix


Enter a valid infix expression:
a+b/c*d^e+f
The postfix expression is:
abcde^*/f++

Enter a valid infix expression:


a+b^c*(E+F)
The postfix expression is:
abc^EF+*+

Dept of CS&BS, KSSEM Page 14


DATA STRUCTURES LABORATORY BCSL305

5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
double compute(char symbol, double op1, double op2) {
switch(symbol) {
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '%':
return fmod(op1, op2); // Use fmod for floating-point modulus

case '$':
case '^':
return pow(op1, op2);
default:
printf("Invalid operator encountered\n");
return 0.0; // Adjust as needed
}
}
int main() {
double s[20];
double res;
double op1;
double op2;

int top = -1;


int i;
char postfix[20];
char symbol;
printf("Enter the postfix expression:\n");
scanf("%s", postfix);
for (i = 0; i < strlen(postfix); i++) {
symbol = postfix[i];
if (isdigit(symbol)) {
s[++top] = symbol - '0';
} else {
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("The result is %f\n", res);
Dept of CS&BS, KSSEM Page 15
DATA STRUCTURES LABORATORY BCSL305
return 0;
}

5a Program Output : Suffix expression

Enter the postfix expression:


123+*321-+*
The result is 20.000000

Enter the postfix expression:


2342*+%
The result is 2.000000

Dept of CS&BS, KSSEM Page 16


DATA STRUCTURES LABORATORY BCSL305

5) b. Solving Tower of Hanoi problem with n disks

#include <stdio.h>
void transfer(int n, char source, char temp, char dest) {
if (n == 0)
return;
transfer(n - 1, source, dest, temp);

printf("Move disc %d from %c to %c\n", n, source, dest);


transfer(n - 1, temp, source, dest);
}
int main() {
int n;
printf("Enter the number of discs\n");
scanf("%d", &n);

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


return 0;
}

5b Program Output - Tower of Hanoi

Enter the number of discs


3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B

Move disc 3 from A to C


Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C

Dept of CS&BS, KSSEM Page 17


DATA STRUCTURES LABORATORY BCSL305

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 max 5
int q[max], f = -1, r = -1;
void insert() {
int t;
if ((r + 1) % max == f)
printf("Queue overflow");
else {
if (f == -1)
f = r = 0;
else
r = (r + 1) % max;
printf("Enter element to be inserted: ");
scanf("%d", &q[r]);
}
}
void delete() {
if (f == -1)
printf("Queue underflow");
else {
printf("Element deleted is %d", q[f]);
if (f == r)
f = r = -1;
else
f = (f + 1) % max;
}
}
void display() {
if (f == -1)
printf("Queue empty");
else {
int i = f;
printf("Queue elements are: ");
while (i != r) {
printf("%d ", q[i]);
i = (i + 1) % max;
}
printf("%d", q[i]);
printf("\nFront is at: %d\nRear is at: %d", q[f], q[r]);
}
}

Dept of CS&BS, KSSEM Page 18


DATA STRUCTURES LABORATORY BCSL305
int main() {

int ch;
while (1) {
printf("\nCircular Queue operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice");
}
}
return 0;
}

Dept of CS&BS, KSSEM Page 19


DATA STRUCTURES LABORATORY BCSL305

6th Program Output - Circular QUEUE

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 3
Queue empty
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter element to be inserted: 34

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter element to be inserted: 37

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter element to be inserted: 39

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter element to be inserted: 43

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter element to be inserted: 47

Circular Queue operations


1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Queue overflow
Circular Queue operations
1. Insert
Dept of CS&BS, KSSEM Page 20
DATA STRUCTURES LABORATORY BCSL305
2. Delete
3. Display
4. Exit
Enter choice: 49
Invalid choice
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 3
Queue elements are: 34 37 39 43 47
Front is at: 34
Rear is at: 47
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Element deleted is 34
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Element deleted is 37
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Element deleted is 39
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Element deleted is 43
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Element deleted is 47
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
Queue underflow
Circular Queue operations
1. Insert
2. Delete
3. Display
Dept of CS&BS, KSSEM Page 21
DATA STRUCTURES LABORATORY BCSL305
4. Exit
Enter choice: 3
Queue empty
Circular Queue operations
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 4

Dept of CS&BS, KSSEM Page 22


DATA STRUCTURES LABORATORY BCSL305

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
{
int usn;
char name[20];
char branch[20];
int sem;
char phone[20];
} STUDENT;

struct node
{
int usn;
char name[20];
char branch[20];
int sem;
char phone[20];
struct node *link;
};

typedef struct node* NODE;

NODE getnode()
{
NODE x;

x = (NODE )malloc(sizeof (struct node)); /* allocate the memory space */


if ( x == NULL ) /* Free nodes don’t exist */
{
printf ("Out of memory\n"); /* Allocation failed */
exit(0); /* Terminate the program */
}
return x; /* allocation successful */
}

NODE insert_front(STUDENT item, NODE first)


{
NODE temp;
temp = getnode(); /*obtain a node from OS */
temp->usn = item.usn; /* Insert various items into new node */
Dept of CS&BS, KSSEM Page 23
DATA STRUCTURES LABORATORY BCSL305
strcpy(temp->name,item.name);
strcpy(temp->branch,item.branch);
temp->sem = item.sem;
strcpy(temp->phone,item.phone);
temp->link = NULL;

if(first == NULL)
return temp; /* Insert a node for the first time */
temp->link = first; /* Insert at the beginning of existing list */
return temp; /* return address of new first node */
}

NODE insert_rear(STUDENT item, NODE first)


{
NODE temp, cur;

temp = getnode(); /*obtain a node from OS */


temp->usn = item.usn; /* Insert various items into new node */
strcpy(temp->name, item.name);
strcpy(temp->branch, item.branch);
temp->sem = item.sem;
strcpy(temp->phone, item.phone);
temp->link = NULL;

if (first == NULL)return temp; /* Insert a node for the first time */

/* Get the address of the first node */


cur = first;
/* Find the address of the last node */
while (cur->link != NULL)
{
cur = cur->link;
}

/* Insert the node at the end */


cur->link = temp;

/* return address of the first node */


return first;
}

NODE delete_front(NODE first)


{
NODE temp;
if(first == NULL) /* Check for empty list */
{
printf ("student list is empty cannot delete\n");
return NULL; // We can replace NULL with first also
}

temp = first; /* Retain address of the node to be deleted */


Dept of CS&BS, KSSEM Page 24
DATA STRUCTURES LABORATORY BCSL305
temp = temp->link; /* Obtain address of the second node */
printf("Delete student record: USN = %d\n", first->usn);
free(first); /* delete the front node */
return temp; /* return address of the first node */
}

NODE delete_rear(NODE first)


{
NODE cur, prev;

if(first == NULL) /* Check for empty list */


{
printf("student list is empty cannot delete\n");
return first;
}

if( first->link == NULL ) /*Only one node is present and delete it */


{
printf ("Delete student record: USN = %d\n", first->usn);
free(first); /* return to availability list */
return NULL; /* List is empty so return NULL */
}

/* Obtain address of the last node and just previous to that */


prev = NULL;
cur = first;
while(cur->link != NULL)
{
prev = cur;
cur = cur->link;
}

printf("Delete student record: USN = %d\n", first->usn);


free(cur); /* delete the last node */

prev->link = NULL; /* Make last but one node as the last node */
return first; /* return address of the first node */
}

void display(NODE first)


{
NODE cur; int count = 0;

if(first == NULL) /* List is empty */


{
printf("student list is empty\n");
return;
}

cur = first; /* Display employee details */


while(cur != NULL)
{
Dept of CS&BS, KSSEM Page 25
DATA STRUCTURES LABORATORY BCSL305
printf("%d %s %s %d %s \n", cur ->usn, cur->name, cur->branch, cur->sem, cur->phone);
cur = cur->link;
count++;
}
printf ("Number of students = %d\n", count);
}

void main()
{
NODE first;
int choice,i,n;
STUDENT item;

first = NULL;

for (;;)
{
printf ("1: Create SLL\n");
printf ("2:Insert_Front 3: Insert_Rear\n");
printf ("4:Delete_Front 5: Delete_Rear\n");
printf ("6:Display 7: Exit\n");
printf ("Enter the choice\n");
scanf ("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the number of students to be created\n");
scanf("%d",&n);
while(i<n)
{ i++;
printf("usn :");
scanf("%d",&item.usn);
printf("name :");
scanf("%s",item.name);
printf("branch :");
scanf("%s",item.branch);
printf("semester :");
scanf("%d",&item.sem);
printf("phone :");
scanf("%s",item.phone);

first = insert_front(item,first); // or call insert_end();


}
break;
case 2:
printf("usn :");
scanf("%d",&item.usn);
printf("name :");
scanf("%s",item.name);
printf("branch :");
scanf("%s",item.branch);
Dept of CS&BS, KSSEM Page 26
DATA STRUCTURES LABORATORY BCSL305
printf("semester :");
scanf("%d",&item.sem);
printf("phone :");
scanf("%s",item.phone);
first = insert_front (item, first);
break ;
case 3:
printf ("usn :");
scanf ("%d",&item.usn);
printf ("name :");
scanf ("%s",item.name);
printf ("branch :");
scanf ("%s",item.branch);
printf ("semester :");
scanf ("%d",&item.sem);
printf ("phone :");
scanf ("%s",item.phone);
first = insert_rear (item, first);
break ;
case 4:
first = delete_front(first);
break ;
case 5:
first = delete_rear(first);
break ;
case 6:
display(first);
break ;
default:
exit(0);
}
}
}

7th Program Output - Singly Linked List (SLL)

1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
1
Enter the number of students to be created
3
usn :001
name :aaa
branch :csbs
semester :3
phone :22331234
usn :2
name :bbb

Dept of CS&BS, KSSEM Page 27


DATA STRUCTURES LABORATORY BCSL305
branch :csbs
semester :3
phone :22331234
usn :3
name :ccc
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
3 ccc csbs 3 22331234
2 bbb csbs 3 22331234
1 aaa csbs 3 22331234
Number of students = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
4
Delete student record: USN = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
2 bbb csbs 3 22331234
1 aaa csbs 3 22331234
Number of students = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
5
Delete student record: USN = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
2 bbb csbs 3 22331234
Number of students = 1
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear

Dept of CS&BS, KSSEM Page 28


DATA STRUCTURES LABORATORY BCSL305
6:Display 7: Exit
Enter the choice
2
usn :4
name :ddd
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
4 ddd csbs 3 22331234
2 bbb csbs 3 22331234
Number of students = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
3
usn :5
name :sss
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
4 ddd csbs 3 22331234
2 bbb csbs 3 22331234
5 sss csbs 3 22331234
Number of students = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
7
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
1
Enter the number of students to be created
3

Dept of CS&BS, KSSEM Page 29


DATA STRUCTURES LABORATORY BCSL305
usn :001
name :aaa
branch :csbs
semester :3
phone :22331234
usn :2
name :bbb
branch :csbs
semester :3
phone :22331234
usn :3
name :ccc
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
3 ccc csbs 3 22331234
2 bbb csbs 3 22331234
1 aaa csbs 3 22331234
Number of students = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
4
Delete student record: USN = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
2 bbb csbs 3 22331234
1 aaa csbs 3 22331234
Number of students = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
5
Delete student record: USN = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit

Dept of CS&BS, KSSEM Page 30


DATA STRUCTURES LABORATORY BCSL305
Enter the choice
6
2 bbb csbs 3 22331234
Number of students = 1
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
2
usn :4
name :ddd
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
4 ddd csbs 3 22331234
2 bbb csbs 3 22331234
Number of students = 2
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
3
usn :5
name :sss
branch :csbs
semester :3
phone :22331234
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
4 ddd csbs 3 22331234
2 bbb csbs 3 22331234
5 sss csbs 3 22331234
Number of students = 3
1: Create SLL
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
7

Dept of CS&BS, KSSEM Page 31


DATA STRUCTURES LABORATORY BCSL305
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>
#include<string.h>

typedef struct
{
int ssn;
char name[20];
char department[20];
char designation[20];
float salary;
char phone[20];
} EMPLOYEE;

struct node
{
int ssn;
char name[20];
char department[20];
char designation[20];
float salary;
char phone[20];
struct node *llink;
struct node *rlink;
};

typedef struct node* NODE;

NODE getnode()
{
NODE x;

x = (NODE )malloc(sizeof (struct node)); /* allocate the memory space */


if ( x == NULL ) /* Free nodes don’t exist */
{
printf ("Out of memory\n"); /* Allocation failed */
exit(0); /* Terminate the program */
}
return x; /* allocation successful */
Dept of CS&BS, KSSEM Page 32
DATA STRUCTURES LABORATORY BCSL305
}

NODE insert_front(EMPLOYEE item, NODE first)


{
NODE temp;

temp = getnode(); /*obtain a node from OS */


temp->ssn = item.ssn; /* Insert various items into new node */
strcpy(temp->name, item.name);
strcpy(temp->department, item.department);
strcpy(temp->designation, item.designation);
temp->salary = item.salary;
strcpy(temp->phone, item.phone);
temp->llink = temp->rlink = NULL;

if(first == NULL)
return temp; /* Insert a node for the first time */

temp->rlink = first; /* Insert at the beginning of existing list */


first->llink = temp;

return temp; /* return address of new first node */


}

//NODE insert_rear(int item, NODE first)


NODE insert_rear(EMPLOYEE item, NODE first)
{
NODE temp, cur;
temp = getnode(); /*obtain a node from OS */
temp->ssn = item.ssn; /* Insert various items into new node */
strcpy(temp->name, item.name);
strcpy(temp->department, item.department);
strcpy(temp->designation, item.designation);
temp->salary = item.salary;
strcpy(temp->phone, item.phone);
temp->llink = temp->rlink = NULL;

if(first == NULL)
return temp; /* Insert a node for the first time */

/* Get the address of the first node */


cur = first;

/* Find the address of the last node */


while(cur->rlink != NULL)
{
Dept of CS&BS, KSSEM Page 33
DATA STRUCTURES LABORATORY BCSL305
cur = cur->rlink;
}

/* Insert the node at the end */


cur->rlink = temp;
temp->llink = cur;

/* return address of the first node */


return first;
}

NODE delete_front(NODE first)


{
NODE second;
if(first == NULL) /* Check for empty list */
{
printf("employee list is empty \n");
return NULL; // We can replace NULL with first also
}

if(first ->rlink == NULL) /* Delete if there is only one node */


{
printf ("Employee details deleted: ssn: %d\n", first->ssn);
free(first);
return NULL;
}

second = first->rlink; /* Get the address of second node */


second->llink = NULL; /* Make second node as the first node */

printf ("Employee details deleted: ssn: %d\n", first->ssn);


free(first); /* Delete the first node */

return second;
}

NODE delete_rear(NODE first)


{
NODE cur, prev;

if(first == NULL) /* Check for empty list */


{
printf ("List is empty cannot delete\n");
return first;
}

if(first ->rlink == NULL) /*Only one node is present and delete it */


{
Dept of CS&BS, KSSEM Page 34
DATA STRUCTURES LABORATORY BCSL305
printf ("Employee details deleted: ssn: %d\n", first->ssn);
free(first); /* return to availability list */
return NULL; /* List is empty so return NULL */
}

/* Obtain address of the last node and just previous to that */


prev = NULL;
cur = first;

while( cur->rlink != NULL )


{
prev = cur;
cur = cur->rlink;
}

printf("Employee details deleted: ssn: %d \n", cur ->ssn);


free(cur); /* delete the last node */

prev->rlink = NULL; /* Make last but one node as the last node */

return first; /* return address of the first node */


}

void display(NODE first)


{
NODE temp,cur;
int count = 0;

if(first == NULL) /* List is empty */


{
printf ("employee list is empty\n");
return;
}

/* Display employee details */


cur = first;
while(cur != NULL)
{
printf("%d %f %s %s %s %s\n", cur ->ssn, cur->salary, cur->name,cur->department, cur->designation,
cur->phone);
cur = cur->rlink;
count++;
}
printf("Number of employees = %d\n", count);
}

void main()
{
Dept of CS&BS, KSSEM Page 35
DATA STRUCTURES LABORATORY BCSL305
NODE first;
int choice,n,i=0;
EMPLOYEE item;

first = NULL;

for (;;)
{
printf("1:Create list\n");
printf("2:Insert_Front 3: Insert_Rear\n");
printf("4:Delete_Front 5: Delete_Rear\n");
printf("6:Display 7: Exit\n");
printf("Enter the choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the number of Employees:");
scanf("%d",&n);
while(i!=n)
{
i++;
printf("Enter details\n");
printf("ssn :");
scanf("%d",&item.ssn);
printf("name :");
scanf("%s",item.name);
printf("department :");
scanf("%s",item.department);
printf("designation :");
scanf("%s",item.designation);
printf("salary :");
scanf("%f",&item.salary);
printf("phone :");
scanf("%s",item.phone);
first = insert_rear (item, first);
}
printf("[+] DONE\n");
break ;
case 2:
printf("ssn :");
scanf("%d",&item.ssn);
printf("name :");
scanf("%s",item.name);
printf("department :");
scanf("%s",item.department);
printf("designation :");
scanf("%s",item.designation);
Dept of CS&BS, KSSEM Page 36
DATA STRUCTURES LABORATORY BCSL305
printf("salary :");
scanf("%f",&item.salary);
printf("phone :");
scanf("%s",item.phone);
first = insert_front(item, first);
break ;
case 3:
printf("ssn :");
scanf("%d",&item.ssn);
printf("name :");
scanf("%s",item.name);
printf("department :");
scanf("%s",item.department);
printf("designation :");
scanf("%s",item.designation);
printf("salary :");
scanf("%f",&item.salary);
printf("phone :");
scanf("%s",item.phone);
first = insert_rear (item, first);
break ;
case 4:
first = delete_front(first);
break ;
case 5:
first = delete_rear(first);
break ;
case 6:
display(first);
break ;
default:
exit(0);
}
}
}

Dept of CS&BS, KSSEM Page 37


DATA STRUCTURES LABORATORY BCSL305

8th Program Output – Doubly Linked List

1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
1
Enter the number of Employees:2
Enter details
ssn :01
name :aaa
department :csbs
designation :developer
salary :12000
phone :22331234
Enter details
ssn :02
name :bbb
department :csbs
designation :developer
salary :7000
phone :22331234
[+] DONE
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
1 12000.000000 aaa csbs developer 22331234
2 7000.000000 bbb csbs developer 22331234
Number of employees = 2
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
5
Employee details deleted: ssn: 2
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
1 12000.000000 aaa csbs developer 22331234
Number of employees = 1
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
Dept of CS&BS, KSSEM Page 38
DATA STRUCTURES LABORATORY BCSL305
2
ssn :9
name :sss
department :csbs
designation :developer
salary :25000
phone :22331234
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
9 25000.000000 sss csbs developer 22331234
1 12000.000000 aaa csbs developer 22331234
Number of employees = 2
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
3
ssn :3
name :ccc
department :csbs
designation :developer
salary :20000
phone :22331234
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
9 25000.000000 sss csbs developer 22331234
1 12000.000000 aaa csbs developer 22331234
3 20000.000000 ccc csbs developer 22331234
Number of employees = 3
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
4
Employee details deleted: ssn: 9
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
6
1 12000.000000 aaa csbs developer 22331234
3 20000.000000 ccc csbs developer 22331234
Number of employees = 2
Dept of CS&BS, KSSEM Page 39
DATA STRUCTURES LABORATORY BCSL305
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
5
Employee details deleted: ssn: 3
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
4
Employee details deleted: ssn: 1
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
4
employee list is empty
1:Create list
2:Insert_Front 3: Insert_Rear
4:Delete_Front 5: Delete_Rear
6:Display 7: Exit
Enter the choice
7

Dept of CS&BS, KSSEM Page 40


DATA STRUCTURES LABORATORY BCSL305
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 41


DATA STRUCTURES LABORATORY BCSL305
{
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 42
DATA STRUCTURES LABORATORY BCSL305
}
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 43
DATA STRUCTURES LABORATORY BCSL305
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 44


DATA STRUCTURES LABORATORY BCSL305

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.45
DATA STRUCTURES LABORATORY BCSL305
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.46
DATA STRUCTURES LABORATORY BCSL305
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.47


DATA STRUCTURES LABORATORY BCSL305
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.48


DATA STRUCTURES LABORATORY BCSL305
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.49


DATA STRUCTURES LABORATORY BCSL305

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


DATA STRUCTURES LABORATORY BCSL305
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.51


DATA STRUCTURES LABORATORY BCSL305
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.52


DATA STRUCTURES LABORATORY BCSL305

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

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
Dept of CS&BS, KSSEM Page No.53
DATA STRUCTURES LABORATORY BCSL305
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
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
Dept of CS&BS, KSSEM Page No.54
DATA STRUCTURES LABORATORY BCSL305
Enter item to be inserted: 34

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
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
Dept of CS&BS, KSSEM Page No.55
DATA STRUCTURES LABORATORY BCSL305
3 search
4 exit
Enter choice: 4

Dept of CS&BS, KSSEM Page No.56


DATA STRUCTURES LABORATORY BCSL305

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


DATA STRUCTURES LABORATORY BCSL305
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.58


DATA STRUCTURES LABORATORY BCSL305

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.59
DATA STRUCTURES LABORATORY BCSL305
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.60


DATA STRUCTURES LABORATORY BCSL305
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;
case 2: printf("?Employee details are\n");

Dept of CS&BS, KSSEM Page No.61


DATA STRUCTURES LABORATORY BCSL305
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.62


DATA STRUCTURES LABORATORY BCSL305
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.63
DATA STRUCTURES LABORATORY BCSL305
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.64
DATA STRUCTURES LABORATORY BCSL305
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.65


DATA STRUCTURES LABORATORY BCSL305

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


DATA STRUCTURES LABORATORY BCSL305

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


DATA STRUCTURES LABORATORY BCSL305

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


DATA STRUCTURES LABORATORY BCSL305

27. Difference between array and structure?

Dept of CS&BS, KSSEM Page No.69

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy