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

Double Linked List

This document describes a double linked list data structure. It includes Node class definition with next and prev pointers and data. It describes head and tail pointers. It provides methods for adding/removing elements from the front/back of the list, getting elements by index, and iterating through the list forward and backward using iterators.

Uploaded by

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

Double Linked List

This document describes a double linked list data structure. It includes Node class definition with next and prev pointers and data. It describes head and tail pointers. It provides methods for adding/removing elements from the front/back of the list, getting elements by index, and iterating through the list forward and backward using iterators.

Uploaded by

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

Double linked list

Node

I I
data next
prev

head temp tail


7 ->
30 E
-

18 28
- & =
I

head, data - 18

head. per data null


-

temp. next. data ->30


temp.prev, data -> 10
tail, next a
data-hull

Insert

temp next
head temp

18 30
-
I
④ & Ge
N
Node() new Node new
=
Node (100);neuNoce
new Node -

$
next=ptr.next;
new Node;
temp. next.prer=
temp. next new
=

Node;
new Node, pre-temp;

Remove
head pr.prev. Al ptronexttail

E lio1 = (21 * 1301

agre
-

&
ptr.prev.next=ptr.next;
& ptr.next.prev ptr.prew;
=


class kW linked list

class
class

method.
Liter
Add Firstmethod

public void add First (E item) E


tail
head

=new Noce
-
want to become the head.

new Node, next-head;


head, prev new
=
Node in
head=
new Node;

Add hast method

public void add last (fitem) E

tail
head
* * I-⑪
A
Node; ②-
tail, next new
=

new No de
new node per tail; -
tail new No de;
-

Want to
be

tail
the
ged First -> return first element(head. data)

hast return last element [tail, data)


get -

I date

asanawe,
Prev

next item
NoceE>

an
as
e
↳ int index;

if next element

-
return true

exists.

1/ return iterator in list

elevat
-> returns next
forward.
and move iterator
public Iterator (2) Iterator()
↳ remove lastelement E
returned
by next method.
KWlistler iterator new
=
KWlist
Her

(O);
return iterater,
3

1/ return histiferator
in list

public histiterator (2) Gistiterator


E C)

[Wlistler iterator new


=
KWlist
Her

(O);
return iterater,
3
methods in kulist iter
return true if
has
I a next
item.
public boolean has Next()
E
return nextltem!Inull
3
element
returns next
& and more iterater
public Enext()
forward
E
if)! has Next())
throw new No Such Element Exception ();

last Item Returned next Item;


=

next Hem=nextltem.next;
index++;
return last tem Returned data;3

public void remove (3 X reement returned


by next
E if (last tem Returned ==
null) method.
these State ();
new
IIgeal Exception
1/ remove head
else if (lastHem Returned head)
==

E
head: head, next;
If (head==
null)
tail=
null;
else

head. prev-null;
Size --;

3
return true
if
there
X is

public boolean has previous () prevt


E
return (nextltem==nill 8 size! 0) I1
=

(nextlem, prer!=
null)
3
return
- previou
public boolean E previous() ·tem
and moves

& If C:has previous (1) iterator b ackward


throw exception ();

else If (nextltem== null)


nextlem=
tails;
else
Hem=
nexttem
next pler;
last Item Returned Hem;
next
=

index--;
return lastHem Returned data; 3

index()
next -> return index of item
next

previousindex()
-> return index of previousitem
Set (2 item) ->
replace returned
lastier by

new item.

You might also like