Title: Week 6a
14122202 Data Sturcture Algorithms?????????????
???????????????
- Week 6a
- List Abstract Data Type
- ??????????????
2????? (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 ????????????????????
??????????????????
3The 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
4Set ??? operations ?????????? List
- PrintList() ??????????????? List ???????
- makeEmpty() ????????????? List ???????
- Find() ?????????????? List ?????????????????????
? - ????????
- Insert() Insert ??????????????????????? List
- Delete() Delete ??????????????????????????????
List
5Set ??? 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
?????????????????????????????????????????
6???????? List ADT
- ?????????????? list ??? 2 ???? ??????
- ??????????????????? (Array Implementation of
List) - ????????????????????????? (Linked List
Implementation of List)
7Simple Array Implementation of Lists
- ????????? list ??????????????????????????????????
???????? ?????????????????????????????????? array
??? - ?????? list ???? Size ??????????????? array ????
gtSize
Array ???? N1 ???????????????????????? N1 ???
??????????????? 0 ??? N
- ?????????? C ????????????????? Array
????????????????????? - ??????????????????????????????????????????????????
???? - ???????????????????????????
8Simple 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 ??????????????????????????????
?
9Simple Array Implementation of Lists
Running Time ???????????????????????????????
- ??? Insert ?????????? list ?????????????
10Simple Array Implementation of Lists
- ??? Delete ?????? ? ???????????????? list
- ??? Insert ??? Delete ???????????????????????????
???????????????????????? ???? Linear Time, O(N)
11Simple Array Implementation of Lists
- ???????????????????? Insert ?????????????????????
????? N ????? ????????????????? O(N) ???????????
O(N2)
12(No Transcript)
13??????????????????????????? (Linked List)
- ??????????????????????????? (series) ??? records
???? nodes ???????????????????????????????????????
???????????????????????? ???????? Record ????
Node ?????????? 2 ???? ??? - Element ??? ????????????????? Record ???? Node
????????????Pointer ??? ??????????????? Record
???? Node ?????????????????????????? ????????
Next Pointer - ??? Pointer ?????????????????????????????????????
nil ????????????? ????????? 0 ???????? nil
14??????????????? P ???? Pointer ????? Record
?????? ???????????????? P ???????????????????
main memory ??? Record ??? P ?????
15Operation ?? Linked List
16Insert() ??????????????????????????????????? Node
???????????????????? new ????????????????????
Next Pointer 2 ???????????
(3)
17Delete() ??????????????????????????????
????????????????Next Pointer 1 ???????????
18Header
- ????? Header ???? Dummy Node ???????? node ?????
?????????????????????????
Header ????????????????????? 0 ?????? ???? List
??????????? 4 ??? A1, A2, A3, A4 ????? Header
19Empty List
- Linked List ????????????????????? ????????????
Empty List ??????????????? Dummy node ???? Header
???? ????? Linked List ?????????????????
20Type 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
21Programming Operations on Linked List
- ???????? List ??????????? Structure
????????????????????????
Struct Node ElementType element
Node next
List L ???? Pointer ????? List Position P ????
Pointer ????? Node Element X ?????????????????????
????? Node ?????
22Programming Operations on Linked List
???????????????????????????? Pointer
????????????????? -gt ????????????????????????????
? Pointer ?????
P
23???????????????????????????? Pointer
L
P
next
next
NULL
L-gtelement 25
L-gtnext P
30
P-gtelement
P-gtnext
NULL
30
L-gtnext -gtelement
24Test 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
25Programming Operations on Linked List
Test Empty List Test ??? Linked List ???? Empty
List ???????
Boolean isEmpty (node pointerToNode, node
pointerToList) return (pointerToList
NULL)
pointerToList-gtNext
NULL
26- 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
27- Test Last Position ??????? Test ??? node ??
Position P ???????????????????? List ???????
boolean IsLast(node P, node L) return
P-gtNext NULL
28- 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
29- 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
30- 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
31- 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
32- 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)
33Deletion of the List ????? List ???????
void deleteList( pointerToList) node p p
pointerToList-gtnext pointerToList-gtnext
NULL while(p!NULL) delete(p) pp-gtnext
34Deletion 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 ??????????
35???????????????? (Doubly Linked List)
- Linked list ??????????????????????????????????????
?????? (Traverse) ???? Linked list ????????????? - ??????????????????? (data member)
???????????????????? Node ????????? pointer
??????????? Node ????????
36?????????????????? (Circular Linked List)
- ????? Application ?????????? Link ??? node
??????????????????? node ?????? list - ?????????????????????????? Header ???????? Header