Dsa - Lab - Journal - Write Up
Dsa - Lab - Journal - Write Up
PROGRAM: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>
struct Day {
char *name;
int date;
char *activity;
};
int main() {
struct Day *calendar = (struct Day *)malloc(7 * sizeof(struct Day));
create(calendar);
read(calendar);
display(calendar);
return 0;
}
Output 1
Enter name of day 1: Monday
Enter date of day 1: 1
Enter activity for day 1: pick and speak
Enter name of day 2: Tuesday
Enter date of day 2: 2
Enter activity for day 2: Reading
Enter name of day 3: Wednesday
Enter date of day 3: 3
Enter activity for day 3: writing
Enter name of day 4: Thursday
Enter date of day 4: 4
Enter activity for day 4: gaming
Enter name of day 5: Friday
Enter date of day 5: 5
Enter activity for day 5: playing
Enter name of day 6: Saturday
Enter date of day 6: 6
Enter activity for day 6: sleeping
Enter name of day 7: Sunday
Enter date of day 7: 7
Dept. of CSE Page 2
DATA STRUCTURES LABORATORY BCSL305
Enter activity for day 7: watching movie
Day 2: Tuesday
Date: 2
Activity: Reading
Day 3: Wednesday
Date: 3
Activity: writing
Day 4: Thursday
Date: 4
Activity: gaming
Day 5: Friday
Date: 5
Activity: playing
Day 6: Saturday
Date: 6
Activity: sleeping
Day 7: Sunday
Date: 7
Activity: watching movie
PROGRAM: 2
Design, develop and implement a Program in C for the following
operationsonStrings
a. Read amainString(STR),aPatternString(PAT)andaReplaceString(REP)
b. Perform Pattern Matching Operation: Find and Replace all
occurrencesofPATinSTRwithREPifPATexistsinSTR.Reportsuitablemessagesincas
e PAT doesnotexistinSTR
Support the program with functions for each of the above operations. Don'tuseBuilt-
in functions
#include<stdio.h>
void read();
void match();
char STR[100],PAT[100],REP[100],ANS[100];
int c,i,j,k,m,flag=0;
void main()
{
read();
match();
}
void read()
{
printf("enter the main string STR:"); gets(STR);
printf("enter pattern string PAT:"); gets(PAT);
printf("enter replace string REP:"); gets(REP);
}
void match()
{
c=i=j=k=m=0;
while(STR[c]!='\0')
{
if(STR[m]==PAT[i])
{
i++;m++;
flag=1; if(PAT[i]=='\0')
{
Dept. of CSE Page 4
DATA STRUCTURES LABORATORY BCSL305
for(k=0;REP[k]!='\0';k++,j++)
ANS[j]=REP[k]; i=0;
c=m;
}
}
else
{
ANS[j]=STR[c];
j++;c++;
m=c; i=0;
}
}
if(flag==0)
printf("pattern not found");
else
{
ANS[j]='\0';
printf("resultant string is %s",ANS);
}
}
Output 1
Enter the MAIN string:
KLS VDIT
Enter a PATTERN string:
KLS
Enter a REPLACE string:
CSE
The RESULTANT string is: CSE VDIT
Output 2
Enter the MAIN string:
Hi cse
Enter a PATTERN string: hello
Enter a REPLACE string: kls
Pattern doesn't found!!!
PROGRAM: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 ontoStack
b. Pop an Element fromStack
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>
#define MAX 4
int stack[MAX],top=-1,item;
void push();
void pop();
void palindrome();
void display();
void main()
{
int choice;
while(1)
{
Printf(―------- STACK OPERATIONS \n―);
printf("1.push\n 2.pop\n 3.palindrome\n 4.display\n 5.exit\n");
printf("enter choice");
scanf("%d",&choice);
switch(choice)
{
case 1:push(); break;
case 2:pop();
break;
case 3:palindrome(); break;
case 4:display(); break;
case 5:exit(0); break;
default:printf("invalid choice\n"); break;
}
Dept. of CSE Page 6
DATA STRUCTURES LABORATORY BCSL305
}
}
void push()
{
if(top==MAX-1)
printf("stack overflow");
else
{
printf("enter the item to be pushed\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop()
{
if(top==-1)
printf("stack underflow");
else
{
item=stack[top];
top=top-1;
printf("deleted item is %d",item);
}
}
void display()
{
int i; if(top==-1)
printf("stack is empty");
else
{
for(i=top;i>=0;i--)
printf("%d\t",stack[i]);
}
}
void palindrome()
{
int num[10],i=0,k,flag=1;
Output
------- STACK OPERATIONS ------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 1
Enter element to be inserted 10
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 1
enter element to be inserted 20
#include<stdio.h>
#include<ctype.h>
#define SIZE 50
char s[SIZE];
int top=-1;
void push(char elem)
{
s[++top]=elem;
}
char pop()
{
return s[top--];
}
int pr(char elem)
{
switch(elem)
{
case '#':return 0;
case '(':return 1; case '+':
case '-':return 2; case '*':
case '/':
case '%':return 3;
case '^':return 4;
}
}
void main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
printf("enter the infix expression\n");
gets(infix);
push('#');
Dept. of CSE Page 11
DATA STRUCTURES LABORATORY BCSL305
while((ch=infix[i++])!='\0')
{
if(ch=='(')
push(ch);
else
if(isalnum(ch))
postfix[k++]=ch;
else if(ch==')')
{
while(s[top]!='(')
postfix[k++]=pop();
elem=pop();
}
else
{
while(pr(s[top])>=pr(ch))
postfix[k++]=pop();
push(ch);
}
}
while(s[top]!='#')
postfix[k++]=pop();
postfix[k]='\0';
printf("infix expression is %s\n postfix expression is %s\n",infix,postfix);
}
Output1
enter the Infix Expression ((a+b)*c)
Given Infix Expn is: ((a+b)*c) The Postfix Expn is: ab+c*
Output 2
enter the Infix Expression (a+ (b-c)*d)
Given Infix Expn is: (a+ (b-c)*d) The Postfix Expn is: abc-d*+
Output2
Enter the valid postfix exp 123-4*+
The result is -3.
#include<stdio.h>
Voidtower(intn,charfrompeg,chartopeg,charauxpeg);
intn;
voidmain()
{
printf("Entertheno.ofdiscs:\n");scanf("%d
",&n);
printf("thenumberofmovesintowerofhenoiproblem\n");tower(n,'A','C','B');
}
voidtower(intn,charfrompeg,chartopeg,charauxpeg)
{
if(n==1)
{
printf("movedisk1from%Cto%C\n",frompeg,topeg);return;
}
tower(n-1,frompeg,auxpeg,topeg);
printf("movedisk%dfrom%CtoC\n",n,frompeg,topeg);tower(n-
1,auxpeg,topeg,frompeg);
}
Output
Entertheno.ofdiscs:
3
the number of moves intower ofhenoiproblemMove disc 1
fromAtoC
Move disc 2 from A to BMove
disc 1 from C to BMove disc 3
from A to CMove disc 1 from
B to AMove disc 2 from B to
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
Char q[MAX],item;
int f=0,r=-1,count=0;
void insert();
void delete();
void display();
void main()
{
int ch;
while(1)
{
printf("1.insert 2.delete 3.display 4.exit \n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:getchar();insert(); break;
case 2:delete(); break;
case 3:display(); break;
case 4:exit(0);
default :printf("Invalid choice\n"); break;
}
}
Output
PROGRAM 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 and Deletion at End of SLL
d.Perform Insertion and Deletion at Front of SLL
eExit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void create();
void insert_front();
void insert_rear();
void display();
void delete_front();
void delete_rear();
int count=0;
struct node
{
char usn[20],name[50],branch[10];
int sem;
unsigned long long int phno;
struct node *link;
};
struct node *first=NULL,*last=NULL,*temp=NULL,*p;
void main()
{
int ch,n,i;
while(1)
{
printf("1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear
7.exit\n");
printf("enter choice\n");
}
else if(p->link==NULL)
{
printf("deleted node is %s\t%s\t%s\t%d\t%llu\n",p->usn,p->name,p->branch,p->sem,p->phno);
free(p);
first=NULL; count--;
}
else
{
first=p->link;
printf("deleted node is %s\t%s\t%s\t%d\t%llu\n",p->usn,p->name,p->branch,p->sem,p->phno);
free(p);
count--;
}
}
void delete_rear()
{
p=first;
if(first==NULL)
{
printf("list is empty\n");
}
else if(p->link==NULL)
{
printf("deleted node is %s\t%s\t%s\t%d\t%llu\n",p->usn,p->name,p->branch,p->sem,p->phno);
free(p);
first=NULL; count--;
}
else
{
while(p->link!=last) p=p->link;
Output
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice1
enter the no.of students 2
enter usn,name,branch,sem,phno
2vd16cs024deepak cs39481830624
enter usn,name,branch,sem,phno
2vd17cs405yogesh cs49449323284
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice4
content of list is
2vd17cs405 yogesh cs 4 9449323284
2vd16cs024 deepak cs 3 9481830624
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice4
content of list is
2vd15cs011 suresh cs 6 9481830624
2vd17cs405 yogesh cs 4 9449323284
2vd16cs024 deepak cs 3 9481830624
total no.of students 3
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice5
deleted node is 2vd15cs011 suresh cs 6 9481830624
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice6
deleted node is monesh vinay cs 8 9481830624
1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice7
PROGRAM 8
Design, Develop and Implement a menu driven Program in C for the following operations
on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation,Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void create();
void insert_front();
void insert_rear();
void display();
void delete_front();
void delete_rear();
int count=0;
struct node
{
int ssn;
char name[50],dept[20],desg[20];
float sal;
unsigned long long int phno;
struct node*llink,*rlink;
};
struct node *first=NULL,*last=NULL,*temp;
void main()
{
int ch,n,i;
while(1)
{
Dept. of CSE Page 24
DATA STRUCTURES LABORATORY BCSL305
printf("1.create\n 2.insert_front\n 3.insert_rear\n 4.display\n 5.delete_front\n 6.delete_rear\n
7.exit\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the number of employee\n");
scanf("%d",&n);
for(i=0;i<n;i++)
insert_rear(); break;
case 2:insert_front();break;
case 3:insert_rear();break;
case 4:display();break;
case 5:delete_front();break;
case 6:delete_rear();break;
case 7:exit(0);
default:printf("invalid choice\n");break;
}
}
}
void create()
{
int ssn;
char name[50],dept[20],desg[20]; float sal;
unsigned long long int phno;
temp=(struct node*)malloc(sizeof(struct node)); temp->llink=temp->rlink=NULL;
printf("enter ssn,name,dept,desg,salaryand phno\n");
scanf("%d%s%s%s%f%llu",&ssn,name,dept,desg,&sal,&phno);
temp->ssn=ssn;
strcpy(temp->name,name); strcpy(temp->dept,dept);
strcpy(temp->desg,desg); temp->sal=sal;
temp->phno=phno; count++;
}
void insert_front()
{
if(first==NULL)
{
create();
first=temp;
void display()
{
struct node *p;
if(first==NULL)
{
printf("list is empty\n");
return;
}
p=first;
printf("contents of list\n");
while(p!=NULL)
{
void delete_front()
{
struct node *p; if(first==NULL)
{
printf("list is empty,cannot delete\n");
}
else if(first->rlink==NULL)
{
printf("deleted data is %d\t%s\t%s\t%s\t%f\t%llu\n",first->ssn,first->name,first->dept,first-
>desg,first->sal,first->phno);
first=NULL;
free(first); count--;
}
else
{
p=first; first=p->rlink;
printf("deleted data is %d\t%s\t%s\t%s\t%f\t%llu\n",p->ssn,p->name,p->dept,p->desg,p->sal,p-
>phno);
free(p);
count--;
}
}
void delete_rear()
{
struct node*p;
if(first==NULL)
{
printf("list is empty,cannot delete\n");
}
else if(first->rlink==NULL)
{
printf("deleted data is %d\t%s\t%s\t%s\t%f\t%llu\n",first->ssn,first->name,first-
>dept,first->desg,first->sal,first->phno);
Output
1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit
enter choice 1
enter the number of employee 2
enter ssn,name,dept,desg,salary and phno 120
yogesh csprogramer 140009481830624
contents of list
111 suresh cs lecturer 40000.000000 9482230624
120 yogesh cs programer 14000.000000 9481830624
110 vinay cs programer 14000.000000 9481830625
222 naveen cs lecturer 50000.000000 9482230677
total no. of employee 4
enter choice 4
contents of list
120 yogesh cs programer 14000.000000 9481830624
110 vinay cs programer 14000.000000 9481830625
total no. of employee 2
enter choice 7
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)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int co,ex,ey,ez; struct node *link;
};
typedef struct node NODE;
NODE *createnode(int,int,int,int);
NODE *attachnode(NODE*,NODE*);
NODE *readpoly();
void display(NODE*);
void evaluate(NODE*);
NODE *addpoly(NODE*,NODE*,NODE*);
NODE *createnode(int co,int ex,int ey,int ez)
{
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE)); temp->co=co;
temp->ex=ex; temp->ey=ey; temp->ez=ez; temp->link=NULL; return temp;
}
poly=attachnode(temp,poly); b=b->link;
}
return poly;
}
Void main()
{
Output
1.represent and evaluate 2.add two polynomial 3.exit
enter choice1
enter a polynomial
enter the number of terms 5
term 1
enter the coefficient 6
enter exponent values of x,yand z 221
term 2
enter the coefficient-4
enter exponent values of x,yand z 015
term 3
enter the coefficient 3
term 4
enter the coefficient 2
enter exponent values of x,yand z 151
term 5
enter the coefficient 2
enter exponent values of x,yand z 113
6x^2y^2z^1+-4x^0y^1z^5+3x^3y^1z^1+2x^1y^5z^1+2x^1y^1z^3+
#include <stdio.h>
#include <stdlib.h>
int flag=0;
typedef struct BST
{
int data;
struct BST *lchild, *rchild;
}node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node * *);
void main()
{
int choice; int ans =1;
int key;
node *new_node, *root, *tmp, *parent; node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:do{
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL)
root = new_node; else
insert(root, new_node);
printf("\nWant To enter More Elements?(1/0)");
scanf("%d",&ans);
}while (ans); break;
case 2:printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
if(flag==1)
{
printf("\nParent of node %d is %d", tmp->data, parent->data);
}
else
{
printf("\n The %d Element is not Present",key);
}
flag=0; break;
case 3:if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display :\n "); inorder(root);
printf("\nThe Preorder display : \n"); preorder(root);
printf("\nThe Postorder display : \n"); postorder(root);
}
break;
}
} while (choice != 4);
}
node *get_node()
{
node *temp; temp = (node *)malloc(sizeof(node));
temp->lchild = NULL; temp->rchild = NULL; return temp;
}
Output
#include<stdio.h>
#include<stdlib.h>
int n,a[10][10],i,j,source,s[10],choice,count;
void bfs(int n,int a[10][10],int source,int s[])
{
int q[10],u;
int front=1,rear=1;
s[source]=1;
q[rear]=source;
while(front<=rear)
{
u=q[front]; front=front+1;
for(i=1;i<=n;i++)
if(a[u][i]==1 &&s[i]==0)
{
rear=rear+1; q[rear]=i;
s[i]=1;
}
}
}
void dfs(int n,int a[10][10],int source,int s[])
{
s[source]=1; for(i=1;i<=n;i++)
if(a[source][i]==1 && s[i]==0) dfs(n,a,i,s);
}
int main()
{
printf("Enter the number of nodes : \n");
scanf("%d",&n);
printf("\n Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
1.BFS2.DFS3.Exit
enter your choice 2
Output2
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 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 100
void display(int a[MAX]);
int create(int num);
void linearprob(int a [MAX],int key,int num);
void main()
{
int a[MAX],i,num,key,ans=1;
printf("collission hanmdling by linear probing\n");
for(i=0;i<MAX;i++)
a[i]= -1;
do{
printf("enter the data\n");
scanf("%4d",&num);
key=create(num);
linearprob(a,key,num);
printf("do yuou want to comntinue[1/0]\n");
scanf("%d",&ans);
}while(ans);
display(a);
}
int create(int num)
{
int key; key=num%100; return key;
}
void linearprob(int a[MAX],int key, int num)
{
Dept. of CSE Page 43
DATA STRUCTURES LABORATORY BCSL305
int flag=0,count=0,i;
if(a[key]==-1)
a[key]=num;
else
{
printf("\n collision deleted\n");
i=0;
while((i<key)&&(flag==0))
{
if(a[i]==-1)
{
a[i]=num; flag=1; break;
} i++;
}
}
}
void display(int a[MAX])
{
int ch,i;
printf("\n 1.display all 2.filtered display\n");
printf("enter choice\n");
scanf("%d",&ch); if(ch==1)
{
for(i=0;i<MAX;i++)
printf("%d\t %d\n",i,a[i]);
}
else
{
for(i=0;i<MAX;i++)
{
if(a[i]!=-1)
{
printf("%d\t %d\n",i,a[i]); continue;
}
}
}
}
output
collision handling by linear probing : Enter the data
1234
Do you wish to continue ? (1/0) 1
Enter the data 2548
Do you wish to continue ? (1/0) 1
Enter the data 3256
Do you wish to continue ? (1/0) 1
Enter the data 1299
Do you wish to continue ? (1/0) 1
Enter the data 1298
Do you wish to continue ? (1/0) 1
Enter the data 1398
Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING Do you wish to continue ? (1/0)
0
VirtualLabExperiments
EXPERIMENT:01
Linked List
Linked list is a linear data structure where each data is a separate object (of same data type). Linked list
objects do not occupy the contiguous memory location as compared to the array which is also a linear
data structure where elements have contiguous memory allocation; instead linked lists are linked using
pointers. Elements of the linked lists are known as Nodes.
Steps of simulator:
Simulation Link:https://ds1-iiith.vlabs.ac.in/exp/linked-list/singly-linked-list/sllpractice.html
A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an
associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree
additionally satisfies the binary search property, which states that the key in each node must be greater
than or equal to any key stored in the left sub-tree, and less than or equal to any key stored in the right
sub-tree. The leaves (final nodes) of the tree contain no key and have no structure to distinguish them
from one another.
Steps of simulator: