1 / 17

List ADTs

List ADTs. By Pantharee S. Abstract Data Types (ADTs). เป็นชนิดข้อมูลที่สร้างขึ้นโดยผู้เขียนโปรแกรม (u ser defined data type) สร้างโดยใช้ structure ที่มีในภาษา imperative เพื่อให้ชนิดข้อมูลนี้ใช้งานได้อย่างปลอดภัย ควรมีลักษณะ

nascha
Download Presentation

List ADTs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. List ADTs By Pantharee S.

  2. Abstract Data Types (ADTs) • เป็นชนิดข้อมูลที่สร้างขึ้นโดยผู้เขียนโปรแกรม (user defined data type) • สร้างโดยใช้ structure ที่มีในภาษา imperative • เพื่อให้ชนิดข้อมูลนี้ใช้งานได้อย่างปลอดภัย ควรมีลักษณะ • รายละเอียดของการเก็บข้อมูลต้องไม่มีความสำคัญต่อการใช้งานชนิดข้อมูล • ข้อมูลภายในจะไม่ถูกอ้างถึงได้โดยตรง แต่ผ่านทาง operations ที่กำหนดไว้ • Build-in type ก็คือ ADTs นั่นเอง

  3. Abstract Data Types (ADTs) • ภาษาที่สร้าง ADT ได้ต้องมีกลไก 2 อย่าง คือ • Encapsulation เพื่อนำข้อมูลกับ operations มาผูกติดกันเป็นหน่วยหนึ่ง • Information hidingเพื่อกำหนดขอบเขตในการอ้างถึงข้อมูลหรือ operations ของชนิดข้อมูลหนึ่ง ๆ • ข้อดี • ง่ายต่อการจัดการโครงสร้างของโปรแกรม • แก้ไขเปลี่ยนแปลงได้สะดวก • คอมไพล์แยกส่วนได้ • สามารถเปลี่ยนการแทนข้อมูลได้ โดยไม่มีผลกระทบต่อผู้ใช้ • Reliability โดยการซ่อนการแทนข้อมูล ผู้ใช้ไม่สามารถเข้าถึงข้อมูลในวัตถุได้โดยตรง

  4. 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

  5. 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

  6. 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

  7. Linked Lists Improve the linear cost of insertion and deletion The list is not stored contiguously A5 A4 A1 A2 A3 A5 A4 A1 A2 A3 O 712 992 692 800 1000 800 712 692 992

  8. Deletion from a Linked List Delete command can be executed in one pointer change A5 A4 A1 A2 A3

  9. 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. A5 A4 A1 A2 A3 3 2 X 1

  10. Doubly Linked Lists It is convenient to traverse lists backward. Memory overhead √ × A5 A4 A1 A2 A3

  11. Circularly Linked Lists To have the last cell keep a pointer back to the first. A5 A4 A1 A2 A3

  12. Index ที่เป็นไปได้ของ add(index,e) คือ ? ตั้งแต่ 0 จนถึง size +1 ตั้งแต่ 0 จนถึง size ตั้งแต่ 0 จนถึง size – 1 เป็นจำนวนไม่ติดลบ

  13. Index ที่ส่งให้ remove มีค่าเท่าใดได้บ้าง ? ตั้งแต่ 0 จนถึง size ตั้งแต่ 0 จนถึง size – 1 ตั้งแต่ 0 จนถึง size +1 เป็นจำนวนไม่ติดลบ

  14. indexOf(e) ไว้ทำอะไร ? ค้นว่า e มีในรายการหรือไม่ ค้นว่าที่ตำแหน่ง e เก็บอะไร ค้นว่า e เก็บที่ใดในรายการ ย้าย e ไปไว้ท้ายรายการ

  15. ถ้ารายการเก็บ e ไว้หลายตัว indexOf(e) คืนอะไร ? คืนตำแหน่งทางซ้ายสุดที่พบ e คืนตำแหน่งทางขวาสุดที่พบ e คืนตำแหน่งใดก็ได้ที่พบ e คืนทุกตำแหน่งที่พบ e

  16. Singly Linked List : Method ใดใช้เวลา O(n) size() isEmpty() add(e) add(i,e) remove(e) set(i,e) get(i)

  17. Circulary Linked Listจะเขียน Code เพื่อทำ Method ทางซ้ายอย่างไร ?

More Related