data stuture record.doc
TRANSCRIPT
-
8/14/2019 data stuture record.doc
1/109
-
8/14/2019 data stuture record.doc
2/109
ARULMIGU MEENAKSHI AMMAN COLLEGE OF
ENGINEERING
VADAMAVANDAL 604 410
-
8/14/2019 data stuture record.doc
3/109
-
8/14/2019 data stuture record.doc
4/109
CERTIFICATE
This is to Certify that the bonafide record of the practical work done by
Register Number ....
of II year B.E Computer !cience and Engineering" submitted for the B.E #
$egree practical e%amination III S#"#()#* in CS2208 DATA
STRUCTURES LAB during the academic year &'(& ) &'(*
S)!33 ' -C!* H#! 3 )# D#5!*)"#)
!ubmitted for the practical e%amination held on #################
I)#*! E7!"'#* E7)#*! E7!"'#*
-
8/14/2019 data stuture record.doc
5/109
INDE
S.NO DATE CONTENT PAGE NO SIGNATURE
1 IMPLEMENT SINGL AND DOUBLLINKED LISTS
2
REPRESENT A POLNOMIAL AS A
LINKED LIST AND 9RITE
FUNCTIONS FOR POLNOMIAL
ADDITION
: IMPLEMENT STACK AND USE ITTO CONVERT INFI TO POSTFI
EPRESSION
4 IMPLEMENT A DOUBLE-ENDED;UEUE IMPLEMENT INSERTION IN AVLTREES
8 IMPLEMENT PRIORIT ;UEUEUSING BINAR HEAPS
? IMPLEMENT HASHING 9ITH OPENADDRESSING
10 IMPLEMENT PRIM@S ALGORITHMUSING PRIORIT ;UEUES TO FIND
MST OF AN UNDIRECTED GRAPH
IMPLEMENT SINGL LINKED LISTS
E.NO$01
DATE$
-
8/14/2019 data stuture record.doc
6/109
AIM$
To write a program to implement singly linked list.
ALGORITHM$
S)#51+ !tart
S)#52+ Creation+ ,et the number of elements- and create the nodes haing structures
$/T/0IN1 and stores the element in $ata field- link them together to
form a linked list.
S)#5:+ Insertion+ ,et the number to be inserted and create a new node store the alue in$/T/ field. /nd insert the node in the re2uired position.
S)#54+ $eletion+ ,et the number to be deleted. !earch the list from the beginning and locate
the node then delete the node.
S)#5=+ $isplay+ $isplay all the nodes in the list.
S)#56+ !top.
-
8/14/2019 data stuture record.doc
7/109
SOURCE CODE$
3include4stdio.h5
3include4conio.h5
3include4stdlib.h5
3define N600 '
typedef struct list
7
int no8
struct list9ne%t8
:0I!T8
0I!T 9p-9t-9h-9y-9ptr-9pt8
oid create oid"8
oid insert oid"8
oid delet oid"8
oid display oid"8
int ;-pos-k
-
8/14/2019 data stuture record.doc
8/109
scanf=>d=-An"8
countd=-Aopt"8
switchopt"
7
case (+
insert"8
count8
break8
case &+
delet"8
count##8
ifcount
-
8/14/2019 data stuture record.doc
9/109
printf=@n list is empty @n="8
:
break8
case *+
printf=list elements are+ @n="8
display"8
break8
:
printf=@n enter your option @n="8
:
whileoptDd=-Ap#5no"8
p#5ne%t < N6008
h
-
8/14/2019 data stuture record.doc
10/109
t< 0I!T9"malloc si?eof 0I!T""8
printf =@n enter the elements="8
scanf =>d=-At#5no"8
t#5ne%t
-
8/14/2019 data stuture record.doc
11/109
td=-Apos"8
ifpos
-
8/14/2019 data stuture record.doc
12/109
:
:
oid display"
7
t
-
8/14/2019 data stuture record.doc
13/109
-
8/14/2019 data stuture record.doc
14/109
enter your option
*
list elements are8
(& ( (* & * (
enter your option
-
8/14/2019 data stuture record.doc
15/109
RESULT$
-
8/14/2019 data stuture record.doc
16/109
AIM$
To write a program to implement doubly linked list with Insert- $elete and $isplay
operations.
ALGORITHM$()#51+ !tart
()#52+ Creation+ ,et the number of elements to create the list. Then create the node
haing the structure B0IN1 $/T/F0IN1 and store the elements in $ata
field. 0ink them together to form a doubly linked list.
S)#5:$Insertion+ ,et the number to be Inserted- create a new node to store the alue.
!earch the list and insert the node in its right position.
()#54$ $eletion+ ,et the number to be deleted. !earch the list from the beginning and tryto locate node p with $/T/. If found then delete the node.
S)#5=$F0IN1 GHs preious node to GHs Ne%t node. B0IN1 GHs Ne%t node to GHs Greious
node else display $ata not FoundJ.
S)#56$$isplay+ $isplay all the nodes in the list.
S)#5>$!top.
IMPLEMENT DOUBL LINKED LISTS
E.NO$01
-
8/14/2019 data stuture record.doc
17/109
SOURCE CODE$
3include4stdio.h5
3include4conio.h5
3include4stdlib.h5
3define N600 '
typedef struct list
7
int no8
struct list9ne%t8
struct list9pre8
:
0I!T8
0I!T 9p-9t-9h8
oid create oid"8
oid insert oid"8
oid delet oid"8
oid display oid"8
int ;-pos-k
-
8/14/2019 data stuture record.doc
18/109
printf=enter the no of nodes+@n="8
scanf=>d=-An"8
countd=-Aopt"8
switchopt"
7
case (+
insert"8
count8
break8
case &+
delet"8
count##8
ifcount
-
8/14/2019 data stuture record.doc
19/109
7
printf=@n list is empty@n="8
:
break8
case *+
printf=@n list element are +@n="8
display"8
break8
:
printf=@n enter your option @n="8
:
whileoptDd=-Ap#5no"8
p#5ne%t
-
8/14/2019 data stuture record.doc
20/109
hd=-At#5no"8
t#5ne%t
-
8/14/2019 data stuture record.doc
21/109
h#5ne%t
-
8/14/2019 data stuture record.doc
22/109
t
-
8/14/2019 data stuture record.doc
23/109
OUTPUT$
enter the number of nodes
-
8/14/2019 data stuture record.doc
24/109
&
enter the position to delete+
(
enter your option
*
list elements are+
(
&
*
((
RESULT$
-
8/14/2019 data stuture record.doc
25/109
AIM$
To deelop a program to represent a polynomial as a linked list and write
functions for polynomial addition.
ALGORITHM$
S)#51$!tart.
S)#52$Create a 0inked lists used to represent and manipulate polynomials.S)#5:$ / polynomial can be represented as GL" < an%nean#(%n#(ea(%(e a Mhere
ai ) non?ero coefficients-'4i4n ei ) e%ponent
()#54$ Each node in the polynomial is considered as a node .Each node contains three fields
Node !tructure
C#33'/'#) E75#) L'
Coefficient Field # Mhich holds the coefficient of a term
E%ponent Field # Mhich holds the e%ponent alue of that term 0ink Field # /ddress of the ne%t term in the polynomial
S)#5=$G and O are two polynomials.G A O can be represented as linked list.
S)#56$G+K%&%P
S)#5>$O+*%*%&%
S)#58$The resultant polynomial can be represented like this. R+*%*Q%&P%P
S)#5?$!top.
REPRESENT A POLNOMIAL AS A LINKED LIST
AND 9RITE FUNCTIONS FOR POLNOMIAL ADDITION
E.NO$02
DATE$
-
8/14/2019 data stuture record.doc
26/109
SOURCE CODE$
3include4stdio.h5
3include4malloc.h5
3define N600 '
struct node
7
float coef8
int e%po8
struct node9link8
:8
struct node9polyaddstruct node9-struct node9"8
struct node9enter struct node9"8
struct node9insertstruct node9-float-int"8
oid main"
7
struct node 9p(start-9p&start-9p*start8
p(start
-
8/14/2019 data stuture record.doc
27/109
p*startf=-Aco"8
printf=enter e%ponent for term >d+=-i"8
scanf=>d=-Ae%"8
start
-
8/14/2019 data stuture record.doc
28/109
:
struct node9insertstruct node9start-float co-int e%"
7
struct node 9ptr-9tmp8
tmp
-
8/14/2019 data stuture record.doc
29/109
:
struct node9polyaddstruct node9p(-struct node9p&"
7
struct node 9p*start-9p*-9tmp8
p*start
-
8/14/2019 data stuture record.doc
30/109
tmp#5e%po
-
8/14/2019 data stuture record.doc
31/109
ifp*start
-
8/14/2019 data stuture record.doc
32/109
7
p*#5link.(f%>d"=-ptr#5coef-ptr#5e%po"8
ptr
-
8/14/2019 data stuture record.doc
33/109
OUTPUT$
polynomial (+
how many terms you want to enter +*
enter coefficint for terms (+K
enter e%ponent for term(+&
enter coefficint for terms &+*
enter e%ponent for term&+(
enter coefficint for terms *+(
enter e%ponent for term*+'
polynomial &+
how many terms you want to enter +*enter coefficint for terms (+*
enter e%ponent for term(+*
enter coefficint for terms &+
enter e%ponent for term&+&
enter coefficint for terms *+
enter e%ponent for term*+(
-
8/14/2019 data stuture record.doc
34/109
RESULT$
-
8/14/2019 data stuture record.doc
35/109
AIM$
To write a program to implement stack and use it to conert infid to postfi%
e%pression.
ALGORITHM$
S)#51$ !tart.
S)#52$Create a stack to store operand and operator.
S)#5:$In Gostfi% notation the operator follows the two operands and in the infi% notation
the operator is in between the two operands.
S)#54$ Consider the sum of / and B. /pply the operator J to the operands / and B
and write the sum as /B is INFIL. /B is GREFIL. /B is GU!TFIL
()#5=$ ,et an Infi% E%pression as input and ealuate it by first conerting it to postfi%
and then ealuating the postfi% e%pression.
S)#56$The e%pressions with in innermost parenthesis must first be conerted to postfi% so
that they can be treated as single operands. In this way Garentheses can be
successiely eliminated until the entire
e%pression is conerted.
S)#5>$ The last pair of parentheses to be opened with in a group of parentheses encloses
the first e%pression with in that group to be transformed. This last#in first#out
immediately suggests the use of !tack. Grecedence plays an important role in the
transforming infi% to Grefi%.
S)#58$!top.
IMPLEMENT STACK AND USE IT TO
CONVERT INFI TO POSTFI EPRESSION
E.NO$0:
DATE$
-
8/14/2019 data stuture record.doc
36/109
SOURCE CODE$
3include4stdio.h5
3include4string.h5
3include4math.h5
3define blank
3define tab V@tV
3define W/L K'
long int pop"8
long int ealpost"8
charinfi% XW/LY-postfi% XW/LY8
long int stack XW/LY8
int top8
main"
7
long int alue8
char choice
-
8/14/2019 data stuture record.doc
37/109
infi%topostfi%"8
printf=postfi%+>s@h=-postfi%"8
aluec=-Achoice"8
::
infi%toposrfi%"
7
int i
-
8/14/2019 data stuture record.doc
38/109
whilestackXtopYD
-
8/14/2019 data stuture record.doc
39/109
printf=stack oerflow @n="8
e%it("8
-
8/14/2019 data stuture record.doc
40/109
OUTPUT$
enter the postfi% e%pression+
ab"
inalid char e%pression
error+stack is empty...
error+stack is full
inalid char e%pression
the tree is created....
inorder traersal+
b
preorder traersal+
b
postorder traersal+
b
-
8/14/2019 data stuture record.doc
41/109
RESULT$
-
8/14/2019 data stuture record.doc
42/109
AIM $
To write a program to implement a double ended 2ueue $EO6E6E" where insertion
and deletion operations are possible at both the ends.
ALGORITHM$S)#51$!tart.
S)#52$The operations that can be performed on a de2ueue are as follows
Insert at front
Insert at rear
$elete from front
$elete from rear
S)#5:$/fter inserting the element B at front end- the de2ueue will look like this
B C $ E
front rear
S)#54$/fter inserting the element F at rear end-the de2ueue will look like this
B C $ E F
front rear
S)#5=$Me delete an element from front which has to be B.
C $ E F
front rear
S)#56$Me delete an element from rear which has to be F.
C $ E
front rear
S)#5>$!top.
IMPLEMENT A DOUBLE-ENDED
-
8/14/2019 data stuture record.doc
43/109
SOURCE CODE$
3include4stdio.h5
3define W/L K
int de2uearrXW/LY8
int left
-
8/14/2019 data stuture record.doc
44/109
printf=wrong choice@n="8
:return8
:
input2ue"
7
int choice8
while("
7
printf=(.insert at right@n="8
printf=&.delete from left@n="8
printf=*.delete from right@n="8
printf=.display@n="8
printf=K.2uit@n="8
printf=enter your choice+="8
scanf=>d=-Achoice"8
switchchoice"
7
case (+
insertright"8
break8
case &+
deleteleft"8
break8
-
8/14/2019 data stuture record.doc
45/109
case *+
deleteright"8
break8
case +
display2ueue"8
break8
case K+
e%it"8
default+
printf=wrong choice="8
:
:
:
output2ue"
7
int choice8
while("
7
printf=(.insert at right@n="8
printf=&.insert at left@n="8
printf=*.delete from left@n="8
printf=.display@n="8
printf=K.2uit@n="8
-
8/14/2019 data stuture record.doc
46/109
printf=enter your choice+="8
scanf=>d=-Achoice"8
switchchoice"
7
case (+
insertright"8
break8
case &+
insertleft"8
break8
case *+
deleteleft"8
break8
case +
display2ueue"8
break8
case K+
e%it"8
default+
printf=wrong choice@n="8
:
:
:
-
8/14/2019 data stuture record.doc
47/109
insertright"
7
int addeditem8
ifleft
-
8/14/2019 data stuture record.doc
48/109
insertleft"
7
int addeditem8
ifleftd=-Aaddeditem"8
return8
:
deleteleft"
-
8/14/2019 data stuture record.doc
49/109
7
ifleftd @n=-de2uearrXleftY"8
ifleft
-
8/14/2019 data stuture record.doc
50/109
printf=2ueue underflow@n="8
return8
:
printf=element deleted from 2ueue is +>d @n=-de2uearrXrightY"8
ifleft
-
8/14/2019 data stuture record.doc
51/109
:
printf=2ueue elements +@n="8
iffrontpos4d=-de2uearrXfrontposY"8
frontpos8
:
:
else
7
whilefrontpos4
-
8/14/2019 data stuture record.doc
52/109
:
printf=@n="8
return8
:
-
8/14/2019 data stuture record.doc
53/109
OUTPUT$
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+(
input the element for addid in 2ueue+&Q
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+&
element deleted from 2ueue is +('
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+
2ueue elements
-
8/14/2019 data stuture record.doc
54/109
&'*'&Q
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+*
element deleted from 2ueue is +&Q
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+
2ueue elements
&'*'
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
enter your choice+*
element deleted from 2ueue is +*'
-
8/14/2019 data stuture record.doc
55/109
(.insert at right
&.delete from left
*.delete from right
.display
K.2uit
-
8/14/2019 data stuture record.doc
56/109
RESULT$
-
8/14/2019 data stuture record.doc
57/109
AIM$
To deelop a program to implement an e%pression tree- that produce its
pre#order- in#order and post#order traersals.
ALGORITHM$
S)#51+ !tart.
S)#52$Create a binary tree with the nodes partitioned into three dis;oint subsets. The first
subset consists of a single element called the root of the tree. The other two subsets
are themseles binary trees called the left and right sub trees of the original tree./
left or right sub tree can be empty.Each element of a binary tree is called node of
the tree.
S)#5:$/ common operation in the binary tree is to traerse that is to pass through the
tree enumerating each nodes once. The traersal of the binary tree can be done in
three ways they are preorder - in order- post order traersal.
S)#54$To traerse a nonempty binary tree preorder we perform the following three
operationsas depth#first order"
o [isit the root
o Traerse the left subtree in preorder
o Traerse the right subtree in preorder
S)#5=$To traerse a nonempty binary tree inorder we perform the following three
operationsas symmetric order"
o Traerse the left subtree in inorder
o [isit the root
o Traerse the right subtree in inorder
S)#56$ To traerse a nonempty binary tree postorder we perform the following three
operations
o Traerse the left subtree in postorder
o Traerse the right subtree in postorder
o [isit the rootS)#5>$ !top.
IMPLEMENT AN EPRESSION
TREE PRODUCE ITS PRE-ORDER, INORDER,
AND POST-ORDER TRAVERSALS
E.NO$0=
DATE$
-
8/14/2019 data stuture record.doc
58/109
SOURCE CODE$
3include4stdio.h5
3include4conio.h5
3include4ctype.h5
3include4alloc.h5
3define si?e &'
3define null '
typedef struct node
7
char data8
struct node9left8
struct node9right8
:
btree8
btree9stackXsi?eY8
int top8
oid main"
7
btree9root8
char e%pX\'Y8
btree9create char e%pX\'Y"8
oid inorder btree9root"8
oid preorder btree9root"8
-
8/14/2019 data stuture record.doc
59/109
oid postorderbtree9root"8
clrscr"8
printf=@n enter the postfi% e%pression+@n="8
scanf=>s=-e%p"8
top
-
8/14/2019 data stuture record.doc
60/109
ch
-
8/14/2019 data stuture record.doc
61/109
7
if top(5
-
8/14/2019 data stuture record.doc
62/109
-
8/14/2019 data stuture record.doc
63/109
OUTPUT$
Enter the postfi% e%pression+
abcd#9
The tree is created.....
Inorder traersal+
ab9c#d
Greorder traersal+
9ab#cd
Gostorder traersal+
abcd#9
-
8/14/2019 data stuture record.doc
64/109
RESULT$
-
8/14/2019 data stuture record.doc
65/109
AIM $
To write a program to create binary search tree and perform insertion and search
operations on it.
ALGORITHM$
S)#51$!tart
S)#52$Create a binary tree using linked representation with nodes haing the structure
0EFT$/T/RI,]T. Build the tree in such a way that alues at each left is less
and alues at each right is greater.
S)#5:$Insert+ ,et the alue to be inserted. Create a new node and set the data field to L.
then set the left and right links to N600.
S)#54$ Compare L with root node data in L 4root continue search in left sub tree. If
L
-
8/14/2019 data stuture record.doc
66/109
SOURCE CODE$
3include4stdio.h5
oid main"
7
int arrX&'Y-start-end-middle-n-i-item8
clrscr"8
printf= how many elements you want to enter in array="8
scanf=>d=-An"8
fori
-
8/14/2019 data stuture record.doc
67/109
else
endd found at position >d@n=-item-middle("8
ifstart5end"
printf=>d not found in array @n=-item"8
getch"8
:
-
8/14/2019 data stuture record.doc
68/109
OUTPUT$
how many elements you want to enter in the array
enter the elements '+(
enter the elements &+&
enter the elements +*
enter the elements +K
enter the element to be searched+&
& not found in array
-
8/14/2019 data stuture record.doc
69/109
RESULT$
-
8/14/2019 data stuture record.doc
70/109
AIM$
To deelop a program to implement the insertion on /[0 Trees.
ALGORITHM$
S)#51$!tart.S)#52$$eclare the node with leftlink- rightlink- data and height of node.
S)#5:$Enter the number of elements to be inserted.
S)#54$Mhile inserting each element the height of each node will be checked.
S)#5=$ If the height difference of left and right node is e2ual to & for an node then the
height is unbalanced at the node.
S)#56$ The present node while inserting a new node at left sub tree then perform rotation
with left child otherwise rotation with right child.
S)#5>$]eight is unbalanced at ,rand Garent node while inserting a new node at right
subtree of parent node then perform double rotation with left.
S)#58$]eight is unbalanced at grandparent node while inserting a new node then perform
double rotation with right.
S)#5?$To iew the tree perform traersal on tree.
S)#510$!top.
IMPLEMENT INSERTION IN AVL TREES
E.NO$0>
DATE$
-
8/14/2019 data stuture record.doc
71/109
SOURCE CODE$
3include4conio.h5
3include4stdio.h5
typedef enum7F/0!E-TR6E:bool8
struct node
7
int info8
int balance8
struct node9lchild8
struct node9rchild8
:
9root8
struct node9searchstruct node9ptr-int info"
7
ifptrD
-
8/14/2019 data stuture record.doc
72/109
-
8/14/2019 data stuture record.doc
73/109
case '+
ptr#5balance
-
8/14/2019 data stuture record.doc
74/109
ptr#5balance
-
8/14/2019 data stuture record.doc
75/109
9htinc
-
8/14/2019 data stuture record.doc
76/109
bptr#5lchild
-
8/14/2019 data stuture record.doc
77/109
rootd=-Ainfo"8
ifsearchroot-info"
-
8/14/2019 data stuture record.doc
78/109
displayroot-("8
printf=@n@n="8
printf=inorder traersals++="8
inorderroot"8
printf=@n="8
break8
default+
printf=inalid choiceDDD="8
e%it'"8
:::
displaystruct node9ptr-int leel"
7
int i8
ifptrD
-
8/14/2019 data stuture record.doc
79/109
inorderstruct node9ptr"
7
ifptrDd=-ptr#5info"8
inorderptr#5rchild"8
return '8
::
-
8/14/2019 data stuture record.doc
80/109
OUTPUT$
(.insert
&.display
*.e%it
enter your choice+
(
enter the alue to be inserted++(K
enter your choice+(
enter the alue to be inserted++(&
enter your choice+(
enter the alue to be inserted++&
enter your choice+(
enter the alue to be inserted++
enter your choice+&
tree is
&
(K
(&
inorder traersal++(&(K&
enter your choice+*
-
8/14/2019 data stuture record.doc
81/109
RESULT$
-
8/14/2019 data stuture record.doc
82/109
AIM$
To deelop a program to implement the priority 2ueue using Binary ]eaps.
ALGORITHM$
()#51$!tart.S)#52$ $efinition+ /n abstract data type to efficiently support finding the item with the
highest priority across a series of operations. The basic operations are+ insert- find#
minimum or ma%imum"- and delete# minimum or ma%imum". !ome
implementations also efficiently support ;oin two priority 2ueues meld"- delete an
arbitrary item- and increase the priority of a item decrease#key".
S)#5:$Formal $efinition+ The operations new"- insert- GO"- find# minimum or minGO"-
and delete#minimum or dmGO" may be defined with a%iomatic semantics as
follows.
S)#54$new" returns a priority 2ueue
S)#5=$mininsert- new""" <
S)#56$dminsert- new""" < new"
S)#5>$ mininsert- insertw- GO""" < if priority" 4 prioritymininsert w- GO""" then
else mininsertw- GO""
()#58$ dminsert- insertw- GO""" < if priority" 4 prioritymininsert w- GO""" then
insertw- GO" else insert- dminsertw- GO""" where GO is a priority 2ueue- and
w are items- and priority" is the priority of item .
()#5?$!top.
IMPLEMENT PRIORITY QUEUE USING BINARY
HEAPS
E.NO$08
DATE$
-
8/14/2019 data stuture record.doc
83/109
SOURCE CODE$
3include4stdio.h5
3include4stdlib.h5
3include4conio.h5
3include4malloc.h5
typedef struct heapstruct9p2ueue8
struct heapstruct
7
int capacity8
int si?e8
int 9elements8
:8
oid insertint-p2ueue"8
p2ueue initiali?eint"8
int deleteminp2ueue"8
int isfullp2ueue"8
int isemptyp2ueue"8
oid displayp2ueue"8
oid main"
7
p2ueue heap8
int i-ma%-ele-ch-t8
-
8/14/2019 data stuture record.doc
84/109
clrscr"8
printf=@n enter the ma%imum no. of elements in the priority 2ueue+="8
scanf=>d=-Ama%"8
heapd=-Aele"8
insertele-heap"8
printf=@n the element is inserted="8
break8
case &+
t
-
8/14/2019 data stuture record.doc
85/109
printf=@n the minimum element >d is deleted@n=-t"8
break8
case *+
printf=@n the elements in the ]E/G are+="8
displayheap"8
break8
case +
e%it'"8
break8
:
:
whilech4"8
getch"8
:
p2ueue initiali?eint ma%"
7
p2ueue h8
ifma%4*"
7
printf=@n priority 2ueue si?e is too small@n="8
e%it'"8
:
h
-
8/14/2019 data stuture record.doc
86/109
ifh
-
8/14/2019 data stuture record.doc
87/109
-
8/14/2019 data stuture record.doc
88/109
h#5elementsXiYd=-h#5elementsXiY"8
:
int isfullp2ueue h"
7
ifh#5si?e
-
8/14/2019 data stuture record.doc
89/109
:
-
8/14/2019 data stuture record.doc
90/109
OUTPUT$
enter the elements to be inserted+&C
the element is insertedWEN6
(.insertion&.delete min*.displayC.e%itenter your choice+(
enter the elements to be inserted+*K
the element is inserted
WEN6(.insertion&.delete min*.displayC.e%it
enter your choice+*
the element in the ]E/G are+#((*&C
*KWEN6(.insertion&.delete min*.displayC.e%itenter your choice+&
-
8/14/2019 data stuture record.doc
91/109
RESULT$
-
8/14/2019 data stuture record.doc
92/109
RESULT$
AIM$
To deelop a program to implement ]ashing with open addressing.
ALGORITHM$
Upen addressing hashing works on the same principle as other types of hashing8 that
is- it uses a hashing function h and the key of a datum % to map % to hXkey%"Y- and thealues are s tored in a table. The difference is in what is stored in the table. Instead of
maintaining a pointer to a linked list- the table contains the actual alues of
key%".Implementation The table is TX'..m#(Y where m is the table si?e- and the input is
%(-...-%n. The has function h is gien.
INSERT$
Me apply our hash function to the first alue+ the last digit is Q- so (Q goes in slot Q.
Ne%t is **+ this goes in slot *. Now we come to a problem+ the ne%t number- * should also
go into slot *. Me hae no linked lists- so where does it go^ In open addressing- a alue that
is slated for an occupied slot goes into the ne%t empty slot. * then goes into slot . K*+ slot
* is filled- go to slot . !lot is filled- go to slot K. !lot K is empty- so K* goes into slot K.The
table is usually made circular- so that a alue meant to be inserted into the last slot which is
occupied" is sent around to the front of the list. !o if we were to insert QQ into the table- we
see that slot Q is occupied- and QQ is sent to slot '- which is empty.
Note that if we hae more alues entered than slots- we can run out of room in the
table- and be unable to make any more insertions. For this reason- the load factor a in open
addressing can neer e%ceed (.' .
DELETE$
!ay we want to delete *. First we go to slot *+ ItVs occupied- but not by *. !o we
go on to the ne%t occupied slot. Me continue to do this until we find either the number weVre
looking for success"- an empty slot or we arrie back where we started. In this case- the
ery ne%t slot is occupied by *- so we remoe it and mark the slot as deleted. MeVll see
why in a moment."
IMPLEMENT HASHING WITH OPEN
ADDRESSING
E.NO$0?
DATE$
-
8/14/2019 data stuture record.doc
93/109
SOURCE CODE$
3include4stdio.h5
3include4conio.h5
3include4stdlib.h5
3define W/L ('
oid main"
7
int aXW/LY-num-key-i8
char ans8
int createint"8
oid linearprobintXY-int-int"-displayintXY" 8
clrscr"8
printf=@n collision handling by linear probling="8
fori
-
8/14/2019 data stuture record.doc
94/109
printf=@n do u wish to continue^_ZN="8
ans
-
8/14/2019 data stuture record.doc
95/109
whilei4W/L"
7
ifaXiYD
-
8/14/2019 data stuture record.doc
96/109
-
8/14/2019 data stuture record.doc
97/109
OUTPUT$
collision handling by linear probling
enter the number(*(
do u wish to continue^_ZN
enter the number
&(
do u wish to continue^_ZN
enter the number
*
do u wish to continue^_ZN
the hash table is......
'(
((
(
**
#(
K#(
#(
P#(
\#(
Q#(
-
8/14/2019 data stuture record.doc
98/109
-
8/14/2019 data stuture record.doc
99/109
RESULT$
AIM$
To deelop a program to implement the GrimHs /lgorithm using Griority Oueues to
find W!T of an 6ndirected ,raph.
ALGORITHM$
GrimVs algorithm is an algorithm in graph theory that finds a minimum spanning tree
for a connected weighted graph. This means it finds a subset of the edges that forms a tree
that includes eery erte%- where the total weight of all the edges in the tree is minimi?ed.
The algorithm was deeloped in (Q*' by C?ech mathematician [o;t`ch arnk and later
independently by computer scientist Robert C. Grim in (QKP and rediscoered by Edsger
$i;kstra in (QKQ. Therefore it is sometimes called the $G algorithm- the arnk algorithm- or
the Grim#arnk algorithm. The algorithm continuously increases the si?e of a tree starting
with a single erte% until it spans all the ertices.
()#51$ I5+)$/ connected weighted graph with ertices [ and edges E.
()#52$I')'!'#$new < 7%:- where % is an arbitrary node starting point" from [- Enew < 7:
S)#5:$R#5#!) +)' V# V$Choose edge u-" with minimal weight such that u is in
[new and is not if there are multiple edges with the same weight- choose
arbitrarily but consistently" /dd to [new- add u- " to Enew.
S)#54$O+)5+)$ [new and Enew describe a minimal spanning tree
IMPLEMENT PRIM'S ALGORITHM USING
PRIORITY QUEUESTO FIND MST OF AN UNDIRECTED
GRAPH
E.NO$10
DATE$
-
8/14/2019 data stuture record.doc
100/109
-
8/14/2019 data stuture record.doc
101/109
int n8
main"
7
int i-;8
int pathXW/LY8
int wttree-count8
struct edge treeXW/LY8
creategraph"8
printf=ad;ency matri% is+@n="8
display"8
countd#5=-treeXiY.u"8
printf=>d@n=-treeXiY."8
:
return '8
:
creategraph"
7
int i-ma%edge-origin-destin-wt8
-
8/14/2019 data stuture record.doc
102/109
-
8/14/2019 data stuture record.doc
103/109
7
printf=spannong tree is not possiable@n="8
e%it("8
:
return '8
:
display"
7
int i-;8
fori*d=-ad;XiYX;Y"8
printf=@n="8
:
return '8
:
int maketreestruct edge treeXW/LY-int 9weight"
7
struct node stateXW/LY8
int i-min-k-count-current-newdist8
int m8
int u(-(8
-
8/14/2019 data stuture record.doc
104/109
9weight
-
8/14/2019 data stuture record.doc
105/109
:
:
min
-
8/14/2019 data stuture record.doc
106/109
fori
-
8/14/2019 data stuture record.doc
107/109
OUTPUT$
enter number of ertices+K
enter edges >' ' to 2uit"+'(
enter weight for this edge+('
inalid edgeD
enter edges >' ' to 2uit"+(&
enter weight for this edge+('
inalid edgeD
enter edges >' ' to 2uit"+&*
enter weight for this edge+(
inalid edgeD
enter edges >' ' to 2uit"+&
enter weight for this edge+
inalid edgeD
enter edges >' ' to 2uit"+*
enter weight for this edge+
&
inalid edgeD
enter edges >' ' to 2uit"+
(&
enter weight for this edge+&
-
8/14/2019 data stuture record.doc
108/109
-
8/14/2019 data stuture record.doc
109/109
RESULT$