DS Manual 22scheme BCSL305
DS Manual 22scheme BCSL305
K. S. Group of Institutions
K. S. SCHOOL OF ENGINEERING & MANAGEMENT
Mallasandra, Off Kanakapura Road, Bengaluru - 560 109
Department of
Computer Science & Business Systems
Prepared by: -
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
MISSION:
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
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
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
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>
return day;
}
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;
}
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
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
}
}
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;
}
#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
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
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;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
postfix[j] = '\0';
int main()
{
char infix[20];
char postfix[20];
infix_postfix(infix,postfix);
return 0;
}
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;
#include <stdio.h>
void transfer(int n, char source, char temp, char dest) {
if (n == 0)
return;
transfer(n - 1, source, dest, temp);
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]);
}
}
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;
}
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;
};
NODE getnode()
{
NODE x;
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 */
}
prev->link = NULL; /* Make last but one node as the last node */
return first; /* return address of the first node */
}
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);
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
#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;
};
NODE getnode()
{
NODE x;
if(first == NULL)
return temp; /* Insert a node for the first time */
if(first == NULL)
return temp; /* Insert a node for the first time */
return second;
}
prev->rlink = NULL; /* Make last but one node as the last node */
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);
}
}
}
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
#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;
};
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n"); exit(0);
}
return x;
}
void polysum()
{
NODE h1,h2,h3;
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=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)
{
#Run 1
#Run
10. Design, Develop and Implement a menu driven Program in C for the following operations on Binary Search
Tree (BST) of Integers
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
}
return x;
}
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(temp->info<cur->info)?cur->llink:cur->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: ");
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
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 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 3:exit(0);
#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");
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?
3. What are the major data structures used in the following areas : RDBMS,
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.
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 + ^
(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.
14. List out few of the applications that make use of Multilinked Structures?
(i) Sparse matrix (ii) Index generation.
16. What is the type of the algorithm used in solving the 8 Queens problem?
Backtracking and Branch Bound.
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.
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?
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.
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.