Array List and Linked List
Download
Report
Transcript Array List and Linked List
List ADTs
BY PANTHAREE S.
Abstract Data Types (ADTs)
เป็ นชนิ ดข้อมูลที่สร้างขึ้นโดยผูเ้ ขียนโปรแกรม (user defined data
type)
สร้างโดยใช้ structure ที่มีในภาษา imperative
เพื่อให้ชนิ ดข้อมูลนี้ ใช้งานได้อย่างปลอดภัย ควรมีลกั ษณะ
รายละเอียดของการเก็บข้อมูลต้องไม่มีความสาคัญต่อการใช้งานชนิ ดข้อมูล
ข้อมูลภายในจะไม่ถก
ู อ้างถึงได้โดยตรง แต่ผา่ นทาง operations ที่กาหนดไว้
Build-in type ก็คือ ADTs นัน
่ เอง
Abstract Data Types (ADTs)
ภาษาที่สร้าง ADT ได้ตอ้ งมีกลไก 2 อย่าง คือ
Encapsulation เพื่อนาข้อมูลกับ operations มาผูกติดกันเป็ นหน่ วยหนึ่ ง
Information hiding เพื่อกาหนดขอบเขตในการอ้างถึงข้อมูลหรื อ operations ของชนิ ด
ข้อมูลหนึ่ง ๆ
ข้อดี
ง่ายต่อการจัดการโครงสร้างของโปรแกรม
แก้ไขเปลี่ยนแปลงได้สะดวก
คอมไพล์แยกส่ วนได้
สามารถเปลี่ยนการแทนข้อมูลได้ โดยไม่มีผลกระทบต่อผูใ้ ช้
Reliability โดยการซ่ อนการแทนข้อมูล ผูใ้ ช้ไม่สามารถเข้าถึงข้อมูลในวัตถุได้โดยตรง
The List ADT
General List of form A1 , A2 , A3 , . . . , AN (Size = N)
Null List the special list of Size=0
Opreation on collection and List
PrintList
MakeEmpty
Find : return the position of the first occurrence of a key
Insert : insert a key into a list
Delete : delete a key in a list
FindKth : return the element in some position
Example
If the list is
34 , 12 , 52 , 16 , 12
Find(52) return 3
Insert(X,3) insert after 3 34 , 12 , 52 , X , 16 , 12
insert before 3 34 , 12 , X , 52 , 16 , 12
Delete(3) 34 , 12 , 52 , X , 16 , 12
34 , 12 , X , 16 , 12
Next
Previous
Simple Array Implementation of Lists
Using for loop to implement PrintList
Return array element address to implement FindKth
Insert and Delete are expensive
Insert shift all element after the position down
Delete shift all element after the position up
Running time of insertion and deletion is slow and the list size
must be know in advance
Simple arrays are generally used to implement lists
Linked Lists
Improve the linear cost of insertion and deletion
The list is not stored contiguously
A1
A1
A2
800
1000
A2
800
712
A3
712
A5
A4
A3
992
A4
692
992
A5
692
O
Deletion from a Linked List
Delete command can be executed in one pointer
change
A1
A2
A3
A4
A5
Insertion into a linked list
Insert command require obtaining a new cell from
the system by using a new cell and the executing two
pointer maneuvers.
A1
A2
A3
3
X
1
2
A4
A5
Doubly Linked Lists
It is convenient to traverse lists backward.
Memory overhead
A1
A2
×
A3
A4
A5
√
Circularly Linked Lists
To have the last cell keep a pointer back to the first.
A1
A2
A3
A4
A5
Index ที่เป็ นไปได้ ของ add(index,e) คือ ?
ตั้งแต่ 0 จนถึง size +1
ตั้งแต่ 0 จนถึง size
ตั้งแต่ 0 จนถึง size – 1
เป็ นจานวนไม่ติดลบ
Index ที่สง่ ให้ remove มีคา่ เท่าใดได้ บ้าง ?
ตั้งแต่ 0 จนถึง size
ตั้งแต่ 0 จนถึง size – 1
ตั้งแต่ 0 จนถึง size +1
เป็ นจานวนไม่ติดลบ
indexOf(e) ไว้ ทาอะไร ?
ค้นว่า e มีในรายการหรื อไม่
ค้นว่าที่ตาแหน่ง e เก็บอะไร
ค้นว่า e เก็บที่ใดในรายการ
ย้าย e ไปไว้ทา้ ยรายการ
ถ้ ารายการเก็บ e ไว้ หลายตัว indexOf(e) คืนอะไร ?
คืนตาแหน่งทางซ้ายสุ ดที่พบ e
คืนตาแหน่งทางขวาสุ ดที่พบ e
คืนตาแหน่งใดก็ได้ที่พบ e
คืนทุกตาแหน่งที่พบ e
Singly Linked List : Method ใดใช้ เวลา O(n)
size()
isEmpty()
add(e)
add(i,e)
remove(e)
set(i,e)
get(i)
Circulary Linked List
จะเขียน Code เพื่อทา Method ทางซ้ ายอย่างไร ?
…..removeFirst()
A. header.next == header
…..removeLast()
B. removeNode(header.prev)
…..addFirst()
C. removeNode(header.next)
…..addLast()
D. removeNode(header)
…..isEmpty()
E. addBefore(header.next)
F. addBefore(header.prev)
G. addBefore(header)