0% found this document useful (0 votes)
5 views

DSA lab manual

Uploaded by

srushtim1420
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)
5 views

DSA lab manual

Uploaded by

srushtim1420
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/ 18

Data Structures Laboratory Manual

Design, Develop and implement a menu dri.ven program in e


following operations on Singly Linked List (SLL) of student
the fields: USN, Name, Branch, Sem & PhNo. data
a. Create a SLL of N s tudents data by using front insertion
b. Display the status of SLL & count the number of nodes in it
C. Perform Insertion/Deletion at end of
SLL
d. Perform Insertion/Deletion at front of SL (Demonstration
e. Exit
o

#include<stdio . h>
#include<conio .h>
struct node *temp=NULL, *p=NULL;
6, 4,77
int i=i,n, choice;

struct node

char NAME [20]


,7 char USN [20];
char BRANCH [20]

6,US7 int SEM;


char MOBNO [20] lo
3,25 struct node *next;
*first=NULL;
1,vokd insertatfront ()
printf ("\nEnter Student
3 id Details: NAME,

p=(struct node* ) mal1loc (si zeof (struct node) );


USN, BRANCH, SEM, PHNO\

,scanf ("is%s%s&d?s",p->NAME, p->USN, p->BRANCH,


&p->SEM, p->MOBNO) ;
3,-6 i+t; // FOR NEXT STUDENT

,0,91/ p->next=0;
if (first==NULL)
90, 15
2,28 first=p
40,22
else

p->next-first:;
first-p;
1),11
7,1,3
void insertatrear ()

printf ("\nEnter Student &d


Oh,31
Details: NAME, USN,
BRANCH,SEM, PHNO \n
p-(struct node*) malloc (sizeof (struct
scanf ("8s%stsid8s" ,p->NAME, node );
p->USN, p->BRANCH, &p->SEM,
p->MOBNO)
it+;// FOR NEXT STUDENT
Data Structures Laboratory Manual
2016-17
temp-first;
tu ial
while(temp->next!-0)
temp-temp->next

temp->next-p; D kot 01
p->next-0;

front ()
void deleteat

p-first

i f (p-=NULL)

printf ("EMPTY SLL.. . Deletion not possible. . \n") ;

first=p->next;

free (p)

void deleteatrear ()

p-first;

i f (p=-NULL)

SLL... Deletion not possible. . \n") ;


printf ("EMPTY

while (p->next->next! =0)

p=p->next;

p->next=0;
free (p->next->next) ;

void display ()
int count=0;

p-first;
if (p-=NULL)
Not
Possible ... \n");
SLL... Display is
printf("EMPTY

White p! =0)
count+1) ;
are.. . \n",
p->USN, p->BRANCH,
Details
8d
p r i n t f ("\nNode \n",p->NAME,
\t8s\t%d\t8s

printf ("8s\tis
p->SEM,p->MOBNO);
p-p->next; Page 17
577501
Chitradurga
-

Engineering, SJMIT,
partment of Computer Science and
Count=count+1;

Number of Nodes 8d \n", count)


printf ("\nTotal

void main ()

clrscr (0
do
-MENU---
.---\n")
printf ("\n----
CREATION OF SLL ") ;
P r i n t f ("\nl->
FRONT. END") ;
printf (" \n2-> INSERT AT..
INSERT AT REAR END") ;
printf ("\n3->
DELETE AT FRONT END ");
printf("\n4->
DELETE AT REAR END") ;
printf ("\n5->
DISPLAY OF SLL")
ptintf (" \n6->
printf ("\n7-> EXIT") ;
--\n") ;
Pintf (" \n----

per Menu. .: ") ;


your choice as .

printf ("Enter
3Canf ("%d", & choi ce) ;
switchchoice)
Number of S t u d e n t s . . :");
case 1: printf ("Enter
scanf("%d", &n);
while (n! =0)

insertatfront ();
n=n-1;

break;
case 2: insertatfront (); break;
Case 3: insertatrear() break;
case 4: deleteat front (); bréak;
case 5: deleteatrear () ; break;
case 6: display() break;
case 7: exit (0);

}while (1);
}/* End of main () */

Department of Computer Science and


Engineering, S]MIT, Chitradurga - 577501
Data Structures Laboratory Manual

8. Des1gn Develop and 2016-17


11owing implement a menu driven
fields:aoperations on
sSN, Name, Doubly Linked List (DLL) progr am in C for
the
Create DLL of N Dept, Designation, Sal, & of Employee Data with
pisplay the status Employees
data by PhNo
and count using end insertion.
b. of DLL
C. Perform Insertion and
Deletion at Endnumber of nodes in
it.
Perform Insertion
and Deletion
of DLL
e.
f.
Demonstrate
De
Exit
how this at Front
DLL can be used as of DLL. Double Ended Queue.

#include<stdio.h>
#include<conio.h>

struct node *temp=NOLL, *p=NULL;


int i=1, n, choi ce;
struct node

int SSN
char NAME [ 20]:
char DEPT [20]
char DESIGN [ 20];
int SALARY
char MOBNO [ 20];

struct node *next, *prev;


*first=NULL;

void insertatrear ()

Details: SSN, NAME, DEPT, DESIGNATION,


printf (" \nEnter id Employee
SAL, PHNO. \n", i);
node) ) ;
p=(struct node * ) malloc (sizeof (struct
scanf ("gdisissidis", &p->SSN,p->NAME, p->DEPT,p->DESIGN, &p->SALARY,
p->MOBNO);

p>next=0;
p->prev=0;
EMPLOYEE
FOR NEXT
i+t///
.f(first==NULL

first=Pi
first->next=0

else

temp=first;
while (temp->next ! =0)
{
temp=temp->next;

p->next=0;
P->prev=temp;

temp->next=p

Page 19
577501
SJMIT, Chitradurga -
and Engineering
Lnent
of Computer Sciende
Manual
Data Structures Laboratory

void insertatfront () NAME, DEPT, DESte.


SSN,
td Employee Details:
("\nEnter
printf
SALARY & MOBILE
NO. \n", 1)
node) )
(sizeof
(struct
P->DESIGN, &p->
node*) malloc
P->DEPT,
kp->SSN, p->NAME,
(struct
p ("8disksis8dts",

Scanf

p->MOBNO)
p->next-0;

p->prev=0;

EMPLOYEE
itt:// FOR NEXT
i f (first-=NULL)

first=p;

else
Tisot PALu=P
first->prev=p;

p->next=first
P urtzJisol
first=p hetap
void deleteatfront ()

i f (p-=NULL)
Possible. \n") ;
is Not
. .

Deletion
DLL.
printf ("EMPTY
. .

p-first;
first=first->next;

free(p);

void deleteatrear ()

p=first;
if (p==NULL)

printf("EMPTY DLL. . .
Deletion is not Possible. .
. \n");

while (p->next->next! =0)

p-p-next;

p-next=0;
free (p->next->next) ;

void display ()

int count=0;
p-first

wuKar
Department of Computer Science and Engineering, SJMIT, Chitradurga - 5775011
Data Structures Laboratory Manual
if (p==NULL) 2016-17

printf("EMPTY DLL Nothing to


else
Display... \n")

printf ("\nsSN\tNAME\t
printf ("---
while (p!=0)
DEPT\tDESG\ESALARY\CMOBILE NOVn") i /
-n")
printf ("
Bd\tts\t8s\tts\t8d\t8s\n",
>DEPT,p->DESIGN, p->SALARY, p->MOBNO) ;) p->8SN,p->NAME,
p-p->next;
count=count+1; *****

printf ("\nTotal Number of Nodes : 8d\n'", count)

void main ()

clrscr();
do

printf (" \n--- --MENU-- ---\n")


printf (" \nl-> CREATION OF DLL ") ;
printf (" \n2-> INSERT AT FRONT END") ;
printf (" \n3-> INSERT AT REAR END") ;
END ")
printf ("\n4-> DELETE AT FRONT
REAR END");
printf (" \n5-> DELETE AT
DISPLAY OF DLL") ;
printf (" \n6->
printf (" \n7-> EXIT"); ---\n")
printf (" \n--
choice as per menu... " );
printf ("Enter your
scanf ("&d", &choi ce)

Switch (choice)
of Employees. .
:");
Number
case 1: printf ("Enter
scanf ("%d", &n);
while (n!=0)

insertatrear();
n=n-1;

break; break;
insertatfront (): break;
case 2 insertatrear(); break;
case 3: deleteat front (); break;
case 4:
deleteatrear (); break;
case 5:
case 6:
display ():
case 7: exit(0);

while (1) Page 21


7* End of main () */
577501
Chitradurga-
Engineering,
SJMIT,
Science and
partment
of Computer
Data Structures Laboratory Manual

PrOgram Ln C for the


a
9. Design,
operations
Develop
on
and
Singly Circular
Implement
Linked i a t
(3CLLa)
Polynomial
with
P(x,Y,z)
headeo
toi
odes
a, Represent and Evaluate
4yz+3x'yn+2xy a-2xyn" POLY1 (x,Y, z) and POLY2 (
polynomials
b. Find the sum of two
s t o r e the r e s u l t in POLYSUM (*,Y, z2) for each

Supportt the program with appropriate


funations of
te
operations.

#include<stdio .h>
#include<stdlib.h>
link
typedef struct

int coeff;
int poW
struct link * next;

my_poly;
**)
my_create poly (my poly
;
void
void my_show poly (my_poly *)
;
*) ;
void my_add_poly (my_poly **, my_poly *, my_poly

int main (void)

int ch
clrscr()
do

poly2,
* poly3;
poly1,
* *

my_poly
printf (" \nCreate 1st expression \n") ;
my_create_poly (&polyl) ;
printf \nStored the 1st expression") ;
("
my_show_poly (poly1) ;

printf ("\nCreate 2nd expression \n"))


my_create_poly (&poly2);
printf (" \nStored the 2nd expression") ;

my_show_poly (poly2) ;

my_add_poly (&poly3, polyl,


my_show_poly (poly3) ;
poly2)

p r i n t f(" \nAdd two more expressions? (Y = 1/N = 0): ")


scanf ("sd", &ch);
}while (ch);
return 0;

void my_create_poly (my_poly ** node)

int flag;/*A flag to control


int coeff, poW
the menu */
*
my_poly tmp_node; /*To hold the temporary last address *
tmp_node= (my_poly* ) malloc (sizeof (my poly) ) ;/*create the lSt no

Department of Computer Science and Engineering, S]MIT,


Chitradurga -

577501
Data StructuresLal
Laboratory Manual
2016-17
*node tmp_ node / Store
variable */ the head address to the reference
do

printf (" \nEnter / Get the user data */


scanf ("Bd", &coefCoeff:") ;
tmp node->coeff=f); coeff;
printf (" \nEnter Pow:"))
scanf ("8d", &pow);
tmp node->pow pow; =

/* Done storing user data /


/* Now increase the-Linked
tmp node->next = NULL;
on user condition */

/* Ask user for continuation */


printf ("\ncontinue adding more terms to the polynomial list? (Y =
1/N = 0): ");
scanf ("%d", &flag) ;
/* print f (" \nEFLAG : %c\n", flag); */
/* Grow the linked 1ist on condition */
if (flag)

Emp_node->next = (my_poly *) malloc (sizeof (my_poly) ) ;


t m p node = tmp_node->next;
tmp node->next = NULL;

while (flag)

void my_show_poly (my_poly * node)

printf ("\nThe polynomi al expression is: \n") ;


while(node!= NULL)

printf ("&dx^8d", node->coeff, node->pow) ;


node = node->next;

if (node != NULL)
printf (" + ");

*
**
result, my_poly
*
polyl, my_poly poly2)
vOld my_add poly(my _poly
my_polytmp_node; //Temporary storage for
the linked list
tmp_node =
(my_poly *) malloc (sizeof(my_polY) )
tmp_node->next = NULL;
the head address to the result LL
result =
tmp node; //Copy
the linked lists have value */
*Loop while both of
while (polyl && poly2)

if (polyl->pow > poly2->pow)

tmp_node->pow = polyl-->pow;

partment of Computer Science and Engineering, S]JMIT, Chitradurga - 577501 Page 23


Laboratory Manual
Structures polyl->coeff;
Data
=

n o d e - > c o e f f

tmp
p o l y l = p o l y 1 - > n e x t ;

->pow)
poly2
( p o l y l - > p o w

if
else
tmp_node->powpoly2->pow;
poly2->coeff;

node->coef f
tmp
p o l y 2 = p o l y 2 - > n e x t ;

else
polyl->pow;

=
poly2->coas
node->pow + eff-
tmp
polyl->coeff
=

node->coe ff
tmp
p o l y l p o l y 1 - > n e x t ;

poly2 - poly2->next;

&& poly2)
i f (polyl
malloc (si zeof (my po
tmp_node->next=
(my_poly *)
node->next;
tmp node tmp =

tmp_node->next = NULL;

linked l i s t s has value */


the
while e i t h e r of
/* Loop
while(polyl || poly2)
{ the l i s t at beginning */
/* We have to c r e a t e unnecessar
will not c r e a t e any
/* As the l a s t while loop
malloc (sizeof (my poly) ) ;
tmp_node->next =(my poly *)
tmp_node = tmp_node->next;

tmp_node->next = NULL;

if (poly1)
tmp_node->pow = polyl->pow;

tmp_node->coeff = polyl->coeff;

polyl polyl->next;

if (poly2)

tmp_node->pow = poly2->pow;
S
tmp_node->coeff poly2->coeff:
poly2 poly2->next;

printf (" \nAddition Complete") ;


Data Structar es LudDoratory Manual

10. Design, Develop and 2016-17


following operations onImplement a menu driven
Create a BsT of N Binary Search Tree Program in C for the
Traverse the BST in Integers: 6, 9, 5, 2, 8, (BsT) of
Integers.
Search the BST for Inorder, Preorder and 15, 24, 14, 7, 8, 5, 2.
message.
a
Postorder.
given element (KEY) and
Delete an
report the
appropriate
d. element (ELEM)
e. E x i t . from BST

#include<stdio.h>

#include<stdlib.h>
struct tree

int data;
struct tree *left;
struct tree *right;

struct tree * create (int) ;

void traverse (struct tree*) ;


void inorder (struct tree *) ;
void preorder (struct tree *)/;
void postorder (struct tree f)
struct tree * search (struct treet, int) ;

struct tree * delete (struct tree* int);

void main ()

int choice, n, key, element;


struct tree *bst=NULL, *temp;
A
clrscr () ;

while (1)

printf ("\n MENU ")


BST Of N integers") ;
printf (" \nl=>Create Preorder, and
p r i n t f ("\n2=>Traverse the BST in Inorder,

Postorder") ;
("\n3=>Search the BST for a Key element ") ;
printf from BST") ;
printf("\n4=>Delete an Element

printf("\n5=>Exit ") ;

your
choice. . ");
printf ("\nEnter
scanf ("sd", &choi ce) ;

switch (choi ce )
elements of
printf ( "Enter the number of
case 1:
BST\n") scanf("&d", &n)
bst=create (n) ;

break;
t r a v e r s e (bst) ; break;
case 2:
Page 25
577501
SJMIT, Chitradurga
-

Science
and Engineering,
Data Structures Laboratory Manual
the Key
searched in BST\n ")
case 3
printf("\nEnteer

scanf ("Bd", &key)


elenent
(bst, key)
temp search
i f(temp is foundi
printf ("Thel/Key
else
element
foundin")
printf("The key
element is notnot fo
foundin
break;

printf ("\nEnter the Element to be d


case 4:

from BST\n") scanf ("8d", &element) ;


element) ;
bst-delete (bst,
break;

case 5: exit (0)

struct tree *cfeate (int n) 0


*temp;
struct tree *head=NULL, *ptr, *ptrl,
int i,ele;

printf ("Enter the Elements \n")


;

for (i-=0; i<n;it+)

scanf ("$d", &ele) ;


temp= (struct t r e e
*
) malloc (sizeof (struct tree) ) ;

temp->data=ele;
temp->left=temp->right=NULL;

i f(!head)
head=temp;
continue;

ptr-ptrl =head;

do

ptr-ptrl;
if (ptr->data>ele)
ptrl=ptr->left
else if (ptr->data<elee)
ptrl-ptr->right;
else

printf ("Dupli.cate el ement" ) ;


i--
Continue;
while (ptr1) ;
2016-17
if (ptr->data>ele)

else ptr->left=temp
ptr->right=temp
retua head;

void traversestruct tree *bst)

printf ("\nThe Inorder Traversal : \t") ;


inorder (bst) ;
printf (" \nThe Preorder Traversal : \t") ;
preorder (bst)
printf ("\nThe Postorder Traversal : \t") ;
postorder (bst) ;
printf"\n\n")

tree
oid inokdef (struct *bst)

struct tree *temp=bst


if (temp)

inorder (temp->left)
printf ("id\t", temp->data) ;

inorder(temp->right):

tree *bst)
yoid preorder (struct
struct tree *temp=bst;
if (temp)
temp->data) ;
printf ("sd\t",
preorder (temp->left);
p r e o r d e r (temp->right) ;

t r e e *bst)
Noid postorder (struct

struct tree * temp=bst;


if (temp)
p o s t o r d e r( t e m p - > l e f t ) ;
postorder (temp->right);
temp->data) ;
p r i n t f ("&d\t",
tree* search (struct tree *bst, int key)
struct
struct tree temp-bst;
while (temp)

i f (temp->data==key)
return temp;
else i f (temp->data>key)
temp=temp->left:

else
temp=temp->right:

return temp

struct tree *delete (struct tree *bst, int ele)

struct tree * temp=bst, *templ=bst, *ptr, *ptrl;

&& !bst->left &&!bst->right)


i f(bst->data==ele
free (bst) ;
return NULL;

i f (bst->data==ele && !bst->left)

temp=bst->right;
free (bst) ;
return temp;

if (bst->data==ele && !bst->right)

temp=bst->left;
free (bst) ;
return temp;

if (bst->data==ele && bst->left && bst->right)

ptr=ptr1=bst
while (ptrl->1eft)

ptr=ptrl;
ptrl-ptr->left;

ptr->left=NULL;
ptrl->left=bst->left;
ptr1->right=bst->right;
free (bst) ;
return ptrl;

while (temp1)

if (temp1->data-=ele)
break;

temp=temp1;
Denartment of Comnuter Scierce and Enoineering SIMIT Chitradurqa c77501
Structures Laboratory Manual
if (temp->data>ele) 2016-17

else templ=temp->left;
templ=temp->right;
if(templ &&
templ->data==ele)
if(!templ->left &&!temp1->right)
if (temp->left==temp1)

else
temp->left=NULL;
temp->right=NULL;
else if (!
templ->left)
if (temp->left==temp1)

else temp->left=temp1->right;
temp->right=templ->right;
else if (!
temp->right)
if (temp->left==temp1)
temp->left=temp1->left;
else
temp->right=temp1->left;
else

ptr=ptrl=templ->right;
while (ptrl->left)

ptr=ptrl;
ptrl=ptr->left ;

ptr->left=NULL;

ptrl->left=temp1->left;

ptrl->right=temp1->right;

if (temp->left==temp1)
temp->left=ptr;
else
temp->right=ptr;

free (temp1) ;

else
element not found \n ")
printf ("The
if (bst->data ! =ele)
return bst;
else
return ptrl
11. Design, develop and
Implement aa Program in C for
operations on Graph (G) of C i t i e a the to
Matriv
a. Create a Graph of N ci ties using Adjacency
nodes reachable
t r o m a given start.
b. Print all the
BFS me thod.
ing no
digraph using is connected or n ott uusing DEs
c. Check whether a given graph

#include<stdio.h>
aol) lto][t0),Pett iS7td
16), iuct n
int Ss t int
int adj[10] [10] , int visited [10], n)
void dfstint src,

int k -u- ----

visited [src] =1;

for (k=0; k<n; k++)

if (adj [src] [k] ==l && visited [k] ==0)

dfs (k,adj, visited, n)

void bfs (int src, int adj[10] [10] , int n),

int q[20],front=0, rear=-1, V, u, visited [ l0]={0}


q[++rear] =src;
visited [src] =1;
printf ("&d", src) ;V
while (front<=rear)

u=q[front++] ;
for (v=0; v<n;v++)

if (adj [u] [v] ==l && visited [v] ==0)

q[++rear] =v;
printf ("%d",v) ;
visited [v] =l ;

void ma in ()

int choice, i,j,src, flag=0;


int adj [10] [101,visited [10] ={0},n;

clrscr()
while (1)

printf("\n MENU ")

printf (" \nl=> Create a graph of N cities");


printf (" \n2=> To print reachable nodes from source node using
BFS") ;
printf (" \n3=> To check graph connected or not using DFs") ;
printf("\n4=> Exit")
printf (" \n Enter the choice. . ")
scanf ("3d", &choice) ;

switch (choi ce)

case 1: printf (" \nenter the number of cities \n ")


scanf ('"%d", &n) ;
printf (" \nenter the adjacency matrix \n")
;

for (i=0; i<n; i++)


for (j=0;j<n;jt+)
Scanf ("id", &adj [i] [j):
break; verteX to start
case 2: printf ("Enter the Source

traversal\n") scanf ("d", &s rc) ;


printf ("vertices
visited are \n")
bfs (srC, adj , n);
break; vertex to start
printf ("Enter the source

case 3:

traversal\n") Scanf ("%d", &src) ;

for (i=0; i<n; i++)


visited [i] =0;
dfs (src, adj, visited, n) ;

for (i=0; i<n; i++)


i f( v i s i t e d [ i j = = 0 )

flag=1;
i f (flag==1) not connected \n") ;
graph i s
printf ("the
else
graph i s connected
\n ") ;
printf ("the

break;

case 4: exit()
LaboratoryManual
Data Structures with a s.

records
employee in file B

a
File
ofN
the
records

o
of m Assume othat 1
2.
Given

which
uniquely
determine

by a
Hash
7able (HT)
(2-digi.t)
of 1
nory
t y locati
memory
in
m a i n t a i n e d

memory
a d d r e s s e s

L are
Integera in
of in
as t h e
in
set

K and
a d d r e s s e s

Hash
function H: K- aS H(K
keys
C
that
uses
ashing techniq
has
coll.
implement
in
Program
( r e m a i n d e r

the
method),

address
and

Ctadet
r
space
.
Resolve
the
11ision (it
to
key
linear probing.

include<stdio. h>
#include<conio.h>
#include<s tdlib.h>

fdefine MAX 30
int create (int num)

int key}
key=num8100;

return key;

int key, int num)


linear_prob (int a [MAX},
yoid
Int lagsisCountr6
int flag, i, count=0;
void display (int ) bid iiply Cint ) );
flag=0;

if (a [key] ==-1)
a [key] =num;
else

i-0
while (i <MAX)

if(a[i]!=-1)
Count++;
itt;

if (count==MAX)

printf ("\nHash Table is Full.. ") ;


display (a);
exit (1)
for
(i=key+1;
if (a[i]==-1)
i<MAX; it+)

afij=num;
flag-1;
break;
for (i=0;
if (a[i]==-1) i<key&&flag==0; i++)
Department of Computer Science
and Engineering,
S|MIT, Chitradurga- 77501
ali] =num;
flag-1
break

a[MAX])
roid display (int
int i;
clrscr ():
printf (" \nThe Hash Table is... \n") ;
for (i-10; i<MAX; i++)
sd sd", i, [i])
-

a ;
printf (" \n

void main ()

i n t a [MAX], key, num, i;


char ans;

clrscr ();
Linear Probing. . ") ;
printf (" \nCollision Handling By
for (i=10;i<MAX; i++)
a[i]=-1;

do
The Four Digit
Number : )
")
printf (" \nEnter
scanf ("sd", &num);

key=create (num);
l i n e a r prob (a, key, num)

You Wish to
Continue? (y/n) ") ;
printf (" \nDo
ans=getche ();
}while (ans=='y') ;

display (a) ;
getch ();

You might also like