Title: ????? (Lists)
1????? (Lists)
- Abstract Data Types (ADTs)
- ??????? set ??? objects ?????? set ???
operations ???????????????? objects (???? lists,
sets ??? graphs) ????????? operations ??????????
objects ????????? ?????????? Abstract Data Type
(ADT) ????????? - Class ?? C ???????????????? ADT
??????????????????????? ??????????????????????????
??????????????????????????????? class
???????????? operation ?? ADT ????????????????????
??????????????????
2The List ADT
- List ?????????????????? A1, A2, A3,, AN ????
List ???? N - List ????? size 0 ??? Empty List
- Ai1 ??????? (Follows ???? succeeds) Ai (iltN)
- Ai-1 ?????? (Precedes) Ai (igt0)
- ?????? (Element) Ai ???????????????????? i ???
List
3Set ??? operations ?????????? List
- PrintList() ??????????????? List ???????
- makeEmpty() ????????????? List ???????
- Find() ?????????????? List ?????????????????????
? - ????????
- Insert() Insert ??????????????????????? List
- Delete() Delete ??????????????????????????????
List
4Set ??? operations ?????????? List
??? List ?????????????? 34, 12, 52, 16, 12 ??????
Find(52) ?????? return ???? 3 Insert(X,3) ?????
List ???????? 34, 12, 52, X, 16,
12 Remove(3) ????? List ???????? 34, 12, X, 16, 12
????????????????? Operations ?????????????????
???? Next() ??? Previous() ?????????????????????
???????? (Argument) Next() return
??????????????????????????????? Previous() return
?????????????????????????????????????????
5???????? List ADT
- ?????????????? list ??? 2 ???? ??????
- ??????????????????? (Array Implementation of
List) - ????????????????????????? (Linked List
Implementation of List)
6Simple Array Implementation of Lists
- ????????? list ??????????????????????????????????
???????? ?????????????????????????????????? array
??? - ?????? list ???? Size ??????????????? array ????
gtSize
Array ???? N1 ???????????????????????? N1 ???
??????????????? 0 ??? N
A0 A1 A2 . AN
- ?????????? C ????????????????? Array
????????????????????? - ??????????????????????????????????????????????????
???? - ???????????????????????????
7Simple Array Implementation of List
- ????????
- ???????????? list ???????????????????????????
(product) ????? 5 ???? ?????? computer, monitor,
hub, router ??? ATM - ??????????????? product ???? sizegt5 ??????
product 0
product 1
product 2
product 3
product 4
Computer
Monitor
Hub
Router
ATM
?????????????? list ??????????????????????????????
?
8Simple Array Implementation of Lists
Running Time ???????????????????????????????
- ??? Insert ?????????? list ?????????????
A0 A1 A2 A3 A4 A5
A0 A1 A2 A3 A4 A5
A0 A1 A2 A3 A4 A5
9Simple Array Implementation of Lists
- ??? Delete ?????? ? ???????????????? list
A0 A1 A2 X A3 A4 A5
A0 A1 A2 A3 A4 A5
A0 A1 A2 A3 A4 A5
- ??? Insert ??? Delete ???????????????????????????
???????????????????????? ???? Linear Time, O(N)
10Simple Array Implementation of Lists
- ???????????????????? Insert ?????????????????????
????? N ????? ????????????????? O(N) ???????????
O(N2)
A0
A1 A0
A2 A1 A0
11Operation Running Time Note
PrintList Linear Time O(N) ?? loop ????? print ??????? ??????? N ???
Find Linear Time O(N) ?? loop ??????? (find) ???????, worst case ??? N ???
FindKth() Constant, c ??????????? Ak ???????????????????
Insert() O(N) Worst Case ???????????(push) ????????????????????????????? ?????????????????????????????? insert ???????? ????????? insert ????????????????? Worst-case ???????????????? insert ???????????????? 0 ????????????????????????? N ?????????????????????
Delete() O(N) Worst Case ??????? delete ????????????? ?????????????????????????????????????????????????????????????????? Worst-case ???????????????? delete ???????????????? 0 ??????????????????????????? N-1 ???????????????????
12??????????????????????????? (Linked List)
- ??????????????????????????? (series) ??? records
???? nodes ???????????????????????????????????????
???????????????????????? ???????? Record ????
Node ?????????? 2 ???? ??? - Element ??? ????????????????? Record ???? Node
????????????Pointer ??? ??????????????? Record
???? Node ?????????????????????????? ????????
Next Pointer - ??? Pointer ?????????????????????????????????????
nil ????????????? ????????? 0 ???????? nil
13??????????????? P ???? Pointer ????? Record
?????? ???????????????? P ???????????????????
main memory ??? Record ??? P ?????
14Operation ?? Linked List
Operation Running Time Note
PrintList Find Linear Time O(N) ????????????? (Traverse) ??? Pointer ????????????????
FindKth() O(i) ?????????? Traverse ??? Pointer ?????????????????? i
15Insert() ??????????????????????????????????? Node
???????????????????? new ????????????????????
Next Pointer 2 ???????????
(3)
16Delete() ??????????????????????????????
????????????????Next Pointer 1 ???????????
17Header
- ????? Header ???? Dummy Node ???????? node ?????
?????????????????????????
Header ????????????????????? 0 ?????? ???? List
??????????? 4 ??? A1, A2, A3, A4 ????? Header
18Empty List
- Linked List ????????????????????? ????????????
Empty List ??????????????? Dummy node ???? Header
???? ????? Linked List ?????????????????
19Type Declaration of Linked Lists
/Declare Function Prototype/ PointerToNode
makeEmpty(ptrToNode) Boolean isEmpty(ptrToNode)
Position find(TypeOfElement, ptrToNode) Position
findPrevious(TypeOfElement, ptrToNode) void
insert(TypeOfElement, ptrToNode, ptrToNode) void
delete(TypeOfElement, ptrToNode) void
deleteList(ptrToNode) Struct Node
TypeOfElement Element PointerToNode
Next
?????? Node ?????????????? Structure
????????????? 2 ????? ??? Element ?????????????
Next
20Programming Operations on Linked List
- ???????? List ??????????? Structure
????????????????????????
Struct Node ElementType element
Node next
List L ???? Pointer ????? List Position P ????
Pointer ????? Node Element X ?????????????????????
????? Node ?????
21Programming Operations on Linked List
???????????????????????????? Pointer
????????????????? -gt ????????????????????????????
? Pointer ?????
P
22???????????????????????????? Pointer
L
P
next
next
NULL
L-gtelement 25
L-gtnext P
30
P-gtelement
P-gtnext
NULL
30
L-gtnext -gtelement
23Test Empty List Test ??? Linked List ???? Empty
List ???????
Algorithm Boolean isEmpty(pointerToNode,
pointerToList) pointerToNode pointer to any
node pointerToList pointer to the list to be
tested Output True if the list is empty False
if the list is not empty Begin (return
PointerToList-gtnext equals NULL) End
24Programming Operations on Linked List
Test Empty List Test ??? Linked List ???? Empty
List ???????
Boolean isEmpty (node pointerToNode, node
pointerToList) return (pointerToList
NULL)
pointerToList-gtNext
NULL
25- Test Last Position ??????? Test ??? node ??
Position P ???????????????????? List ???????
Algorithm Boolean isLast(pointerToNode,
pointerToList) pointerToNode pointer to any
node pointerToList pointer to the list to be
tested Output True if pointerToNode points to
last position in the list False if
pointerToNode not point to the last position in
the list Begin (return next of
pointerToNode equals NULL) End
26- Test Last Position ??????? Test ??? node ??
Position P ???????????????????? List ???????
boolean IsLast(node P, node L) return
P-gtNext NULL
27- Find ??????????????????????? Element X ?????
List
Algorithm pointerToNode find(target,
pointerToList) target value to
find pointerToList pointer to the
list Output pointerToNode pointer to the node
found Begin (set pointerToNode to start at
pointerToList) While(pointerToNode
is not NULL and element of pointerToNode is
not target) (Move pointerToNode to the
next node) (return
pointerToNode) End
28- Find Position ????????? (Position) ?????????
Element X ????? List
Position Find( int X, node L ) node P
P L-gtNext while( P!NULL
P-gtElement ! X ) P P-gtNext return P
29- Find Previous Position ???????????? node
????????????????? Element X ?? List
??????????????????????? Null ????????????? Header
node findPrevious(int target, node
pointerToList) node pointerToNode pointerToN
ode pointerToList while((pointerToNode-gtnext
! NULL) (pointerToNode-gtnext-gtelement !
target)) pointerToNode
pointerToNode-gtnext return pointerToNode
30- Insertion Routine ???? node ?????? Element X
????????????????? P ????????????????? Header ????
void insert(int newElement, node pointerToList,
node pointerToNode) node tempCell tempCell
new node if(tempCell NULL) coutgtgtError
out of memory space!! else tempCell-gteleme
nt newElement tempCell-gtnext
pointerToNode-gtnext pointerToNode-gtnext
tempCell
X
31- Deletion ?? node ????? List ??????????????????
Element X ??????????????????? Header ????
void delete(int target, node pointerToList) no
de pointerToNode pointerToNode
findPrevious(target, pointerToList) tempCell
pointerToNode-gtnext pointerToNode-gtnext
tempCell-gtnext delete(tempCell)
32Deletion of the List ????? List ???????
void deleteList( pointerToList) node p p
pointerToList-gtnext pointerToList-gtnext
NULL while(p!NULL) delete(p) pp-gtnext
33Deletion of the List
void DeleteList( List L ) Position P,
Tmp P L-gtNext L-gtNext
NULL while( P ! NULL ) Tmp
P-gtNext free(P) P Tmp
- ????????? Delete ??????????
34???????????????? (Doubly Linked List)
- Linked list ??????????????????????????????????????
?????? (Traverse) ???? Linked list ????????????? - ??????????????????? (data member)
???????????????????? Node ????????? pointer
??????????? Node ????????
35?????????????????? (Circular Linked List)
- ????? Application ?????????? Link ??? node
??????????????????? node ?????? list - ?????????????????????????? Header ???????? Header