Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Daniyar Shukurov

ELCE 305

Assignment 3

AUl pseudocodes will be written on


P

7thon
a
class Notes

Jef ainit ( value


.
):
self,
self = value
.value
self None
prev
=
-
self next = None
.
class List
DoublyLinket
.
jef sinit (
self):
-
self head - None
,
self tail a None
.
Jef Ensert (

знд
x
):
self.
-lerst
Xinext - selt hedd
.
if seltinead
:
sell head prev = x
,
,
self head =

x
.
if not self

-tai.l:
self tail 2

x
.
tef ensert (

j
x

-last
):
self.
self tail
x
-prev
=

.
if self tairl
pazY
.
.
self next 2

x
-tail
self 2
x
-tail
if not self
-head:
sefff hedt : x
.
-
tef belete (

7
-first
selt):
if not self

РЕ
-tail:
return " empry
г
E
List
if setf head 2
self tail
,
a
.
:
self head =
,
Nene
self a
aone
-tail
elses

self seff
-head-next
a
-head
selt head Nene
prev
a

.
tef telete (

-last
self):
)
it not selt

.tai.l:
PET

hese furctions tme Oli


compexity =

since
they anvolve
only manipulations with
pointers
without ferations
.
class Notes

Jef init ( self value

.
):
,
selfovalue = value

self None
=
-prev
self next = None
,
class List
DoublyLinket

:
jef init (
-
self):
self head - None
,
self tail a Nome
.
tef insert ( X :
-last
self.
)
self
x
-prev
-tail
=

if self tail
.
.:
self tail next =
x
.
self 2
x
.-tail
if not self
-head,
sefff -
.
hedt
x.
def remove between ( Xpc Xi :
-notes.-
)
selt;
if not X or not X

2:
1
return O

.
new boublykinked Last ()
-list
=

if
Xiprew
:
xprew next X

2-next
-
.
else
:
self hedd =
X next
.
2
if tn next
.
X next prev xapprew
-
2-
.
elsez

sell - Xo
prev
-tail
41 prev a ovone

X next -
None
2-
new heat = x
.
-list
new tail =
42
-list.
return newlast

Same in
A time complexing O
(
as =

since wo use
pointers again
O
C

class Node

:
Jef init self value

.-(
):
,
selfovalue - value

self None
prev
=
-
self next = None
.
class List
DoublyLinket

:
jef init - (
self):
self head - None
,
self tail = None
.
fef insert ( 4):
-last
self.
= self
x
-tail
prev

if self tairl
.
.
selt next =
x
-tail
self 2
x
-tail
if not self
,-head:
sefff hedt =
x.
,
Jef
splice clist affer (
-
*
ch2):
self,
of not X or not La

:
returr 0

Ir not ta

-head,
returro

h head =

x
2,
-prev
if x next
.
next L taal
x
prevw
-
2
.
.
t next 2 xnext
2-tail,
else
:
self L
2-tail
=
-tail
X next a
Ly head
Lalead =
None

ha - Neme
-tail
Q Tome
compledicg 20 same as in
Я
l0
L),
Of class Nose

:
Jef init ( self value

.
):
,
selfvalue - value

self None

-prev
=

self next = None


-
class List
DoublyLinket

:
jef init (

self):
-
self head - None
,
self tail a None
.
Lef Ensert ( x
):
self.
-terst
Xinext - selt hedd
.
if selt
-head:
seld head prev = x
,
-
self head =
x
,
if not self
-fail.:
self tail 24
.
tef insert ( , 4):

-last
self
self
x

-prev
-tail
=

if self tairl

.
.
self tail next =

x
.
,
self 2

x
-tail
if not self

-head:
sefff hedt :
x.
-
tef selete (
-first
sell):
if not self tail
:
return " empry
List
if self head self taidl
,
a a
.
:
self head -
,
Nene
self -
done
-tail
else
.
self setf
-head-next
a
-head
sell
head Nene
-prev
-
,
te f felete (
self):
-last
it not self
.tail:
Listempry

"
"

return

if self head 2
self tail

,
a
.
:
self head,
a None

self itail a
done
else
:
self otail a self tail prev .
self next = Nene
.-tail
fef remove hodes ( , 41 42):
.
-between:
belf
it not X or not X
2.
,1
retuern O
.
new boublykinked Last C
-list
=
)
if
X
1prev.
Xeoprew next X
2-next
-

else
.
self hedd a
X
2-next
.
if X next
2
:
X prev -
next

Xaprev
.
2
elsel
sell -
Xo
prev

.tail
X1
prev a wone

X next -
None
2-
new heat = X

.
-List,
new tail =
42
-List
return newlist

tef splee ( L
-fist-alter
x

7:
self.
,
of not X or not La :
returr 0

Ir not La
-heads
return 0

Lashead =
x
-prev
if
x
-next.
next La tarl
x
prew
=
.
.
tartail next = xnett
,
else
:
Д
self tail = L Laliead anene
2-tail
-
x next
head h Nene
.
-
2tail
All necessary tests are
provided in

attached files below (2 B PY

.CID.
)
a r t X

,
Overall time
competing =
ocis

You might also like