Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Linked Lists

Pointers
Singly Singly Linked Lists
Dynamically Linked Stacks and Queues
Polynomials
Additional List Operations
Equivalence Relations
Sparse Matrices
Doubly Doubly Linked Lists
Generalized Lists
Pointers
Sequential Sequential representation
! Disadvanta"es
#nsertion $ Deletion
%aryin" size
Linked Linked representation
! Sin"ly&Dou'ly linked lists
Singly Linked Lists
Linked list
! An ordered sequence o( nodes )it* links
! +*e nodes do not reside in sequential locations
! +*e locations o( t*e nodes may c*an"e on di((erent runs
Linked list may be represented in mmemory: Data[ ], Link[ ]
,
'at
cat
sat
vat null
Singly Linked Lists(Cont.)
#nsertion
Singly Linked Lists (Cont.)
Deletion
Create and Use Linked Lists in C++
.ode Class
class List-ode.
friend class List/
private:
0
sat
cat
bat
vat
1
2
,
0
,
0
3
2
4
5
'at cat sat vat null
'at cat sat vat null
mat
#nsert mat a(ter cat
(irst
'at cat sat vat null mat
Delete mat (rom list
(irst
int data/ &&char data637
ListNode 8Link/
9/
.List Class
class List .
public:
&&List manipulation operations
void create:;/
void insert:;/
void delete:;/
private:
ListNode 8(irst/
9/
.Creating a t!o"node list
&&Pro"ram 2<3= e>ample 2<0= pa"e ,?0<
.Listode #onstr$#tor
&&Pro"ram 2<3= e>ample 2<0= pa"e ,?0<
.%nserting a node
&&Pro"ram 2<2= (i"ure 2<,,= e>ample 2<3= pa"e ,?3<
.Deleting a node
&&Pro"ram 2<4= e>ample 2<2= pa"e ,?2<
%mplementing Linked Lists !it& 'emplates
.%nteger List
List@intA intlist/
.(loat List
List@(loatA (loatlist/
3
.C&ara#ter List
List@c*arA c*arlist/
.'emplate de)inition o) linked lists
&&Pro"ram 2<5= pa"e ,?5<
Cir#$lar Lists
Bircular listC t*e link (ield o( t*e last node points to t*e (irst
&&Di"ure 2<,3= pa"e ,E2<
. The empty circular lists
&&Di"ure 2<,5= pa"e ,E4<
Linked Sta#ks and *$e$es
<F*en )e need multiple stacks and queues= linked stacks and linked
queues are "ood solution<
+ ,epresentation o) m Sta#ks
.+ #olle#tion o) m sta#ks
Stack 8stack G new Stack6m7/
.Sta#k #lass de)inition and adding a node and deleting top node
&&pro"ram 2<,4= pro"ram 2<,5= pro"ram 2<,?= pa"e ,EE<
2
top top
data link
(a)Linked Stack
null
)ront )ront
(b)Linked Queue
rear rear
data link
null
Polynomials
Polynomial representation
.Polynomial #lass de)inition and de#laration o) operator+( )
&&pro"ram 2<01= pa"e ,H,
. %mplementation o) operator+( )
&&pro"ram 2<0,= pa"e ,H3
E>ample
! aG 3>
,2
I 0>
E
I ,
! 'G E>
,2
J 3>
,1
I ,1>
5
--)ig$re ../0: Polynomial representation, page /1/.
--)ig$re ../1: Polynomial adding, C2 a + b, page /13.
+dditional List 4perations
,e5ersing a #&ain
4
coef expon link
6 Con#atenates t!o #&ain
Cir#$lar Lists ,epresentation o) Polynomials
Representin" polynomials
! -onezero polynomialsC 3>
,2
I 0>
E
I ,
Sparse 7atri#es
.ead field: to distin"uis* 'et)een *ead node and element node:(alse;<
.T*e Kead node (or ro! i is also t*e *ead node (or #ol$mn i<
!a" head node !b" typical node
.#$ample:
5
3 14 2 8 1 0
do)n *ead
do)n ri"*t col ro) *ead
value
ri"*t
ne>t
first
0 1 2 3
0 0 11 0
12 0 0 0
0 !4 0 0
0 0 0 !1"
Do$bly Linked Lists
.%f we have a problem in which moving in either direction is often
necessary& then it is useful to have doubly linked lists.
?
3 3
!1"
4 4

0
1
2
3
1 0

12
2 1

!4
0 2

11
#ote$ %&e ta' field of a node is not s&o(n)
ead node rlink llink
Doubly linked circular list with head node
%nsertion into a Do$bly Linked Cir#$lar List
.'rogram (.)*& page +,-.
Deletion )rom a Do$bly Linked Cir#$lar List
.'rogram (.)(& figure (.),& page +,-.
8enerali9ed Lists
Generalized List
! A "eneralized list= += is a (inite sequence o( nAG 1 elements= a
:
,;,a
n"
/
= )*ere a
i
is eit*er an atom or a list<
E>amples
! DG:;
! AG :a= :'= c;;
! LG :A= A= :;;
! BG :a= B;
Bonsequences
! lists may 'e s*ared 'y ot*er lists :E>ample L;
! lists may 'e recursive:E>ample B;
8enerali9ed Lists(Cont.)
Represent polynomial
! p:>=y=z;G >
,1
y
3
z
0
I0>
E
y
3
z
0
I3>
E
y
0
z
0
I>
2
y
2
zI5>
3
y
2
zI0yz
Solution #
J
Solution ##
! Msin" "eneral list
! 3>
0
yC See Di"ure 2<30= p033<
E
*nsert
+oef expx exp,
exp- link
S$mmary
Lists
! Sin"ly&Dou'ly linked list
! Bircular
Sin"ly&Dou'ly linked list
! Linked lists )it* *ead
! Generalized linked lists
Operations
! #nsertion= Deletion= Modi(ication= Retrieval= Boncatenation= etc<
H

You might also like