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

DS Unit 1

1. The document discusses data structures and algorithms, describing them as ways to organize data to provide efficient access and solutions to problems. 2. It describes the two main types of data structures as primitive and non-primitive, giving examples such as integers, characters, arrays, and linked lists as primitive and trees, graphs, stacks and queues as non-primitive. 3. The document also discusses algorithms, defining them as well-defined computational procedures that take inputs, process them, and produce outputs while satisfying properties like finiteness and definiteness. It describes analyzing algorithms based on time and space complexity.

Uploaded by

satvikisback
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

DS Unit 1

1. The document discusses data structures and algorithms, describing them as ways to organize data to provide efficient access and solutions to problems. 2. It describes the two main types of data structures as primitive and non-primitive, giving examples such as integers, characters, arrays, and linked lists as primitive and trees, graphs, stacks and queues as non-primitive. 3. The document also discusses algorithms, defining them as well-defined computational procedures that take inputs, process them, and produce outputs while satisfying properties like finiteness and definiteness. It describes analyzing algorithms based on time and space complexity.

Uploaded by

satvikisback
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Page No.

Date 5Sept a

UNIT-L
dATA TRUCTURES
DS
roblemn

SolutionS CAlgorithm

roqram
Efficient"

oTh time wth Space

DS Data Shuchre is a way o 0qanisingdata


that rovides houw to Atnte data and how to
acce.ss data.

YPES OF DATA STRUCTURE

Nhereis qenEhallytwo tupes o DS

imihye
Non- iminye deniveda om pimitve )
Page No.
Date Page No.
Date

DS
Analuses a Algorithm

Non- Pimitive DS
Bimiive
ime Space
Chahacteh line.ax Nan- lineaa Comblexits- Comblexi
LFloat nteqer
CHirarcha) DS) imeomhlexitu=ltanaluse the time takenbu olgo
t o eneu te iHs task
Liti Tree Garaph Space Comblexi t analuse he spaae taken bu ann
alaorithm exclo de it
Arrau kinked list Stack Queue DS
aini Comblexitu
ALGORITHM:
Time Comble xity- iSpace Complexiy
His awe)l- defined Combutotionalprocedure
thai
akesr omeor het oinputitand produce one or
Aetso Joutput no. okinbuts3 no.o noo mo
inSmuchons inbutsS
Cn)
instruchone
An it also satifies theollowingpropexties
Asymotic
nput O atn Natations-
Outbut20
Finitemess Nheu are unctong hat are Sed to analyse
Deiniteness the behaviou o an algorithm
eciveness
Q Bia oh 1ithe ob
B i q omeaa 1ittle omeaa
O Theta
Page No.
Date
PageNo.
Date

Notations 0n Ont) o(nn Q Notafion E is called Ubbe hound


s Used worgf cabe analusig

mlt uill un
dan timesSo i belouo o n read as
Print (Hello )
Can be_ LutlHen
cbositiue
Constants
atheir exist) Gome the Constant C
Qnd no- (such that) na no a no O-
t wil un n
times or s
Pint( Hello), loob then again 3 dn 3n
n imes or Sec tna
Loop So On)

E-3 ori-0 iqniat)


t
print(");

It will 2un rsi rabh o Notai


bxintC"u; n hmes hen
move ta ot
o Ck0 knk) loob gain b ir guea- (n) dn+ n 0 (gn)): 2
n imes then a i
paiat 2 ) n hm eA

mmm) mn ntn Cn
dn m
OCn)
c3 n +n 3n
A

V
Page No.
Date
Page No.
Date

C3 Cfails conditioni- n) aCn)


NOu
butiag 2
n n 2 2n some the constantc C 8 n
n no

Su- n r n &(n2)

nin
n10On c n owt Pp21 }aANEE bound L e bo un d
Ppet
2 C-
5 + 10
n n+n n2-
Aet n-
C 155 Qu-2 n2) ind ac
Sn 10n 5
lo reachclosest Value
ne 10 Lueknou m - an n
5n* 10n Sn
For toLAbound,
(3 Notaion- (Aeraqe boun d ) Lowe
Cn n

C g(n)
0t n= 0-
n) Cnoi catis tied

At n6

3n n An
D
Page No.
Date Page No.
Dato

dosen t ne ed auxi
tmeis more and we

oirtAhace ArY Kepresemtatian_în memory


ARRAY
huwo tubes
is aCallechion o Smihak pes oY
Aray
homoq eneous elements under a Comm.an namhe
Kou major order Columnmajor order
iS a comtiquouusmemoru allDCaHON
N rebresendatiaa rebreAeDtaHon

Arau dan be aaces.sed bu eheUing0dex


bu dddress. peaakna
Suntax- datatype HHAuname size One dimenaional lwo dimnensíonal Jhree dimengion
1D D KD
dbeinserion or acces.sinq elemen fom ar
0hau heouiredn below oO( Hme One row and mulhble m-rowS and
Column n-columos
n ( lanquoqe

inta lsl
int a fs 214,210 ini.af2121 12 4
K a 1 al al2) a3] aC4? Cin dex no)

2000
Kous maoT Order
oo o0 4 d006 008 (addresses)

eha a Ls1
a Lol aC al2] a[33 alu1
o2000 0ol dooa do03 a0o Coumn maoYAYder
|Page No.
Date
Page No.
Date

xambles a121 2000 +

c Raw major order


Rou ma =Loo0ilr) BA wN-) (i- A
allcatas 5o butes
int a LsIIs1
4] [21LO 2C4)_
phere Size o he memoru
index numbe y
BA bane address.
YOw TOU-5 YOu) -4 ES-
rous-1
Quesnt a [3J[33:
Used in C Puthon and Java ato to)
aLinto a C23 Lo3 aL2121

i Colunmn maior order


18 buteA
int a [ILS]; allocates 50 butes

a L31c3i ar aL3,31 w bute baseadd-200o


ind
D Loc (a Cilr1 2
sed in Fortran Le Rnout,
Loc a Ci1(1) BA N Ci-&,+(-)
Loc a Cillil)
Lac a LiL)=Aa0o23 -a)+(2-o
Let a L U e Ucl
010

N Ue-le * no oColumDs

a 12] Bane address t i


dooo Aize d meMoru ach aivenanoya 12 8,3. 9
element
Page No.

Date Page No.


Date

bane add d020 w butes nd .alumn majar 0Yden


à loc (a [u[s))-
Li Loc (a C1]Ls61)

Arrau
Lwe have aiven Al2.1, 381 BA= 3000 and
2 Ug 8 widh L bute nd i loc la3irul-
locCa [ulrs)
So
Ma a - 1 2-2+1 (noa Here 2 U, 7
N : De- le +1 9-3+1 no ol columns BA-3 000

we knoe Loc La L31r4)= 3000 6 (4-3)+ (3-2


Lac laLilg)= BA w lNi-1) +(G-A 30D0+ %.+
=3000+28
i loc la [4][s])a 2040 t 1(4-2) + (5-3) 3028
00+ 2 +9lL
Aoc Ca [ul[41)3000+ H[6(-32 + (4-2)
d0 84 3000 4 3 +21

o c a ina1):do 20 [7-2) + g-3)1 3080


00+ 475 +51
0.20 160 AL-2. 73 81
218 0

LiLoc la [2][5]): J0A0 +4 1(-3) + (5-3J


dO0+4 [21
0 8
F
Page No.
Date Page NO.
Date

AdvantnqeA=- LCoe-

once and xeeute mulHble Hmes.


Urite
takes_less time AB C
no. moveA=

Drawbackg

mhnite eCHSive call then e menmoru Ulisafon


hecomes Ye lo or_SuSem may CHasb_ mOveA -1

louwer o Hamoi LPROBLEM oluhon UAingrecumion

ing- hzee ou we need o ans e TOHn AB C)


n dieees rom one holeo anoher pole bu
using hird pole. TOH n- AC B)
prini A h c
Condiion-i We _lan maye only ane disc at a TOH h-1 B AC
Hme romone_Dole O_an the print o A

Sn_the movement ok- dise_higher bize is Numben Suharoblems


0annotbe blaaed OveLAmallen disc.
Tn-) T(n-2)t
Cabe-1L AC T (n) 2 1 n-2)+ 2F1
T (n-2) 1 (n-$)
n=1. 1 n)- 22 T (n-3) +2*+2t
B
nTh
no moves =
1(n Ln-k)+ ---- tt
Case-2 T n) 2K T (O) a

h-2 ho. o moves 5


A B
F

TOH3

ToH

C To ToaC2|3)
H.23 ToH 1,
TH (0, ) oH
To (,3 2 p) oHCo23
Stop Stop
Sop

ToH c,32 2 H(0, ,32

top
To,13, ToH3,13)
Stop
Stp Srop

loH, A,8,) ues


Page No.
Date Page No.
Date

q
paint " Entex the value o memoyMu Qllecated then ît retung vold-
seanf Z d &b= p AShte be ainter
=p nt2
7 9 memoYu S Yot allocafed ben YefutnS
INULL
int Zd"b

ZunamicMemory Allacahitn Suntax datatube Vaname(tunecast callaa


m n Size a datatibe2
Atd libh
no, o
blacks _elemens 2 oDe elemen

malloc ( Calloc( ree realloc 9 standsar Cuniglou memor allecotion 1


allocates m4 no o bloa ks and ach blocR Aiye is
Syntay: int b (Lcast tube*malloen* size o n * size odatadsbe
Ldatatube
dekaultiniial yalue_ Cach element 1S Zero.

inth lint * malloc (n eize of lin 1 aehuns_Void tube address o r s t block allocakd
hu e 0S,obexahing Byslem
NDLL
the uequested memoruar not allocatRd h e n
Dxint N InGuf Hcient memnors
rennsNULL alue
ehun lo);
3 ee) de-qlocathan
else
Void lne þånteu
Suntax ee (pointer)
be atoTd
int p z (int mallog Size o at)
mallc bu delali slpie btatements
qaubaae
eeCptr
Page No.
Page No
Date
Date

sreeall: (De-allecatian) Abblicaion hed o rebrebentqrabh or tree


UAed do imblement directoruAmuctuNe.
realloc. oed o imblemet Atack or Queue

untax realloc pointer neuasize- ForLwad and hack uasd MONemnent in a combutez

it
Ayskem imhlemenled bu linked lisi En-media-
int 7hh= (nt ) malloc ( n s e blauer
Atakemet

ealloeLptr n r size o i n t - upe a inke list


ree ph
ubea
LiNKEDLST
head Circula Singlu Ciroulan doud
u000 Node 1 Sianallu Oouhlu
Address o nent_nade
data |6oao Node 2
1000
Sianally linked list-
dataz Node 3
Lwe can movee In_onlu wad direotion
6000

Ldata 3 - }0, 000


head nodel Aode 2_ nede

n alinean data biruehine (bequ enha like asaa


but 1t 1s n0n- cenfaqiots memoru lacanon Hoo to CreQle_g n0de

Advantaqe o linked list is_we can add or delee data Addres


any node wih minimim_eiiorT OY ube combleyi
Atruct node
YOwback: ue can OcceAsanu lement C
enl in
node
AeQuential mánner (andom_ acces datatyhe data,
not alloLsed) Aiructnode nevt
Page
Page No.
Date
Date

a print enler 0 data


node head
suet m a l l o c A z eo s u c t
noe
de S Canfd Zd áp dota),-
b(suet node SMuctnode Ne XE = NOLL 7* head
de )
maloc A l e n
a e (shuet node head = P
hile (n> D
ncde 2
headp truc node mallnc ( > e of (cruci
Temb
entey data o next node
prinr
exit ( 0 - Zd tenp data)
scan
temp neT= OLL

node ube D 0Ekt = temb


create Vaiable o temp node becomeA-
Kseudo Code 2 p=tenmp
Pllocate memoru Heu data / =
3 Reunn Oddres oncde

tunction in C- MOidCreate

check 1 ist is emH oxnot


ncnno
SHuci mnde b Su- write a
p = ( u c t ode 2 mallac4lze 2
Stroc node No1d emph

1 head= N u )
printf( iet is emPhy
Hous to creale a singlu inR.ed lst 2
printE e t ig mot embtu
head
data n dad ne

Create O ncHon
Psevdo code NL nen
nead G equol
veid create (int n) iSt iG emp
heiS nat emb
3 0heHLoise
Dtrut node tem
(Stuct ngde ma lo e sizea Aruci nod
Page No.
Date Page No.

Date

heNuMhes
u rie aunctionh count Write abrngram in C t ereate N node and aSa
Yode preent in a singly liaked list_ he ODImaion of each Node.

FOL Count nclude Gtdio-h)


include conio, h
Ahuet node *temh- Einclode std lib.h)
temb head: Sruet Node_

int o 0-
while temb 4 nent e NOLD- int data
Auct nodenext
head
Lemb ztemb9 next vaid oreate (Int n)

Fe tuAN - tuct node *h temp


b(shruct node * ) malloc (izeolSuct node 1
hrint CEnteY he data "2;
Write a nction to brintinfornmahian ofeach_n Acanf 4 d " & p> data) a
in_ asingle linked list p nent=_AULL
head =D
VoidprintlistO bile n>1)
smuct node * temb temb = (sruct node *) malloc (Sizeof (struca
iemb=head, n.ode*)
Lwhile temb nexT = NULL)_ brinttcEnter data o next node")
scanf7d x temb da ta)2
pxint data n node Zd" tCmb dain temb 9 neys NOLL
int Pddress o next node 1temp D 2 nexf = temb
temb temb Nex tem

head yoid main (


000 dada next emp

uono 3
Page No.
-

Date PageNo.
Date

reate (n)j 1)
Drintist printf C faund ");
else
Void printlist( ) DxinT Not kound ";

3truct node *temb-


tembhead OPERATIONS INuNK ST:
while temb_=NULL)
L LgmsEH hon in a link lig-
int data on node 4d temb doio.
brintt addressina neKinade Au emby nsent a nade at the atout a inglling liel
temb temb 2 neuF M00
hea d
Create anes mode
P X
temp
T 8ooo

u dite a unc hon o seanch an element in_a link lst


(linR neu noide t i f h ligt
yoid eanch lchan Aeach temb next =head

Sructnode temb; T4000


6000
temb head temp

Lwhile kLAéK A dte éle R) (temb = =NULL)


G)Set neuspointer to taHT (head
temh data== SeaHD head=temb
Lemk/ztemb mest hea
lag
break- fonctiom:-
vaid ost int_inbeset elemeat
temb temb 2 nent-
hhruet node temb
temb-bmuet node Jmallcc.izeaStuct node
Page No.
Date
Page No.
Date
Ined
temb nent NDLL mid nsed (datatube Reus datatube datakes
insesd element oplenthat
tema data
tembdneut =bead Sruet node *P, * Lemp
head temb- tem (Arucf node malloc (Aizeo (Atruct node
temp data Reu
temb next s NOLL-
the last o nk ist-
inset a node at p=head
uhilep data datakeu
Create a aa node tem
ube
-Create a neu Vaiable pain ter S nex
taut Chead)
Set neuf pointer tob
temb nent =p net
Void Atast iat nsent elment neyt emb

Ahuet nade ttemb -


temb =(smvet noede ) malloc Sizeo Lshuctr 2DeleHon o a node in alinked list-
temp neyt 2 NOLL,
temb data = ngerf elemenT i i r s t node de lete p=head1
b head head =head Ment
while (p2 Next |e NULI) ee P )

AA last node
iretnodo lastnDde
p nexd s temb
D nent eNULL

u Write aunciono insext a node in be twet LA intexnal node p temp q


linked list. next =temp Ehead

head pei
heod Dnents4nent
1 R 3
Page No.
Date Page No.
Date

Functionodelete anode Ou Urte aunchon to ins.ext a node in a barted


inked list
delete ( datatupe Rey
Lyaid insent shmuct node head stoct no de*munode
uct node p Shruct node Current
p head
Q head nex Curent Ahead
p data= keu deletel node srom t a i bhile (Corent neut NULL &6 current next data
munode data
headep2 next
ree p) COrrent CuHent nemt

CISE Munode nent cumentnext


CUYYent néntuE munode
while o data e Rey)

ppnext
anent Circwlax Snaly Linked List
NULL) intexnal node
Head
pnent :0 nex
reela
else lastnode
in_a cireula nked iet we can directlu MOVE

D next =NULL- om last node the etnode


ree r
Abblication- Implementin Queve and stack
2lmble.menhon irewall Heat
3 Joimplement OUnd Robin bcheduling
in opeHa finq ucem. to schedule
Page No. Page No.
Date
Date

he CPQ
aAt Stont
DrOGesS om
bAt end
memoruspaCe_ eAt internal
rauwbacks-i E reauired eyMra
o eut nodle
ore Qddress
2 We Can move
onls n onedirecon
(aInsenhnon o no.de at Atast

circulax singu linked ist void stast (int insext element)/ insent-at stant
Hou) to reate a
stuct nodetemp
Void Create int n)
temb Atuct node)mallac (sizea (Shuct nade
temb2mext-NULLS
Bruct node h temb -
node temb data a insext element
b (shuct ndde ) mallo.c Aizeof Lshuct
Enter the data ") temb nent head
pint shrct node
head temb
Scant( 4d 8p data);
p net : NULL; Dnext : temb
lot data
head =p -
nead Shruct node next
while (n 1)- 7000 headlast

temp -sruct node *2mallecsized (buct ned


printf( Enter the_data o nent node ),-
Scan d%tembdatal; temp 7 09o

temb next =NDLL


P next : heed
Lemp temp
ibneeion o node at last.

inser_at dast
D next - head_
shuct node * temb
emp stract node *) mallo.c (Aize.ok (stuet node
write o nction to inset node in a ciraula orintfEnter data of ne node
inRed list Scanf(d temb datal;
temb nexi N0L
Page No
Page No.
Data

Date

Aast next tem> Hout to CLeatedoubly linked list 2-


Temb next = head
last =temb Create (iat n)

.- Shuct node p * temp last


ioAnsextion o no.de in intennal Shuct node mallac (Aize ok (amuat node D
print (u Enter the data "
insertat internal int keu SCan CN /.d p data)
NULL
Shmuct nodertemb b3- p rey NULL2
temb =(shuct node *)mallac headp
brint Enter data a neu node "; las
Acan (Zd&tem data; while (n).
temh neut NULL
pehead temb =shuct mode )malloc (Size o (etucfnodel
while p data |= keyl priot(Endev the data of next node -
Gcanf Zd temp datal
-p ment Temp Nent =NULl
abt nentEtemD
femp nexi pnent; Lemppre last
last emp-

aDoubls ink list

n doubly ink st uwe move in both orwara a Insertion în Dnubly linked list
and backoand dixechon.
head
head 000 30 00
lous
2000
oo
000
prev
temP
neT m
next
PageNo.
Date Page No.

Date

OA
At stant insert at start Transvere inreverse

Smvet node p - at
malloc (izeof Lstuct
Srucf node *em
p=Lstuet node femp head
prof CEnex data o newnd ratta2E Y- last NULL;
Scanf("7d" &p> data); p 2 prev NUL
while temp NULL
next = NU11

pnext head
last tempprev
headpeyp temb temp next
head =p
last last next
temp last
A last: insert-at labt O while temp = AULL

Suct node *p femp q Priot Data _of node ctemp da)


p (Suct node malloc S1ze o sh
femp temp 2prev-
Ddata m
pprev =NOLL,
nent -NOLL
temp head Lrite a _ pra>ram_in C to xemave_duplicake elemens
while ( temb NOLL) rom Qiven _remove

temp 2 brev AuWrite a uncion to mege two Ainglu inked list.


temp emb> nENt;-

^next
nent =b;
pprev4
nt DLL in reverse order
Page No.
Date
Page No.
Date

o DLL
nsert in behueen (internal) Noid delL-astart)
head
3 o00
Sruc node *p
3o phead
head-bead nexti
head prey NULL
new node
ree p)
Void insert.at-mid (int key)
At lant
Shuct node a node s head
=(shuct node *) malloc(sizeo (sheuct O00
print("Enter data o node);
ScanfZd" 2h data);-
b prev NULL Vo d del-ak_last
next- NULL
4 =head Sruct Node * last lemb-
while (qdata=keu) last head
Rmb - head
= next ohile temb NULL

bnext q2 next lantRmp


Rmp=emp next-
pevE
pa nex prev=p labtprey next =NULL
ree last;
wriR a un.ction to deie a node in DLLL

daAtstart Liin hetueenlist


m
hea d
000 4000 300
2000
lem 3o x
prevnod netnode
VO.

Date
Page No.

Date

Gint keu
Vaid delat mid Cabes
SMuct_node prevnade netnode

bead next,- pdeq e =a degl


Prevnode aepreveaea
phrCoff=pco tq cof
a 2 next pt deq= p dea
next rode
D next
Lshile (qdata le Rey
next
nest;
pdea 4deq Auat node
prevnode z 42pev
ph dea p deaj-
next oode z4 2_next-
ph co p _coPf fot coffdeg
pEpf2 next sructnode * next-
prevnode next nextode
h1Fh2 *h
nextnode prey prev node-
3 ( p deq<qdea)
fee lqj p degqdeg-
ph cof cof
4next
ADDITIOAN OF Two PoLyNOMIALS 6y UsING SnG
LINKED isT:
Ql) 4
E_pn) 3xt Bnt5
Co

Cobes
h cdeg rext h2

pdeadeg4deg)
pf cot
5 L733 4ON
h2
i p - nezt ls NOLL && 4 next NUlL
ptr2next p

i p next ==NULL 3 K qnext 1z NULL)


pt next z4

You might also like