L i n k e d l i s t s
L i n k e d l i s t s L i n k e d l i s t s a r e a d a t a s t r u c t u r e d e s i g n e d f o r t o a l i s t s e q u e n t i a l a c c e s s ● Mo v e f o r w a r d s a n d b a c k w a r d s t h r o u g h t h e l i s t , o n e e l e m e n t a t a t i m e ● R e a d o r w r i t e t h e e l e m e n t a t t h e c u r r e n t p o s i t i o n ● I n s e r t o r d e l e t e e l e m e n t s a t t h e c u r r e n t p o s i t i o n ● a l l i n O ( 1 ) t i m e Tie d o w n s i d e : g e t t i n g t o a s p e c i fj c p o s i t i o n i n t h e l i s t t a k e s O ( n ) t i m e ● L i n k e d l i s t s a r e b a d f o r r a n d o m a c c e s s
S i n g l y - l i n k e d l i s t s A s i n g l y - l i n k e d l i s t i s m a d e u p o f n o d e s , w h e r e e a c h n o d e c o n t a i n s : ● s o m e d a t a ( t h e n o d e ' s v a l u e ) ● a l i n k ( r e f e r e n c e ) t o t h e n e x t n o d e i n t h e l i s t class Node<E> { E data; Node<E> next; } Tie l i s t i t s e l f i s a r e f e r e n c e t o t h e fj r s t n o d e : class List<E> { Node<E> head; }
S i n g l y - l i n k e d l i s t s Tie l i s t [ T o m , D i c k , H a r r y ] a s a l i n k e d l i s t : tom.next == dick T o m D i c k H a r r y tom.data == list.head == “Tom” tom
M o d i f y i n g a l i n k e d l i s t // Insert item at front of list void addFirst(E item) // Insert item after another item void addAfter(Node<E> node, E item) // Remove first item void removeFirst() // Remove item after another item void removeAfter(Node<E> node)
C a l l i n g l i s t . a d d F i r s t ( “ A n n ” ) T o m D i c k H a r r y newNode A n n F i r s t c r e a t e a n e w l i s t n o d e
C a l l i n g l i s t . a d d F i r s t ( “ A n n ” ) T o m D i c k H a r r y A n n newNode.next = list.head Tie n s e t
C a l l i n g l i s t . a d d F i r s t ( “ A n n ” ) T o m D i c k H a r r y A n n list.head = newNode Tie n s e t D o n e ! addAfter i s v e r y s i m i l a r
C a l l i n g l i s t . d e l e t e A f t e r ( t o m ) A A n n n n T T o o m m D D i i c c k k H H a a r r r r y y tom tom.next tom.next.next tom.next T o r e m o v e f r o m t h e l i s t , tom.next = tom.next.next s e t
C a l l i n g l i s t . d e l e t e A f t e r ( t o m ) A n n T o m D i c k H a r r y tom tom.next tom.next.next D o n e ! deleteFirst i s v e r y s i m i l a r
H e a d e r n o d e s I t ' s n o t g o o d t o h a v e t w o v e r s i o n s o f e a c h l i s t addFirst addAfter ) o p e r a t i o n ( e . g . v s : ● Tie A P I g e t s t w i c e a s b i g ● C o d e u s i n g t h e l i s t l i b r a r y w i l l n e e d s p e c i a l c a s e s w h e n i t m o d i fj e s t h e f r o n t o f t h e l i s t ● T w i c e a s m u c h c o d e t o w r i t e I d e a : a d d a h e a d e r n o d e , a f a k e n o d e t h a t s i t s a t t h e f r o n t o f t h e l i s t b u t d o e s n ' t c o n t a i n a n y d a t a addFirst(x) W e c a n g e t r i d o f a n d d o addAfter(headerNode, x) i n s t e a d
L i s t w i t h h e a d e r n o d e I f W w e e w c o a u n l t d t e o v a e d n d “ A n n ” b e f o r e “ T o m ” , w e addAfter(head, “Ann”) g e t r i d o f t h i s c a n d o l i s t o b j e c t n o w A n n T T o o m m D D i i c c k k H H a a r r r r y y Tie h e a d e r n o d e !
D o u b l y - l i n k e d l i s t s I n a s i n g l y - l i n k e d l i s t y o u c a n o n l y g o f o r w a r d s t h r o u g h t h e l i s t : ● I f y o u ' r e a t a n o d e , a n d w a n t t o fj n d t h e p r e v i o u s n o d e , t o o b a d ! O n l y w a y i s t o s e a r c h f o r w a r d f r o m t h e b e g i n n i n g o f t h e l i s t ● Tii s a l s o m e a n s w e c a n ' t d e l e t e t h e c u r r e n t n o d e ( w o u l d n e e d t o next u p d a t e i t s p r e d e c e s s o r ' s fj e l d ) I n a d o u b l y - l i n k e d l i s t , e a c h n o d e h a s a l i n k t o t h e n e x t n o d e s a n d t h e p r e v i o u s Y o u c a n i n O ( 1 ) t i m e : ● g o f o r w a r d s a n d b a c k w a r d s t h r o u g h t h e l i s t ● i n s e r t a n o d e b e f o r e o r a f t e r t h e c u r r e n t o n e ● m o d i f y o r d e l e t e t h e c u r r e n t n o d e Tie “ c l a s s i c ” d a t a s t r u c t u r e f o r s e q u e n t i a l a c c e s s
Tie l i s t i t s e l f A d o u b l y - l i n k e d l i s t l i n k s t o t h e fj r s t a n d l a s t n o d e s list.first = ann list.last = harry A n A n n n T o m T o m D i c D k i c k H H a r a r r y r y tom.next = dick tom.prev = ann
I n s e r t i o n a n d d e l e t i o n i n d o u b l y - l i n k e d l i s t s S i m i l a r t o s i n g l y - l i n k e d l i s t s , b u t y o u prev h a v e t o u p d a t e t h e p o i n t e r t o o . T o d e l e t e T o m i n t h e l i s t b e l o w : dick.prev = ann; ann.next = dick; A n A n n n T o m T o m D i c D k i c k H a H r a r r y r y I n g e n e r a l w e c a n d o : node.next.prev = node.prev; node.prev.next = node.next;
I n s e r t i o n a n d d e l e t i o n i n d o u b l y - l i n k e d l i s t s , c o n t i n u e d T o d e l e t e t h e c u r r e n t n o d e t h e i d e a i s : node.next.prev = node.prev; node.prev.next = node.next; B u t t h e r e a r e l o t s o f s p e c i a l c a s e s ! ● Wh a t i f t h e n o d e i s t h e fj r s t n o d e ? node.prev == null Tii s c o d e c r a s h e s , s i n c e list.first W e a l s o n e e d t o u p d a t e ● Wh a t i f t h e n o d e i s t h e l a s t n o d e ? ● Wh a t i f t h e l i s t o n l y h a s o n e e l e m e n t s o t h e n o d e i s b o t h t h e fj r s t t h e l a s t n o d e ? a n d S o l u t i o n : c i r c u l a r l i n k e d l i s t !
Tie l i s t o b j e c t Tie h e a d e r i s j u s t a n o r m a l l i s t n o d e head.next = ann head.prev = harry A n A n n n T o m T o m D i c D k i c k H a H r a r r y r y ann.prev = head harry.next = head
C i r c u l a r l y - l i n k e d l i s t w i t h h e a d e r n o d e A n e x t r a h e a d e r n o d e , “ i n b e t w e e n ” t h e fj r s t a n d l a s t e l e m e n t s i n t h e l i s t W o r k s o u t q u i t e n i c e l y ! ● head.next i s t h e fj r s t e l e m e n t i n t h e l i s t ● head.prev i s t h e l a s t e l e m e n t ● y head o u n e v e r n e e d t o u p d a t e ● n next prev null o n o d e ' s o r i s e v e r N o s p e c i a l c a s e s i n i n s e r t i o n o r d e l e t i o n !
S t a c k s a n d l i s t s u s i n g l i n k e d l i s t s Y o u c a n i m p l e m e n t a s t a c k u s i n g a l i n k e d l i s t : ● p u s h : a d d t o f r o n t o f l i s t ● p o p : r e m o v e f r o m f r o n t o f l i s t Y o u c a n a l s o i m p l e m e n t a q u e u e : ● e n q u e u e : a d d t o r e a r o f l i s t ● d e q u e u e : r e m o v e f r o m f r o n t o f l i s t
Recommend
More recommend