DS Manual - 22scheme - CSBS
DS Manual - 22scheme - CSBS
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:
Department of Computer Science and Engineering shall,
l Provide good infrastructure and facilitate learning to become competent
engineers who meet global challenges.
l Encourages industry institute interaction to give an edge to the students.
l Facilitates experimental learning through interdisciplinary projects.
l Strengthen soft skill to address global challenges.
K.S. SCHOOL OF ENGINEERING AND MANAGEMENT
# 15, Mallasandra, off Kanakapura Road, Bengaluru-560109
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
INDEX
Sl No Programs List Page
No.
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having three fields.
1
The first field is the name of the Day (A dynamically allocated String), The second field
is the date of the Day (A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data from the
keyboard and to print weeks activity details report on screen.
2. Design, Develop and Implement a Program in C for the following operations on Strings.
a) Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b) Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not 5
exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.
3. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a) Push an Element on to Stack
b) Pop an Element from Stack
c) Demonstrate how Stack can be used to check Palindrome 8
d) Demonstrate Overflow and Underflow situations on Stack
e) Display the status of Stack
f) Exit
Support the program with appropriate functions for each of the above operations
4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
14
operands.
5. Design, Develop and Implement a Program in C for the following Stack Applications
a) Evaluation of Suffix expression with single digit operands and operators: +, -, *, /,
%,^ 17
b) Solving Tower of Hanoi problem with n disks
6. Design, Develop and Implement a menu driven Program in C for the following operations
on Circular QUEUE of Characters (Array Implementation of Queue with maximum size
MAX)
a) Insert an Element on to Circular QUEUE 21
b) Delete an Element from Circular QUEUE
c) Demonstrate Overflow and Underflow situations on Circular QUEUE
d) Display the status of Circular QUEUE
e) Exit
Support the program with appropriate functions for each of the above operations
7. Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name,
Programme, Sem, PhNo
a) Create a SLL of N Students Data by using front insertion. 26
b) Display the status of SLL and count the number of nodes in it
c) Perform Insertion / Deletion at End of SLL
d) Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e) Exit
8. Design, Develop and Implement a menu driven Program in C for the following operations
on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it 34
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
9. Design, Develop and Implement a Program in C for the following operations on
Singly Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store 43
theresult in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
10. Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree (BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order 52
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
11. Design, Develop and Implement a Program in C for the following operations on Graph(G)of
Cities
a. Create a Graph of N cities using Adjacency Matrix. 61
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Design and develop a Program in C that uses 65
Hashfunction H: K ->L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.
13. Viva Questions 70
DATA STRUCTURES LABORATORY
SEMESTER – III
2. Design, Develop and Implement a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.
3. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized expressions
with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,
^
b. Solving Tower of Hanoi problem with n disks
6. Design, Develop and Implement a menu driven Program in C for the following operations on Circular
QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
7. Design, Develop and Implement a menu driven Program in C for the following operations on Singly
Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem,
PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
8. Design, Develop and Implement a menu driven Program in C for the following operations on Doubly
Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
9. Design, Develop and Implement a Program in C for the following operationson Singly Circular
Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
10. Design, Develop and Implement a menu driven Program in C for the following operations on Binary
Search Tree (BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
11. Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the
records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory
locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Design and develop a Program in C that uses Hash function H: K L as
H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using linear
probing.
Laboratory Outcomes: The student should be able to:
Analyze and Compare various linear and non-linear data structures
Demonstrate the working nature of different types of data structures and their applications.
Use appropriate searching and sorting algorithms for the give scenario.
Apply the appropriate data structure for solving real world problems
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;
}
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:
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>
void readstr(void);
void readsearchstr(void);
void readreplacestr(void);
void matching(char str[100]);
char mainstr[100],pat[100],replace[100],ans[100];
void main()
{
readstr();
readsearchstr();
readreplacestr();
matching(mainstr);
}
void readstr(void)
{
printf("\n enter a string\n");
gets(mainstr);
}
void readsearchstr(void)
{
printf("enter a search string\n");
gets(pat);
}
void readreplacestr(void)
{
printf("\n enter a replace string\n");
gets(replace);
}
void matching(char mainstr[100])
{
char ans[100];
int i,j,c,m,k,flag;
i=m=c=j=flag=0;
while(mainstr[c]!='\0')
{
if(mainstr[m]==pat[i])
{
i++;
m++;
if(pat[i]=='\0')
Dept of CS&BS,KSSEM Page 5
DATA STRUCTURES LABORATORY BCSL305
{
for(k=0;replace[k]!='\0';k++,j++)
ans[j]=replace[k];
i=0;
c=m;
}
}
else
{
flag=1;
ans[j]=mainstr[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
if(flag==0)
{
printf("search string is not present\n");
printf("the resultant string is %s",ans);
}
else
printf("the resultant string is %s\n",ans);
}
#Run
enter a string
kammavari sangah is the institution name.
enter a search string
kssem
#Run
enter a string
ks group are starting new colleges in bangalore and hyderabad
enter a search string
hyderabad
3. Design, Develop and Implement a menu driven Program in C for the following operations on STACK
of integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 5
int stack[MAX],top=-1;
char stack1[MAX],top1=-1;
void push()
{
int ele;
if(top<MAX-1)
{
printf("enter the value to be inserted into the stack:\n");
scanf("%d",&ele);
stack[++top]=ele;
}
else
printf("\n stack is full\n");
return;
}
void pop()
{
if(top!=-1)
{
printf("\n the element deleted from the stack is:%d\n",stack[top--]);
}
else
printf("\n stack is empty\n");
return;
}
void palindrome()
{
int i,count=0,len;
char str[100];
printf("\n entertring to check whether it is palindrome or not:");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
Dept of CS&BS, KSSEM Page 8
DATA STRUCTURES LABORATORY BCSL305
{
stack1[++top1]=str[i];
}
for(i=0;i<len;i++)
{
if(str[i]==stack1[top1--]) count++;
}
if(count==len)
printf("\n %s is a palindrome string\n",str);
else
printf("\n %s is not a palindrome string\n",str);
return;
}
void check()
{
if(top>=MAX-1)
printf("stack is overflow\n");
else if(top==-1)
printf("stack is underflow\n");
else
printf("stack operation can be performed\n");
}
void display()
{
int i;
if(top==-1)
{
printf("\n stack is empty\n");
}
else
{
printf("\n elements in the stack are\n");
for(i=top;i>=0;--i)
printf("|%d|\n",stack[i]);
}
return;
}
void main()
{
int choice;
while(1)
{
printf("\n STACK OPERATIONS\n");
printf("\n 1:push\t2:pop\t3:palindrome\t4:check\t5:display\t6:exit\n");
printf("\n enter your choice[1/2/3/4/5/6]:");
scanf("%d",&choice); switch(choice)
{
case 1: push();break;
case 2: pop();break;
case 3: palindrome();break;
case 4: check();break;
Dept of CS&BS, KSSEM Page 9
DATA STRUCTURES LABORATORY BCSL305
case 5: display();break;
case 6: exit(0);
default : printf("invaild choice\n");
}
}
return;
}
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
Dept of CS&BS, KSSEM Page 11
DATA STRUCTURES LABORATORY BCSL305
STACK OPERATIONS
stack is full
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
stack is empty
STACK OPERATIONS
stack is empty
STACK OPERATIONS
STACK OPERATIONS
STACK OPERATIONS
STACK 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.
#include <stdio.h>
char s[SIZE];
5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
#include<stdio.h>
#include<math.h>
#include<ctype.h>
int top=-1;
float s[50];
float pop()
{
return(s[top--]);
}
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);
}
#include<stdio.h>
void towers(int n,char source,char dest,char auxilary);
void main()
{
int n;
printf("enter the number of disk\");
scanf("%d",&n);
towers(n,'A','C','B');
}
void towers(int n,char source,char dest,char auxilary)
{
if(n==0)
return;
towers(n-1,source,auxilary,dest);
printf("move disk %d from %c to %c \",n,source,dest);
towers(n-1,auxilary,dest,source);
}
#Run
enter the number of disk
5
move disk 1 from A to C
move disk 2 from A to B
move disk 1 from C to B
move disk 3 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C
move disk 4 from A to B
move disk 1 from C to B
move disk 2 from C to A
move disk 1 from B to A
move disk 3 from C to B
move disk 1 from A to C
move disk 2 from A to B
Dept of CS&BS, KSSEM Page 19
DATA STRUCTURES LABORATORY 18CSBSL3
move disk 1 from C to B 8
move disk 5 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C
move disk 3 from B to A
move disk 1 from C to B
move disk 2 from C to A
move disk 1 from B to A
move disk 4 from B to C
move disk 1 from A to C
move disk 2 from A to B
move disk 1 from C to B
move disk 3 from A to C
move disk 1 from B to A
move disk 2 from B to C
move disk 1 from A to C
6. Design, Develop and Implement a menu driven Program in C for the following operations
on
Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define size 5
int CQ[size],f=-1,r=-1;
int CQfull()
{
if((f==r+1)||(f==0&&r==size-1))
return 1;
return 0;
}
int CQempty()
{
if(f==-1)
return 1;
return 0;
}
CQinsert(int ele)
{
if(CQfull())
printf("\n overflow!!!\n");
else
{
if(f==-1)
f=0;
r=(r+1)%size;
CQ[r]=ele;
}
}
int CQdelete()
{
int ele;
if(CQempty())
{
printf("\n underflow!!!\n");
return(-1);
}
else
{
ele=CQ[f];
Dept of CS&BS, KSSEM Page 21
DATA STRUCTURES LABORATORY 18CSL38
if(f==r)
{
f=-1;
r=-1;
}
else
f=(f+1)%size;
return(ele);
}
}
display()
{
int i;
if(CQempty())
printf("\n empty queue\n");
else
{
printf("front[%d]->",f);
for(i=f;i!=r;i=(i+1)%size)
printf(" %d ",CQ[i]);
printf(" %d ",CQ[i]);
printf("<-[%d]rear",r);
}
}
void main()
{
int opn,ele;
do
{
printf("\n \n Circular queue operations\n");
printf("\n press 1-insert,2-delete,3-display,4-exit\n"); printf("\n your option? ");
scanf("%d",&opn); switch(opn)
{
case 1:printf("\n read the element to be inserted ?\n");
scanf("%d",&ele);
CQinsert(ele);
break;
case 2:ele=CQdelete();
if(ele!=-1)
printf("\n delete element is %d\n",ele); break;
case 3:printf("\n status of circular queue \n\n");
display();
break;
case 4:exit(0);
default:printf("invalid option\n");
}
}
while(opn!=4);
}
press 1-insert,2-delete,3-display,4-exit
your option? 3
empty queue
press 1-insert,2-delete,3-display,4-exit
your option? 1
press 1-insert,2-delete,3-display,4-exit
your option? 1
press 1-insert,2-delete,3-display,4-exit
your option? 1
press 1-insert,2-delete,3-display,4-exit
your option? 1
your option? 1
press 1-insert,2-delete,3-display,4-exit
your option? 1
overflow!!!
press 1-insert,2-delete,3-display,4-exit
your option? 3
front[0]-> 34 37 39 43 47 <-[4]rear
press 1-insert,2-delete,3-display,4-exit
your option? 2
delete element is 34
press 1-insert,2-delete,3-display,4-exit
your option? 2
delete element is 37
press 1-insert,2-delete,3-display,4-exit
your option? 2
delete element is 39
your option? 2
delete element is 43
press 1-insert,2-delete,3-display,4-exit
your option? 2
delete element is 47
press 1-insert,2-delete,3-display,4-exit
your option? 2
underflow!!!
press 1-insert,2-delete,3-display,4-exit
your option? 4
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>
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);
}
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);
void del_end()
{
node *cur,*pre;
if(start==NULL)
{
printf("List is empty\
n"); return;
}
cur=start;
pre=NULL;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
if(pre==NULL)
start=NULL;
else
pre->next=NULL;
printf("The node of student usn %s is deleted\n",cur->usn);
free(cur);
}
void main()
{
int
ch; for(;;)
{
printf("\n 1: Create SLL \n 2. Insert Front \n 3: Insert End \n 4: Delete Front \n 5: Delete End \n
6:Display\n 7: Exit \n");
printf("Enter you are
choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: createSLL();break;
case 2:insert_front();break;
case 3: insert_end(); break;
case 4: del_front();break;
case 5: del_end();break;
case 6: display();break;
case 7: exit(0);
default : printf("Invalid option\
n"); exit(0);
}
}
}
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
List Empty
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
1
Enter the number of students to be created
3
Enter the student
Usn
Name
Branch
Semester
Phone
43
Rama
CS&BS
3
66221234
Enter the student
Usn
Name
Branch
Semester
Phone
45
Raj
CS&BS
3
66221234
Enter the student
Usn
Name
Branch
Semester
Dept of CS&BS, KSSEM Page 29
DATA STRUCTURES LABORATORY 18CSL38
Phone
46
Prince
CS&BS
3
66221234
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
3
Enter the student
Usn
Name
Branch
Semester
Phone
42
Ved
CS&BS
3
66221234
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
2
Enter the student
Usn
Name
Branch
Semester
Phone
47
Paru
CS&BS
3
66221234
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
4
The node of student usn 47 is deleted
1: Create SLL
2. Insert Front
3: Insert End
4: Delete Front
5: Delete End
6:Display
7: Exit
Enter you are choice
6
1: Create SLL
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
#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;
};
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 displaycount(void)
{
NODE temp1=first;
int count=1;
if(temp1==NULL)
printf("[!] List is empty\n");
else
{
printf("SSN\tName\tDEPT\tDESIG\tPhone\tSalary\n");
printf(" -\t- \t -\t-\t \t \n");
while(temp1!=end)
{
count++;
printf("%s\t%s\t%s\t%s\t%s\t%ld\n",temp1->ssn,temp1->name, temp1->dept,temp1->desg,temp1->phno,
temp1->sal);
temp1=temp1->rlink;
}
printf("%s\t%s\t%s\t%s\t%s\t%ld\n",temp1->ssn,temp1->name,temp1->dept,temp1->desg,temp1->phno,
temp1->sal);
printf("\nCount:%d\n",count);
}
}
void insertfront(void)
{
printf("Enter Details\n");
Dept of CS&BS, KSSEM Page 35
DATA STRUCTURES LABORATORY 18CSL38
read();
if(first==NULL)
first=temp;
else
{
temp->rlink = first;
first->llink = temp;
first=temp;
}
}
void insertend(void)
{
printf("Enter Details\n");
temp=getnode();
temp->llink = temp->rlink = NULL;
read();
if(first==NULL)
first = end = temp;
else
{
end->rlink = temp;
temp->llink = end;
end = temp;
}
}
void deletefront(void)
{
NODE temp2;
if(first==NULL)
printf("[!] List is empty\n");
else if(first == end)
{
temp2 = first;
printf("[+] Deleted record %s\n",temp2->ssn);
first=end=NULL;
}
else
{
temp2= first;
printf("[+] Deleted record %s\n",temp2->ssn);
first = first->rlink;
temp2->llink = NULL;
free(temp2);
}
}
void main(void)
{
int ch;
while(1)
{
printf("\nMENU\n \n[1] Create list\n[2] Display list\n[3] Insert front\n[4] Insert end\n[5] Deleted front\n[6]
Delete rear\n[7] Exit\n");
printf("\nEnter the choice: ");
scanf("%d",&ch); switch(ch)
{
case 1: create_dll();break;
case 2: displaycount(); break;
case 3: insertfront();break;
case 4: insertend();break;
case 5: deletefront(); break;
case 6: deleteend();break;
case 7: exit(0);
default:printf("invalid choice\n");
}
}
}
MENU
MENU
MENU
MENU
Count:3
MENU
MENU
Count:2
MENU
MENU
Count:1
MENU
Count:2
MENU
MENU
MENU
#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 45
DATA STRUCTURES LABORATORY 18CSL38
int x,y,z;
float sum=0.0;
e1=getnode();
e1->link=e1;
printf("enter the polynomail\n");
e1=read_poly(e1);
printf("polynomial is\n");
display(e1);
printf("enter the values of coefficient\n");
scanf("%d%d%d",&x,&y,&z);
if(e1==NULL)
{
printf("list is empty");
}
else
{
temp=e1->link;
while(temp!=e1)
{
sum+=temp->cf*pow(x,temp->px)*pow(y,temp->py)*pow(z,temp->pz);
temp=temp->link;
}
printf("the total sum is %f\n",sum);
}
return;
}
void main()
{
int choice;
while(1)
{
#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;
}
Dept of CS&BS, KSSEM Page No.52
DATA STRUCTURES LABORATORY 18CSL38
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
}
void pre(NODE PRE)
{
if(PRE!=NULL)
{
printf("%d\t",PRE->info);pre(PRE->llink); pre(PRE->rlink);
}
return;
}
void in(NODE IN)
{
if(IN!=NULL)
{
in(IN->llink); printf("%d\t",IN->info);in(IN->rlink);
}
return;
}
void post(NODE POST)
{
if(POST!=NULL)
{
post(POST->llink);
post(POST->rlink);
printf("%d\t",POST->info);
}
return;
}
void traversal(NODE root)
{
NODE IN,PRE,POST;
IN=root;
PRE=root;
POST=root;
if(root==NULL)
{
printf("emty tree");
return;
}
printf("preorder traversal:
"); pre(PRE);
printf("\ninorder traversal: ");
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: 2
preorder traversal: 2 8 5 2 15 24
inorder traversal: 2 2 5 8 15 24
post order traversal: 2 5 24 15 8 2
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 14
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 7
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 8
1 create bst
2 traverse
Dept of CS&BS, KSSEM Page No.56
DATA STRUCTURES LABORATORY 18CSL38
3 search
4 exit
Enter choice: 2
preorder traversal: 2 8 5 2 7 15 14 8 24
inorder traversal: 2 2 5 7 8 8 14 15 24
post order traversal: 2 7 5 8 14 24 15 8 2
1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 24
key found
1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 3
key not found
1 create bst
2 traverse
3 search
4 exit
Enter choice: 4
#Run
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 34
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 15
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 8
1 create bst
2 traverse
3 search
4 exit
Enter choice: 1
Enter item to be inserted: 5
1 create bst
2 traverse
3 search
4 exit
Enter choice: 2
preorder traversal: 34 9 8 5 15
inorder traversal: 5 8 9 15 34
post order traversal: 5 8 15 9 34
1 create bst
2 traverse
Dept of CS&BS, KSSEM Page No.58
DATA STRUCTURES LABORATORY 18CSL38
3 search
4 exit
Enter choice: 3
enter element to be searched: 15
key found
1 create bst
2 traverse
3 search
4 exit
Enter choice: 3
enter element to be searched: 2
key not found
1 create bst
2 traverse
3 search
4 exit
Enter choice: 4
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;
Dept of CS&BS, KSSEM Page No.64
DATA STRUCTURES LABORATORY 18CSL38
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 :
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.
(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.