Double Linked List Variations of Linked List 2
Double Linked List Variations of Linked List
2 Double Linked List Linked list with 2 pointers Connect previous and next element Last First / 12 35 78 / 2/27/2021
3 Structure Usually with 2 head pointer Last First / / Each element divided into 3 parts Prev Info Next 2/27/2021
4 ADT Element Double Linked List There are 2 pointer to point the next and previous element Type infotype : integer Type address : pointer to Elm. List Type Elm. List < info : infotype next : address prev : address > Prev Info Next 2/27/2021
5 ADT Double Linked List There are 2 head to point the first and the last element on the list Type List : < First : address Last : address > Dictionary L : List LAST FIRST L 2/27/2021
6 Create New List Last(X) is a keyword to refer the last element of the list X On the creation of new list, there is no element, thus first(L) and last(L) is Nil / Null Algorithm First(L) Nil Last(L) Nil / / First last L 2/27/2021
7 Creating New Element Prev(Y) is a keyword to refer the previous element of element pointed by Y On the creation of new element, set Next and Prev element = Nil Algorithm P Allocate(P) Next(P) Nil Prev(P) Nil / Info(P) 10 Prev 10 Info / next 2/27/2021
8 Exercise L P Last First / 12 43 68 80 89 / Task : Draw the Pointer Q prev(P)) R next(prev(last(L)))) S next(prev(next(P)))) 2/27/2021
9 Exercise L P Last First / 12 43 Task : How to … 68 80 output Access info of the last element 89 Access info of the second element of the list 43 Access info of the fourth element of the list 80 89 / Answer Copy info of 4 th element to 2 nd element Make P points 4 th element 2/27/2021
10 Exercise L P Last First / 12 43 68 Task : what is the output? 80 89 / Answer Info(Prev(next(prev(last(L)))) Info(Next(next(P))) Info(prev(Prev(next(P)))) Info(Next(Prev(Next(first(L))))) 2/27/2021
11 Exercise L P Last First / 12 43 68 Task : what is the output? 80 89 / Answer Info(next(P)) + info(prev(last(L))) Info(first(L)) – info(next(P))) Info(prev(P)) – info(next(first(L)))) Info(Next(Prev(Next(first(L))))) + info(P) 2/27/2021
12 Inserting new Element Insert first Insert last Insert after 2/27/2021
13 Insert First Algorithm next(P) first(L) prev(first(L)) P P first(L) P / L 6 / First Last / 12 68 89 / WHAT WILL HAPPEN IF THE LIST IS EMPTY? 2/27/2021
14 Insert First on empty list Algorithm next(P) first(L) P prev(first(L)) P / L 43 ERROR first(L) P / First Last / / // if list is empty first(L) P last(L) P 2/27/2021
15 Insert First Algorithm If (first(L) ≠ Nil and last(L) ≠ Nil ) then next(P) first(L) prev(first(L)) P first(L) P else first(L) P last(L) P 2/27/2021
16 Insert Last Algorithm prev(P) = Last(L) next(last(L)) = P P last(L) = P / L 99 / Last First / 12 68 89 / Again, careful when the list is empty 2/27/2021
17 Insert After Algorithm next(P) next(Prec) prev(P) Prec prev(next(prec)) P next(Prec) P P Prec L / 77 / Last First / 12 68 89 / 2/27/2021
18 Deleting the Element Delete first Delete last Delete after 2/27/2021
19 Delete First Algorithm P first(L) next(P) WHAT WILL HAPPEN IF THERE IS ONLY 1 ELEMENT INSIDE THE LIST ? next(P) Nil prev(first(L)) Nil P P L Last First / 12 / / 43 80 89 / 2/27/2021
20 Delete First Algorithm P first(L) next(P) Nil ERROR P L prev(first(L)) Nil //if only one element Last First / 12 first(L) Nil last(L) Nil / P 2/27/2021
21 Delete First Algorithm P first(L) If (first(L) ≠ last(L)) then first(L) next(P) Nil prev(first(L)) Nil else first(L) Nil last(L) Nil P 2/27/2021
22 Delete Last Algorithm P Last(L) prev(last(L)) prev(P) Nil next(last(L)) Nil P P L Last First / 12 43 / / 89 / 2/27/2021
23 Delete After Algorithm P next(Prec) Next(Prec) next(P) Prev(next(P)) Prec Prev(P) Nil Next(P) Nil P Prec L P Last First / 12 43 / 80 / 89 / 2/27/2021
Question?
Terimakasih
- Slides: 25