0% found this document useful (0 votes)
14 views82 pages

DS Manual - 22scheme - Updated

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)
14 views82 pages

DS Manual - 22scheme - Updated

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/ 82

K.

S SCHOOL OF ENGINEERING & MANAGEMENT,


BANGALORE - 109

“DATA STRUCTURES LABORATORY”


Subject code: BCSL305

Prepared by: -

Mrs. Kavitha K S Mrs. Bindu K P


Assistant Professor Assistant Professor
Dept. of CSE, KSSEM Dept. of CSE, KSSEM

Department of Computer Science & Engineering


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 ENGINEERING

Course: Data Structures Laboratory


Type: Core Course Academic Year: 2023-2024
Code:22BCSL305
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 produce quality Computer Science professional, possessing excellent
technical knowledge, skills, personality through education and research.”

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

Data Structures Laboratory (BCSL305)


INDEX
Sl Programs List Page
No No.
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7 days
of a week. Each Element of the array is a structure having three fields. The first field is the name
of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), 1
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 10
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 13
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. 20

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 23

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
27
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 onSingly
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 35
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 onDoubly
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 44
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 54
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 60
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 66

12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determinethe records
in file F. Assume that file F is maintained in memory by a Hash Table (HT) of mmemory locations with
L as the set of memory addresses (2-digit) of locations in HT. Let thekeys in K and addresses in L are
Integers. Design and develop a Program in C that uses Hashfunction H: K ->L as H(K)=K mod m 70
(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 74


DATA STRUCTURES LABORATORY
SEMESTER – III

Course Code BCSL305 CIE Marks 50


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

Descriptions (if any):


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

2. Design, Develop and Implement a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.

3. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations

4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
operands.
5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,
^
b. Solving Tower of Hanoi problem with n disks
6. Design, Develop and Implement a menu driven Program in C for the following operations on
Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
7. Design, Develop and Implement a menu driven Program in C for the following operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem,
PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
8. Design, Develop and Implement a menu driven Program in C for the following operations on
Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
9. Design, Develop and Implement a Program in C for the following operationson Singly
Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
10. Design, Develop and Implement a menu driven Program in C for the following operations on
Binary Search Tree (BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
11. Design, Develop and Implement a Program in C for the following operations on Graph(G)
of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Design and develop a Program in C that uses Hash
function H: K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.
Laboratory Outcomes: The student should be able to:
• Analyze and Compare various linear and non-linear data structures
• Demonstrate the working nature of different types of data structures and 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 (Courseed to change in accoradance 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;
int month;
int year;
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 (dd/mm/yy): ");
scanf("%d/%d/%d", &day.date, &day.month, &day.year);
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();
}
}

Dept. of CSE, KSSEM Page 1


DATA STRUCTURES LABORATORY BCSL305

// Function to display the calendar


void display(struct Day calendar[7]) {
printf("\nWeekly Calendar:\n");

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


printf("\nDay %d:\n", i + 1);
printf("Name: %s\n", calendar[i].dayName);
printf("Date: %02d/%02d/%02d\n", calendar[i].date, calendar[i].month, calendar[i].year % 100);
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 CSE, KSSEM Page 2


DATA STRUCTURES LABORATORY BCSL305

1st Program Output- Calendar

Dept. of CSE, KSSEM Page 3


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 4


DATA STRUCTURES LABORATORY BCSL305

2. Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.

#include<stdio.h>
void readstr(void);
void readsearchstr(void);
void readreplacestr(void);
void matching(char str[100]);
char mainstr[100],pat[100],replace[100],ans[100];

void main()
{
readstr();
readsearchstr();
readreplacestr();
matching(mainstr);
}
void readstr(void)
{
printf("\n enter a string\n");
gets(mainstr);
}
void readsearchstr(void)
{
printf("enter a search string\n");
gets(pat);
}
void readreplacestr(void)
{
printf("\n enter a replace string\n");
gets(replace);
}
void matching(char mainstr[100])
{
char ans[100];
int i,j,c,m,k,flag;
i=m=c=j=flag=0;
while(mainstr[c]!='\0')
{
if(mainstr[m]==pat[i])
{
i++;

m++;
if(pat[i]=='\0')
{
for(k=0;replace[k]!='\0';k++,j++)
ans[j]=replace[k];
i=0;
Dept. of CSE, KSSEM Page 5
DATA STRUCTURES LABORATORY BCSL305

c=m;
}
} flag=1;
else
{
ans[j]=mainstr[c];
j++;
c++;
m=c;
i=0;
}
}

ans[j]='\0';
if(flag==0)
{
printf("search string is not present\n");
printf("the resultant string is %s",ans);
}
else

printf("the resultant string is %s\n",ans);


}

2nd Program Output - String

Dept. of CSE, KSSEM Page 6


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 7


DATA STRUCTURES LABORATORY BCSL305

3. Design, Develop and Implement a menu driven Program in C for the following operations on STACK of
integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations

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

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

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

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

scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
stack1[++top1]=str[i];
Dept. of CSE, KSSEM Page 8
DATA STRUCTURES LABORATORY BCSL305

}
for(i=0;i<len;i++)
{
if(str[i]==stack1[top1--])
count++;
}
if(count==len)
printf("\n %s is a palindrome string\n",str);
else
printf("\n %s is not a palindrome string\n",str);
return;
}

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

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

void main()
{
int choice;
while(1)
{
printf("\n STACK OPERATIONS\n");
printf("\n 1:push\t2:pop\t3:palindrome\t4:check\t5:display\t6:exit\n");
printf("\n enter your choice[1/2/3/4/5/6]:");
scanf("%d",&choice);
switch(choice)
{
case 1: push();break;
case 2: pop();break;
case 3: palindrome();break;

Dept. of CSE, KSSEM Page 9


DATA STRUCTURES LABORATORY BCSL305
case 4: check();break;
case 5: display();break;
case 6: exit(0);
default : printf("invaild choice\n");
}
}
return;
}

3rd Program Output - Stack

Dept. of CSE, KSSEM Page 10


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 11


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 12


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 13


DATA STRUCTURES LABORATORY BCSL305

Dept. of CSE, KSSEM Page 14


DATA STRUCTURES LABORATORY BCSL305

4. Design, Develop and Implement a Program in C for converting an Infix Expression to


Postfix Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -,*, /, % (Remainder), ^(Power) and alphanumeric
operands.

#define SIZE 50 /* Size of Stack*/


#include <ctype.h>
#include <stdio.h>
char s[SIZE];
int top = -1; /* Global declarations */
push(char elem) /* Function for PUSH operation */
{
s[++top] = elem;
}
char pop() /* Function for POP operation */
{
return (s[top--]);
}
int pr(char elem) /* Function for precedence */
{

switch (elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 3;
case '^': return 4;
}
}
void main() /* Main Program */
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;

printf("\n\nRead the Infix Expression ? ");


scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
Dept. of CSE, KSSEM Page 15
DATA STRUCTURES LABORATORY BCSL305

if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}

Dept. of CSE, KSSEM Page 16


DATA STRUCTURES LABORATORY BCSL305
4th Program Output – Infix to Postfix

Dept. of CSE, KSSEM Page 17


DATA STRUCTURES LABORATORY 18CSL38

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

#include<stdio.h>
#include<math.h>
#include<ctype.h>
int top=-1;
float s[50];

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


{
switch(symbol)
{
case '+' :return(opr1+opr2);
case '-' :return(opr1-opr2);
case '*' :return(opr1*opr2);
case '/' :return(opr1/opr2);
case '^' :return(pow(opr1,opr2));
}
}
void push(float symbol)
{
s[++top]= symbol;
}

float pop()
{
return(s[top--]);
}
float eval(char postfix[50])
{
int i;
char symbol;
float opr1,opr2,value,res;
for(i=0;postfix[i]!='\0';i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(symbol-48);
else if(isalpha(symbol))
{
printf("\n enter the value for %c ",symbol);
scanf("%f",&value);
push(value);
}
else
{
opr2=pop();
opr1=pop();
res=operate(opr1,opr2,symbol);

Dept. of CSE, KSSEM Page 18


DATA STRUCTURES LABORATORY 18CSL38

push(res);
}
}
return(pop());
}

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

Enter The Postfix Expression


45+
The Given Expression is :45+
The value of the expression is 9.000000
Enter The Postfix Expression
abc*+
The Given Expression is :abc*+
enter the value for a 1
enter the value for b 2
enter the value for c 3
The value of the expression is 7.000000*/

Dept. of CSE, KSSEM Page 19


DATA STRUCTURES LABORATORY 18CSL38

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

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

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

5b Program Output - Tower of Hanoi

Dept. of CSE, KSSEM Page 20


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 21


DATA STRUCTURES LABORATORY 18CSL38

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

#include<stdio.h>
#include<stdlib.h>
#define size 5
int CQ[size],f=-1,r=-1;
int CQfull()
{
if((f==r+1)||(f==0&&r==size-1))
return 1;
return 0;
}
int CQempty()
{
if(f==-1)
return 1;
return 0;
}
CQinsert(int ele)
{
if(CQfull())
printf("\n overflow!!!\n");
else
{
if(f==-1)
f=0;
r=(r+1)%size;
CQ[r]=ele;
}
}

int CQdelete()
{
int ele;
if(CQempty())
{
printf("\n underflow!!!\n");
return(-1);
}
else
{ ele=CQ[f];
if(f==r)
{
f=-1;
} r=-1;
else
Dept. of CSE, KSSEM Page 22
DATA STRUCTURES LABORATORY 18CSL38

f=(f+1)%size;
return(ele);
}
}
display()
{
int i;
if(CQempty())
printf("\n empty queue\n");
else
{
printf("front[%d]->",f);
for(i=f;i!=r;i=(i+1)%size)
printf(" %d ",CQ[i]);
printf(" %d ",CQ[i]);
printf("<-[%d]rear",r);
}
}
main()
{

int opn,ele;
do
{
printf("\n \n Circular queue operations\n");
printf("\n press 1-insert,2-delete,3-display,4-exit\n");
printf("\n your option? ");
scanf("%d",&opn);
switch(opn)
{
case 1:printf("\n read the element to be inserted ?\n");
scanf("%d",&ele);
CQinsert(ele);break;
case 2:ele=CQdelete();
if(ele!=-1)
printf("\n delete element is %d\n",ele);
break;
case 3:printf("\n status of circular queue \n\n");
display();
break;
case 4:exit(0);
default:printf("invalid option\n");
}
}
while(opn!=4);
}

Dept. of CSE, KSSEM Page 23


DATA STRUCTURES LABORATORY 18CSL38

6th Program Output - Circular QUEUE

Dept. of CSE, KSSEM Page 24


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 25


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 26


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 27


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 28


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 29


DATA STRUCTURES LABORATORY 18CSL38

7. Design, Develop and Implement a menu driven Program in C for the following operations on Singly Linked
List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit

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

typedef struct stud


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

node *start=NULL;

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

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

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

Dept. of CSE, KSSEM Page 30


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

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

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

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

Dept. of CSE, KSSEM Page 31


DATA STRUCTURES LABORATORY 18CSL38
free(temp);
}

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

cur=start;
pre=NULL;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
if(pre==NULL)
start=NULL;
else
pre->next=NULL;
printf("The node of student usn %s is deleted\n",cur->usn);
free(cur);
}

void main()
{
int ch;
for(;;)
{
printf("\n 1: CraeteSLL \n 2. Insert Front \n 3: Insert End \n 4: Delete Front \n 5: Delete End \n
6:Display\n 7: Exit \n");
printf("Enter ur choice\n");
scanf("%d",&ch);

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

Dept. of CSE, KSSEM Page 32


DATA STRUCTURES LABORATORY 18CSL38

7th Program Output - Singly Linked List (SLL)

Dept. of CSE, KSSEM Page 33


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 34


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 35


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 36


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 37


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 38


DATA STRUCTURES LABORATORY 18CSL38

8. Design, Develop and Implement a menu driven Program in C for the following operations on Doubly Linked
List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo

a. Create a DLL of N Employees Data by using end insertion.


b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit

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

struct node {
char ssn[15],name[15],dept[15],desg[15],phno[15];
long int sal;
struct node *llink,*rlink;
};
typedef struct node *NODE;
NODE temp, first=NULL, end=NULL;

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

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

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

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

void insertend(void)
{
printf("Enter Details\n");

Dept. of CSE, KSSEM Page 40


DATA STRUCTURES LABORATORY 18CSL38
temp=getnode();
temp->llink = temp->rlink = NULL;
read();
if(first==NULL)
first = end = temp;
else
{
end->rlink = temp;
temp->llink = end;
end = temp;
}
}

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

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

void main(void)
{

Dept. of CSE, KSSEM Page 41


DATA STRUCTURES LABORATORY 18CSL38
int ch;

Dept. of CSE, KSSEM Page 42


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

8th Program Output – Doubly Linked List

Dept. of CSE, KSSEM Page 43


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 44


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 45


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 46


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 47


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 48


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page 49


DATA STRUCTURES LABORATORY 18CSL38

9. Design, Develop and Implement a Program in C for the following operations on Singly Circular Linked List
(SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z- 2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z) Support the program with appropriate functions for each of the above operations

#include<stdio.h>
#include<malloc.h>
#include<math.h>
struct node
{
float cf;
int px,py,pz;
int flag;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
NODE insert_rear(float cf,float x,float y,float z,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
temp->flag=0;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;

void display(NODE head)


{
NODE temp;
if(head->link==head)
Dept. of CSE, KSSEM Page 50
DATA STRUCTURES LABORATORY 18CSL38
{
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 CSE, KSSEM Page 51


DATA STRUCTURES LABORATORY 18CSL38
}
return h3;
}

NODE read_poly(NODE head)


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

NODE h1,h2,h3;
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;

printf("enter the first polynomial\n");


h1=read_poly(h1);
printf("enter the second polynomail\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;
int x,y,z;
float sum=0.0;
e1=getnode();

Dept. of CSE, KSSEM Page 52


DATA STRUCTURES LABORATORY 18CSL38
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 CSE, KSSEM Page 53


DATA STRUCTURES LABORATORY 18CSL38

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

Dept. of CSE, KSSEM Page No.54


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.55


DATA STRUCTURES LABORATORY 18CSL38

10. Design, Develop and Implement a menu driven Program in C for the following operations on Binary Search
Tree (BST) of Integers

a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2


b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
e. Exit

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct node
{
int info;
struct node *llink;
struct node *rlink;
};

typedef struct node *NODE;


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

}
return x;
}

NODE insert(int item,NODE root)


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

}
else
{
prev=NULL;
cur=root;

while(cur!=NULL)
{
prev=cur;
cur=(temp->info<cur->info)?cur->llink:cur->rlink;
}

Dept. of CSE, KSSEM Page No.56


DATA STRUCTURES LABORATORY 18CSL38
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
}
void pre(NODE PRE)
{
if(PRE!=NULL)
{
printf("%d\t",PRE->info);pre(PRE->llink); pre(PRE->rlink);

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

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

void traversal(NODE root)


{
NODE IN,PRE,POST;
IN=root;
PRE=root;
POST=root;
if(root==NULL)
{
printf("emty tree");
return;
}
printf("preorder traversal: ");
pre(PRE);
printf("\ninorder traversal: ");

Dept. of CSE, KSSEM Page No.57


DATA STRUCTURES LABORATORY 18CSL38
in(IN);
printf("\npost order traversal: ");
post(POST);
return;
}

void search(NODE root)


{
int item,flag=0;
NODE cur;
printf("enter element to be searched: ");
scanf("%d",&item);
if(root==NULL)
{
printf("empty ree");
return;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->info)
flag=1;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}

if(flag==1)
printf("key found\n");
else
printf("key not found\n");
return;
}

void main()
{
int choice,item;
NODE root=NULL;
while(1)
{
printf("\n1 create bst\n2 traverse\n3 search\n4 exit");
printf("\nEnter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter item to be inserted: ");
scanf("%d",&item);
root=insert(item,root);break;
case 2: traversal(root);break;
case 3: search(root);break;
case 4: exit(0);
default: printf("enter prowopejwopu");
}

Dept. of CSE, KSSEM Page No.58


DATA STRUCTURES LABORATORY 18CSL38
}
}

Dept. of CSE, KSSEM Page No.59


DATA STRUCTURES LABORATORY 18CSL38

10th Program Output - Binary Search Tree (BST)

Dept. of CSE, KSSEM Page No.60


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.61


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.62


DATA STRUCTURES LABORATORY 18CSL38

11. Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define MAX 20
int G[MAX][MAX], n,visited[10];

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

void reachabilitycheck( int source)


{
int i;
for(i=0;i<n;i++)
visited[i]=0;
dfs(source);

Dept. of CSE, KSSEM Page No.63


DATA STRUCTURES LABORATORY 18CSL38
for(i=0;i<n;i++)
{
if(visited[i]==1)
printf("vertex %d is reachable\n",i);
else
printf("vertex %d is not reachable\n",i);
}
}

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

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


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

case 3:exit(0);

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


}
}
}

11th Program Output - Graph(G)

Dept. of CSE, KSSEM Page No.64


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.65


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.66


DATA STRUCTURES LABORATORY 18CSL38

12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine the records in
file F. Assume that file F is maintained in memory by a Hash Table(HT) of m memory locations with L
as the set ofmemory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are
Integers. Design and develop a Program in C that uses Hash function H: K →L as H(K)=K mod m
(remainder method), and implement hashing technique to map a given key K to the address space L.
Resolve the collision (if any) using linear probing.

#include<stdio.h>
#include<stdlib.h>
#define MAX_ADDR 5
int hash (int key);
struct employee
{
int emp_id, emp_age;
char emp_name[25];
}emp[MAX_ADDR];

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

count++;
printf("Successfuly inserted at Actual Address: %d\n\n", index);
break;
}
}
break;
case 2: printf("?Employee details are\n");

Dept. of CSE, KSSEM Page No.67


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

int hash (int key)


{
return key % MAX_ADDR;
}

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

Dept. of CSE, KSSEM Page No.68


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.69


DATA STRUCTURES LABORATORY 18CSL38

Dept. of CSE, KSSEM Page No.70


DATA STRUCTURES LABORATORY 18CSL38

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 CSE, KSSEM Page No.71


DATA STRUCTURES LABORATORY 18CSL38

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


prefix and postfix forms?
Polish and Reverse Polish notations.

8. Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix notations.
Prefix Notation: ^ - * +ABC - DE + FG
Postfix Notation: AB + C * DE - - FG + ^

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


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

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

10.A binary tree with 20 nodes has null branches?


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

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


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

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


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

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


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

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

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


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

(b) Linked list

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

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


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

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

Dept. of CSE, KSSEM Page No.72


DATA STRUCTURES LABORATORY 18CSL38

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

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

21. What are the different Abstract Data Type Groups?


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

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

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

24. What is Strictly Binary Tree?


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

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


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

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

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

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

Dept. of CSE, KSSEM Page No.73


DATA STRUCTURES LABORATORY 18CSL38

27. Difference between array and structure?

Dept. of CSE, KSSEM Page No.74

You might also like