DSA lab manual
DSA lab manual
#include<stdio . h>
#include<conio .h>
struct node *temp=NULL, *p=NULL;
6, 4,77
int i=i,n, choice;
struct node
,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 ()
temp->next-p; D kot 01
p->next-0;
front ()
void deleteat
p-first
i f (p-=NULL)
first=p->next;
free (p)
void deleteatrear ()
p-first;
i f (p=-NULL)
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;
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----
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 () */
#include<stdio.h>
#include<conio.h>
int SSN
char NAME [ 20]:
char DEPT [20]
char DESIGN [ 20];
int SALARY
char MOBNO [ 20];
void insertatrear ()
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
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");
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 ("\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; *****
void main ()
clrscr();
do
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);
#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 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) ;
my_show_poly (poly2) ;
577501
Data StructuresLal
Laboratory Manual
2016-17
*node tmp_ node / Store
variable */ the head address to the reference
do
while (flag)
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)
tmp_node->pow = polyl-->pow;
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;
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;
#include<stdio.h>
#include<stdlib.h>
struct tree
int data;
struct tree *left;
struct tree *right;
void main ()
while (1)
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
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
else ptr->left=temp
ptr->right=temp
retua head;
tree
oid inokdef (struct *bst)
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
i f (temp->data==key)
return temp;
else i f (temp->data>key)
temp=temp->left:
else
temp=temp->right:
return temp
temp=bst->right;
free (bst) ;
return temp;
temp=bst->left;
free (bst) ;
return temp;
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,
u=q[front++] ;
for (v=0; v<n;v++)
q[++rear] =v;
printf ("%d",v) ;
visited [v] =l ;
void ma in ()
clrscr()
while (1)
case 3:
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;
if (a [key] ==-1)
a [key] =num;
else
i-0
while (i <MAX)
if(a[i]!=-1)
Count++;
itt;
if (count==MAX)
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 ()
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 ();