0% found this document useful (0 votes)
46 views48 pages

Dsa - Lab - Journal - Write Up

Uploaded by

sumukhchavan6
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)
46 views48 pages

Dsa - Lab - Journal - Write Up

Uploaded by

sumukhchavan6
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/ 48

DATA STRUCTURES LABORATORY BCSL305

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;
};

void create(struct Day *calendar) {


for (int i = 0; i < 7; i++) {
(calendar + i)->name = (char *)malloc(20 * sizeof(char));
(calendar + i)->activity = (char *)malloc(100 * sizeof(char));
}
}

void read(struct Day *calendar) {


for (int i = 0; i < 7; i++) {
printf("Enter name of day %d: ", i + 1);
scanf("%s", (calendar + i)->name);
printf("Enter date of day %d: ", i + 1);
scanf("%d", &(calendar + i)->date);
printf("Enter activity for day %d: ", i + 1);
scanf(" %[^\n]s", (calendar + i)->activity);
}
}

void display(struct Day *calendar) {


printf("\nWeekly Activity Details:\n");
for (int i = 0; i < 7; i++) {

Dept. of CSE Page 1


DATA STRUCTURES LABORATORY BCSL305
printf("Day %d: %s\n", i + 1, (calendar + i)->name);
printf("Date: %d\n", (calendar + i)->date);
printf("Activity: %s\n\n", (calendar + i)->activity);
}
}

int main() {
struct Day *calendar = (struct Day *)malloc(7 * sizeof(struct Day));
create(calendar);
read(calendar);
display(calendar);

// Free dynamically allocated memory


for (int i = 0; i < 7; i++) {
free((calendar + i)->name);
free((calendar + i)->activity);
}
free(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

Weekly Activity Details:


Day 1: Monday
Date: 1
Activity: pick and speak

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

Dept. of CSE Page 3


DATA STRUCTURES LABORATORY BCSL305

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!!!

Dept. of CSE Page 5


DATA STRUCTURES LABORATORY BCSL305

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;

Dept. of CSE Page 7


DATA STRUCTURES LABORATORY BCSL305
k=top;
while(k!=-1)
num[i++]=stack[k--];
for(i=0;i<=top;i++)
{
if(num[i]==stack[i])
continue;
else
flag=0;
}
if(top==-1)
printf("stack is empty");
else
{
if(flag)
printf("palindrome");
else
printf("not a palindrome");
}
}

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

Dept. of CSE Page 8


DATA STRUCTURES LABORATORY BCSL305
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit

Enter your choice 1


enter element to be inserted 30
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 1
enter element to be inserted 40
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 1
enter element to be inserted 50
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 1 Stack Overflow:
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 4

Dept. of CSE Page 9


DATA STRUCTURES LABORATORY BCSL305

stack elements are:


50 40 30 20 10
------- STACK OPERATIONS------
1.Push 2.Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 2 The poped element: 50
------- STACK OPERATIONS------
1.Push 2.Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 2 The poped element: 40
------- STACK OPERATIONS------
1.Push 2.Pop
3. Palindrome
4. Display
5. Exit
Enter the choice 2
The poped element: 30
------- STACK OPERATIONS------
1. Push
2. Pop
3. Palindrome
4. Display
5. Exit
Enter your choice 2 The poped element: 20

Dept. of CSE Page 10


DATA STRUCTURES LABORATORY BCSL305
PROGRAM 4

Design, develop and implement a Program in C for converting an Infix Expression


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

#include<stdio.h>
#include<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*+

Dept. of CSE Page 12


DATA STRUCTURES LABORATORY BCSL305
PROGRAM 5:
Design, develop and implement a Program inC for the following Stack Applications
a.Evaluation of Suffixexpression withsingle digit operands and operators: +, -, *, /,%, ^
b.Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<math.h>
#include<string.h>
int s[30],op1,op2;
int top=-1,i;
char p[30],sym;
int op(int op1,char sym,int op2){
switch(sym){
case '+':return op1+op2;
case '-':return op1-op2;
case '*':return op1*op2;
case '/':return op1/op2;
case '%':return op1%op2;
case '^':
case '$':return pow(op1,op2);}
return 0;
}
int main(){
printf("\nEnter the valid postfix exp:");
scanf("%s",p);
for(i=0;i<strlen(p);i++)
{
sym=p[i];
if(sym>='0' &&sym<='9')
s[++top]=sym-'0';
else
{
op2=s[top--];
op1=s[top--];
s[++top]=op(op1,sym,op2);
}
}
printf("\nThe result is %d",s[top]);

Dept. of CSE Page 13


DATA STRUCTURES LABORATORY BCSL305
Output1
Enter the valid postfix exp: 23+
The result is 5

Output2
Enter the valid postfix exp 123-4*+
The result is -3.

5b.Solving Tower of Hanoi problem with n disks

#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

Dept. of CSE Page 14


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

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
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;

}
}

Dept. of CSE Page 15


DATA STRUCTURES LABORATORY BCSL305
}
void insert()
{
if(count==MAX) printf("queue overflow\n");
else
{
printf("enter the item to be inserted\n");
scanf("%c",&item );
r=(r+1)%MAX;
q[r]=item; count++;
}
}
void delete()
{
if(count==0)
printf("queue underflow\n"); else
{
printf("deleted item is %c\n",q[f]);
f=(f+1)%MAX;
count--;
}
}
void display()
{
int j=f,i;
if(count==0) printf("queue is empty\n");
else
{
printf("contents of circular queue\n");
for(i=1;i<=count;i++)
{
printf("%c\t",q[j]);
j=(j+1)%MAX;
}
printf("total number of items=%d\n",count);
}
}

Dept. of CSE Page 16


DATA STRUCTURES LABORATORY BCSL305

Output

1.insert 2.delete 3.display 4.exit enter choice1


enter the item to be inserted A
1.insert 2.delete 3.display 4.exit enter choice1
enter the item to be inserted B
1.insert 2.delete 3.display 4.exit enter choice1
enter the item to be inserted C
1.insert 2.delete 3.display 4.exit enter choice1
enter the item to be inserted D
1.insert 2.delete 3.display 4.exit enter choice1
enter the item to be inserted E
1.insert 2.delete 3.display 4.exit enter choice1
queue overflow

1.insert 2.delete 3.display 4.exit enter choiceF


queue overflow
1.insert 2.delete 3.display 4.exit enter choice3
contents of circular queue
AB C D E
total number of items=5
1.insert 2.delete 3.display 4.exitenter choice 2
deleted item is A
1.insert 2.delete 3.display 4.exit enter choice2
deleted item is B
1.insert 2.delete 3.display 4.exit enter choice2
deleted item is C
1.insert 2.delete 3.display 4.exit enter choice2
deleted item is D
1.insert 2.delete 3.display 4.exit enter choice2
deleted item is E
1.insert 2.delete 3.display 4.exit enter choice2
queue underflow
1.insert 2.delete 3.display 4.exit enter choice4

Dept. of CSE Page 17


DATA STRUCTURES LABORATORY BCSL305

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");

Dept. of CSE Page 18


DATA STRUCTURES LABORATORY BCSL305
scanf("%d",&ch);
switch(ch)
{

case 1:printf("enter the no.of students\n");


scanf("%d",&n);
for(i=1;i<=n;i++)
insert_front(); 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()
{
char usn[20],name[50],branch[10]; int sem;
unsigned long long int phno;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter usn,name,branch,sem,phno\n");
scanf("%s%s%s%d%llu",usn,name,branch,&sem,&phno);
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem=sem;
temp->phno=phno;
count++;
}
void insert_front()
{
if(first==NULL)
{
create();
temp->link=NULL;
first=temp;

Dept. of CSE Page 19


DATA STRUCTURES LABORATORY BCSL305
last=temp;
}
else
{
create();
temp->link=first;
first=temp;
}
}
void insert_rear()
{
if(first==NULL)
{
create();
temp->link=NULL; first=temp; last=temp;
}
else
{
create();
temp->link=NULL;
last->link=temp;
last=temp;
}
}
void display()
{
if(first==NULL)
{
printf("list is empty\n");
}
else
{
p=first;
printf("content of list is\n"); while(p!=NULL)
{
printf("%s\t%s\t%s\t%d\t%llu\n",p->usn,p->name,p->branch,p->sem,p->phno);
p=p->link;
}
printf("total no.of students %d\n",count);

Dept. of CSE Page 20


DATA STRUCTURES LABORATORY BCSL305
}
}
void delete_front()
{
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
{
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;

Dept. of CSE Page 21


DATA STRUCTURES LABORATORY BCSL305
printf("deleted node is %s\t%s\t%s\t%d\t%llu\n",last->usn,last->name,last->branch,last-
>sem,last->phno); free(last);
p->link=NULL; last=p;
count--;
}
}

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

total no.of students 2


1.create SLL 2.insert at front 3.insert at rear 4.display 5.delete at front 6.delete at rear 7.exit
enter choice2
enter usn,name,branch,sem,phno 2vd15cs011suresh cs69481830624

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

Dept. of CSE Page 22


DATA STRUCTURES LABORATORY BCSL305
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
2vd15cs048 vinay cs 8 9481830624
total no.of students 4

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

Dept. of CSE Page 23


DATA STRUCTURES LABORATORY BCSL305

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;

Dept. of CSE Page 25


DATA STRUCTURES LABORATORY BCSL305
last=temp;
}
else
{
create();
temp->rlink=first;
first->llink=temp;
first=temp;
}
}
void insert_rear()
{
if(first==NULL)
{
create();
first=temp;
last=temp;
else{
create();
last->rlink=temp;
temp->llink=last;
temp->rlink=NULL;
last=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)
{

Dept. of CSE Page 26


DATA STRUCTURES LABORATORY BCSL305
printf("%d\t%s\t%s\t%s\t%f\t%llu\n",p->ssn,p->name,p->dept,p->desg,p->sal,p->phno); p=p-
>rlink;
}
printf("total no. of employee %d\n",count);
}

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);

Dept. of CSE Page 27


DATA STRUCTURES LABORATORY BCSL305
first=NULL;
free(first); count--;
}
else
{
p=last;
last=p->llink;
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);
last->rlink=NULL; count--;
}
}

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

enter ssn,name,dept,desg,salary and phno 110vinay csprogramer 140009481830625

1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit


enter choice 2
enter ssn,name,dept,desg,salary and phno 111suresh cs lecturer 400009482230624

1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit


enter choice 3
enter ssn,name,dept,desg,salary and phno 222naveen cs lecturer 500009482230677
1. create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit
enter choice 4

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

Dept. of CSE Page 28


DATA STRUCTURES LABORATORY BCSL305
1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit
enter choice 5
deleted data is 111
1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit
enter choice 4
contents of list
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 3


1.create2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit
enter choice 6

deleted data is 222 naveen cs lecturer 50000.000000 9482230677


1.create 2.insert_front 3.insert_rear 4.display 5.delete_fron 6.delete_rear 7.exit

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

1.create 2.insert_front 3.insert_rear 4.display 5.delete_front 6.delete_rear 7.exit

enter choice 7

Dept. of CSE Page 29


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

#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;
}

NODE *attachnode(NODE *temp,NODE *head)


{
NODE *cur; cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp; temp->link=head; return head;
}

Dept. of CSE Page 30


DATA STRUCTURES LABORATORY BCSL305
NODE *readpoly()
{
int i,n,co,ex,ey,ez;
NODE *head=(NODE*)malloc(sizeof(NODE)); NODE *temp;
head->link=head;
printf("enter the number of terms\n");
scanf("%d",&n); for(i=0;i<n;i++)
{
printf("term %d\n",i+1);
printf("enter the coefficient\n");
scanf("%d",&co);
printf("enter exponent values of x,yand z\n");
scanf("%d%d%d",&ex,&ey,&ez);
temp=createnode(co,ex,ey,ez);
head=attachnode(temp,head);
}
return head;
}
void display(NODE *poly)
{
NODE *cur; cur=poly->link;
while(cur!=poly)
{
printf("%dx^%dy^%dz^%d+",cur->co,cur->ex,cur->ey,cur->ez);
cur=cur->link;
}
printf("\n");
}
void evaluate(NODE *poly)
{
NODE *cur; int x,y,z,res=0; cur=poly->link;
printf("enter the values of x,y,z\n");
scanf("%d%d%d",&x,&y,&z); while(cur!=poly)
{
res+=cur->co*pow(x,cur->ex)*pow(y,cur->ey)*pow(z,cur->ez); cur=cur->link;
}
printf("result=%d\n",res);
}

Dept. of CSE Page 31


DATA STRUCTURES LABORATORY BCSL305

NODE *addpoly(NODE *p1,NODE *p2,NODE *poly)


{
int comp;
NODE *a,*b,*temp; a=p1->link;
b=p2->link;
while(a!=p1&&b!=p2)
{
if(a->ex==b->ex && a->ey==b->ey && a->ez==b->ez) comp=0;
else if(a->ex>b->ex) comp=1;
else if(a->ex==b->ex && a->ey==b->ey) comp=1;
else if(a->ex==b->ex && a->ey==b->ey && a->ez>b->ez) comp=1;
else comp=-1;
switch(comp)
{
case 0:temp=createnode(a->co+b->co, a->ex, a->ey, a->ez); poly=attachnode(temp,poly);
a=a->link;
b=b->link; break;
case 1:temp=createnode(a->co, a->ex,a->ey,a->ez);
poly=attachnode(temp,poly); a=a->link;
break;
case-1:temp=createnode(b->co,b->ex,b->ey,b->ez); poly=attachnode(temp,poly);
b=b->link; break;
}
}
while(a!=p1)
{
temp=createnode(a->co,a->ex,a->ey,a->ez); poly=attachnode(temp,poly);
a=a->link;
}
while(b!=p2)
{ temp=createnode(b->co,b->ex,b->ey,b->ez);

poly=attachnode(temp,poly); b=b->link;
}
return poly;
}
Void main()
{

Dept. of CSE Page 32


DATA STRUCTURES LABORATORY BCSL305
int ch;
NODE *p1,*p2,*p3; p3=(NODE*)malloc(sizeof(NODE)); p3->link=p3;
while(1)
{
printf("1.represent and evaluate 2.add two polynomial 3.exit\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter a polynomial\n");
p1=readpoly();
display(p1);
evaluate(p1);break;
case 2:printf("enter polynomial 1\n");
p1=readpoly();display(p1);
printf(" enter polynomial 2\n");
p2=readpoly();
display(p2);
p3=addpoly(p1,p2,p3);
printf("the resultant polynomial is\n"); display(p3);break;
case 3:exit(0);
}
}
}

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

Dept. of CSE Page 33


DATA STRUCTURES LABORATORY BCSL305
enter exponent values of x,yand z 311

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+

enter the values of x,y,z


1
1
1
result=9

Dept. of CSE Page 34


DATA STRUCTURES LABORATORY BCSL305
PROGRAM 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 In-order, Preorder and PostOrder
C Searchthe BST for a givenelement (KEY) and reportthe appropriate message
D Delete anelement (ELEM) fromBST
EExit

#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{

Dept. of CSE Page 35


DATA STRUCTURES LABORATORY BCSL305

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;
}

Dept. of CSE Page 36


DATA STRUCTURES LABORATORY BCSL305
void insert(node *root, node *new_node)
{
if (new_node->data < root->data)
{
if (root->lchild == NULL) root->lchild = new_node; else
insert(root->lchild, new_node);
}
if (new_node->data > root->data)
{
if (root->rchild == NULL) root->rchild = new_node; else
insert(root->rchild, new_node);
}
}
node *search(node *root, int key, node **parent)
{
node *temp; temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
printf("\nThe %d Element is Present", temp->data);
flag=1;
return temp;
}
*parent = temp;
if (temp->data > key) temp = temp->lchild; else
temp = temp->rchild;
}
return NULL;
}
void inorder(node *temp)
{
if (temp != NULL)
{
inorder(temp->lchild); printf("%d\t", temp->data); inorder(temp->rchild);
}
}

Dept. of CSE Page 37


DATA STRUCTURES LABORATORY BCSL305
void preorder(node *temp)
{
if (temp != NULL)
{
printf("%d\t",temp->data); preorder(temp->lchild); preorder(temp->rchild);
}
}
void postorder(node *temp)
{
if (temp != NULL)
{
postorder(temp->lchild); postorder(temp->rchild); printf("%d\t", temp->data);
}
}

Output

Program For Binary Search Tree 1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :1
Enter The Element 6
Want To enter More Elements?(1/0)1 Enter The Element 9
Want To enter More Elements?(1/0)1 Enter The Element 5
Want To enter More Elements?(1/0)1 Enter The Element 2
Want To enter More Elements?(1/0)1 Enter The Element 8
Want To enter More Elements?(1/0)1

Enter The Element 15


Want To enter More Elements?(1/0)1 Enter The Element 24
Want To enter More Elements?(1/0)1 Enter The Element 14
Want To enter More Elements?(1/0)1 Enter The Element 7
Want To enter More Elements?(1/0)1 Enter The Element 8
Want To enter More Elements?(1/0)1 Enter The Element 5
Want To enter More Elements?(1/0)1 Enter The Element 2
Want To enter More Elements?(1/0)0

1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :3

Dept. of CSE Page 38


DATA STRUCTURES LABORATORY BCSL305

The Inorder display :


2 5 6 7 8 9 14 15 24
The Preorder display :
6 5 2 9 8 7 15 14 24
The Postorder display :
2 5 7 8 14 24 15 9 6

1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :2
Enter Element to be searched:15

The 15 Element is Present Parent of node 15 is 9

1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :2
Enter Element to be searched :24

The 24 Element is Present Parent of node 24 is 15

1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :2
Enter Element to be searched :2

1.Create2.Search3.Recursive Traversals 4.Exit


Enter your choice :4

Dept. of CSE Page 39


DATA STRUCTURES LABORATORY BCSL305
PROGRAM 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.
BPrintall the nodes reachable from a givenstarting node ina digraph using BFS method
CCheck whether a given graph is connected or notusingDFS Method.

#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++)

Dept. of CSE Page 40


DATA STRUCTURES LABORATORY BCSL305
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
while(1)
{
printf("\n\n1.BFS\n 2.DFS\n 3.Exit\n");
printf("\nenter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter the source :\n");
scanf("%d",&source);
for(i=1;i<=n;i++)
s[i]=0;
bfs(n,a,source,s);
for(i=1;i<=n;i++)
{
if(s[i]==0)
printf("\n The node %d is not reachable\n",i);
else
printf("\n The node %d is reachable\n",i);
}
break;
case 2:printf("\nEnter the source vertex :\n");
scanf("%d",&source);
count=0; for(i=1;i<=n;i++) s[i]=0;
dfs(n,a,source,s);
for(i=1;i<=n;i++)
if(s[i])
count=count+1;
if(count==n)
printf("\nThe graph is connected.");
else
printf("\nThe graph is not connected."); break;
case 3: exit(0);
}
}
}

Dept. of CSE Page 41


DATA STRUCTURES LABORATORY BCSL305
Output1
Enter the number of nodes: 4
Enter the adjacencymatrix 0 0 1 0
1010
0000
0000

enter your choice 1

Enter the source :1


The node 1 is reachable
The node 2 is notreachable
The node 3 is reachable
The node 4 is notreachable

1.BFS2.DFS3.Exit
enter your choice 2

Output2

Enter the number of nodes : 3


Enter theadjacencymatrix 0 1 1
000
000
1.BFS2.DFS3.Exit
enter your choice 1
Enter the source : 1
The node 1 is reachable
The node 2 is reachable
The node 3 is reachable

Dept. of CSE Page 42


DATA STRUCTURES LABORATORY BCSL305
PROGRAM 12

Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let
the keys in K and addresses in L are Integers.

Design and develop a Program in C that uses Hash 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;
}
}
}
}

Dept. of CSE Page 44


DATA STRUCTURES LABORATORY BCSL305

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

1.Display ALL 2.Filtered Display

enter the choice2


the hash table is
0 1398
34 1234
48 2548
56 3256
57 1456
98 1298
99 1299

Dept. of CSE Page 45


DATA STRUCTURES LABORATORY BCSL305

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:

Insertion of elements into Singly Linked List


Two cases may arise when we want to insert data at the front.

Case – 1 : When the list is empty i.e. head = NULL

Here we can simply do head = tail = newNode(pointer)

Case – 2 : When list contains some entries i.e. head != NULL


Insertion at Front
Insertion at End
Insertion after a Node

Dept. of CSE Page 46


DATA STRUCTURES LABORATORY BCSL305

Simulation Link:https://ds1-iiith.vlabs.ac.in/exp/linked-list/singly-linked-list/sllpractice.html

Dept. of CSE Page 47


DATA STRUCTURES LABORATORY BCSL305
EXPERIMENT:02

Binary Search Tree

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:

Inserting a new node into a BST


 Start from root.
 Compare the inserting element with root and if it's lesser than root, then recurse for left,
else recurse for right.
 After reaching the end, just insert that node at left(if less than current), else right.
Simulation Link:https://ds1-iiith.vlabs.ac.in/exp/binary-search-trees/bst-insert/bstInsert.html

Dept. of CSE Page 48

You might also like