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

EC2209 Algorithms

The document discusses different data structures and algorithms. It explains how to implement queue, stack, linked lists and binary search trees using arrays and pointers. Algorithms for operations on these data structures like insertion, deletion and traversal are also provided. Implementation of priority queue and quicksort are discussed as well.

Uploaded by

Raghu Rocky
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

EC2209 Algorithms

The document discusses different data structures and algorithms. It explains how to implement queue, stack, linked lists and binary search trees using arrays and pointers. Algorithms for operations on these data structures like insertion, deletion and traversal are also provided. Implementation of priority queue and quicksort are discussed as well.

Uploaded by

Raghu Rocky
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

DATA STRUCTURES AND ALGORITHMS LABORATORY 1. Queue implementation using a a!

s Aim To write a C++ program to implement queue using arrays. Algo it"m 1. Declare an array Q of size N. 2. %. (. ,. -. /. 2. Assign F an ! to "e t#e front an rear pointers of t#e queue an assign $ to F an !. &et t#e new element ' to "e inserte in to t#e queue )f ! is less t#an N* insert ' at t#e en * "y incrementing ! "y 1. +t#erwise isplay
queue is full.

)f F is zero t#en assign F to "e 1. To elete an element c#ec. w#et#er F is greater t#an zero* t#en elete an element pointe "y F* ot#erwise isplay queue is empty. )f F an ! are equal t#e set F 0 !0$1ot#erwise F0F+11 Display t#e queue Q from F to !.

#$

Sta%& implementation using a a!s$ Aim To implement stac. using arrays Algo it"m 1. 2. %. (. ,. Declare an array 3 of size N. Assign T+4 as a pointer to enote t#e top element in t#e stac. &et t#e new element ' to "e a e in to t#e stac.. )f T+4 is greater t#an or equal to N t#en isplay stac. o5er flow1 ot#erwise set T+40T+4+1. 3et 36T+47 0 '.

-. /.

To elete top element from t#e stac. c#ec. if T+4 0$*t#e isplay stac. un erflow* ot#erwise ecrement T+4 "y one* an Display t#e stac. 3 from 1 to T+4. isplay 3 6T+4+17.

'$

Singl!( Dou)l! an* Ci %ula lin&e* list implementation an* all possi)le ope ations on lists Aim To write a C++ program to implement singly* ou"ly an circular lin.e list using ADT Algo it"m
SINGLY LIN+ED LIST,

1. 2. %. (. ,. -. /. 2. @. 1$. 11. 12. 1%. 1(.

3et a no e to contain )NF+ an 8)N9 fiel s. Allot memory ynamically for a no e an 3et 31;<)NF+0N1 an 31;<8)N90N=88. !epeat t#e a"o5e two steps for all t#e elements. A no e can "e inserte at t#e front* in t#e mi >;<8)N90:;<8)N9 an :;<8)N90>. To insert a no e > at t#e en tra5el till t#e en of t#e list an assign t#e last no e?s 8)N9 5alue to >. To insert a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e . 3et >;<8)N90';<8)N9 an ';<8)N90> A no e can "e elete at t#e front* in t#e mi le or at t#e en of t#e list. To elete a no e > at t#e front set :;<8)N90:;<8)N9;<8)N9. To elete a no e > at t#e en tra5el t#e list till t#e en an assign t#e pre5ious to last no e?s 8)N9 5alue to "e N=88. To elete a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e 3et ';<8)N90 ';<8)N9;<8)N9. To searc# an element A tra5erse t#e list until A is foun . le or at t#e en of t#e list. To insert a no e > at t#e front c#ec. w#et#er t#e list is empty* if not set eclare it as a #ea er :. To create a singly lin.e lists get t#e element N an allot memory for a no e 31.

DOUBLY LIN+ED LIST,

1. 2. %. (. ,. -. /. 2. @.

3et a no e to contain )NF+ an !8)N9 an 88)N9 fiel s. Allot memory ynamically for a no e an eclare it as a #ea er :. To create a ou"ly lin.e list get t#e element N an allot memory for a no e 31. 3et 31;<)NF+0N1 an 31;<!8)N90N=88* 31;<88)N90:. !epeat t#e a"o5e two steps for all t#e elements. A no e can "e inserte at t#e front* in t#e mi >;<!8)N90:;<!8)N9 an :;<!8)N90>. To insert a no e > at t#e en tra5el till t#e en of t#e list an assign t#e last no e?s !8)N9 5alue to > an set >;<88)N90last no e?s !8)N9. To insert a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e . 3et >;<!8)N90';<!8)N9 * ';<!8)N90>*>;<88)N90' an >; <!8)N9;<88)N90> le or at t#e en of t#e list. To insert a no e > at t#e front c#ec. w#et#er t#e list is empty* if not set

1$. 11. 12. 1%. 1(.

A no e can "e elete at t#e front* in t#e mi le or at t#e en of t#e list. To elete a no e > at t#e front set >;<!8)N9;<88)N90:*:;<!8)N9; <!8)N90 >. To elete a no e > at t#e en tra5el t#e list till t#e en an assign t#e pre5ious to last no e?s !8)N9 5alue to "e N=88. To elete a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e 3et >;<!8)N9;<88)N90'* ';<!8)N90 >;<!8)N9. To searc# an element A tra5erse t#e list until A is foun .

CIRCULAR LIN+ED LIST

1. 2. %. (. ,. -. /.

3et a no e to contain )NF+ an 8)N9 fiel s. Allot memory ynamically for a no e an 3et 31;<)NF+0N1 an 31;<8)N90:. !epeat t#e a"o5e two steps for all t#e elements. A no e can "e inserte at t#e front* in t#e mi le or at t#e en of t#e list. To insert a no e > at t#e front c#ec. w#et#er t#e list is empty* if not set eclare it as a #ea er :. To create a singly lin.e lists get t#e element N an allot memory for a no e 31.

>;<8)N90:;<8)N9 an :;<8)N90>. 2. @. 1$. 11. 12. 1%. 1(. -$ Aim To implement queue an stac. using lin.e list. Algo it"m
STAC+

To insert a no e > at t#e en tra5el till t#e en of t#e list an assign t#e last no e?s 8)N9 5alue to > an >;<8)N90:. To insert a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e . 3et >;<8)N90';<8)N9 an ';<8)N90> A no e can "e elete at t#e front* in t#e mi le or at t#e en of t#e list. To elete a no e > at t#e front set :;<8)N90:;<8)N9;<8)N9. To elete a no e > at t#e en tra5el t#e list till t#e en an assign t#e pre5ious to last no e?s 8)N9 5alue to "e :. To elete a no e > after t#e specifie no e '* tra5el t#e list till t#e no e ' is reac#e 3et ';<8)N90 ';<8)N9;<8)N9. To searc# an element A tra5erse t#e list until A is foun .

Queue an* Sta%& implementation using lin&e* list

1. 2. %. (.

Create a singly lin.e list. To 4=3: a no e > tra5el t#e list until t#e en is reac#e . Assign last no e?s 8)N9 to >. To 4+4 a no e > elete t#e last no e an set t#e pre5ious to last no e?s 8)N9 to N=88. To isplay t#e stac. contents tra5erse t#e list from t#e #ea er till t#e last no e.

QUEUE

1. 2. %. (. ,.

Create a singly lin.e list. 3et first no e as F an last no e as !. To insert a no e > set !;<8)N90>1 To elete a no e > c#ec. w#et#er t#e list is empty* if not set F0F;<8)N91 To isplay t#e queue contents tra5erse t#e list from F to !.

.$

In/o *e ( 0 e/o *e an* 0ost/o *e T a1e sals Aim To perform )n;or er* 4reor er an 4ost or er tra5ersals in Binary 3earc# Tree Algo it"m 1. 2. Create t#e "inary searc# tree To perform in;or er tra5ersals a. ". c. %. a. ". c. (. a. ". c. process t#e left su" tree process t#e root process t#e rig#t su";tree process t#e root no e process t#e left process t#e rig#t process t#e left no e process t#e rig#t no e. process t#e root no e.

To perform preor er tra5ersal

To perform post;or er tra5ersal

2$

Bina ! sea %" t ee implementation using lin&e* list an* possi)le ope ations on )ina ! sea %" t ees Aim
To implement binary search tree linked list and possible operations on binary search trees.

O)3e%ti1e To write a C++ pgm to represent "inary searc# tree using ADT an perform its operation E4e %ises 1. 2. Create t#e memory space for t#e root no e an initialize t#e 5alue to zero. !ea t#e 5alue.

%.

)f t#e 5alue is less t#an t#e root 5alue *it is assigne as t#e left c#il of t#e root. Alse if new 5alue is greater t#an t#e root 5alue* it is assigne as t#e rig#t c#il of t#e root. Alse if t#ere is no 5alue in t#e root* t#e new 5alue is assigne as t#e root. T#e stepC2D an C%D is repeate to insert t#e En? num"er of 5alues.

(.

Sea %" ope ation 1. 2. %. !ea t#e 5alue to "e searc#e . C#ec. w#et#er t#e root is not null. )f t#e 5alue to "e searc#e is less t#an t#e root* consi er t#e left su";tree for searc#ing t#e particular element else if t#e 5alue is greater t#an t#e root consi er t#e rig#t su" ; tree to searc# t#e particular element else if t#e 5alue is equal t#en return t#e 5alue t#at is t#e 5alue w#ic# was searc#e .

Inse tion 1. 2. %. !ea t#e 5alue to "e inserte First perform t#e searc# operation to c#ec. w#et#er t#e .ey 5alues is ifferent from t#ose eFisting element )f t#e searc# is unsuccessful* t#en t#e .ey is inserte at t#e point t#e searc# is terminate .

5$

Qui%& so t implementation Aim To implement quic. sort O)3e%ti1e To arrange t#e elements using fastest sorting tec#nique quic. sort an t#e time ta.en to sort t#e elements. E4e %ises 1. &et N elements w#ic# are to "e sorte * an store it in t#e array A. 2. %. 3elect t#e element from A6$ 7 to A6N;17 for mi le. T#is element is t#e pi5ot. 4artition t#e remaining elements into t#e segments left an rig#t so t#at no elements in left #as a .ey larger t#an t#at of t#e pi5ot an no elements in rig#t #as a .ey smaller t#an t#at of t#e pi5ot.

(. ,. -.

3ort left using quic. sort recursi5ely. 3ort rig#t using quic. sort recursi5ely. Display t#e sorte array A.

You might also like