งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

21 August 2014 204351 ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "21 August 2014 204351 ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list."— ใบสำเนางานนำเสนอ:

1 21 August 2014 204351 ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list

2 21 August 2014 204351 Data Structures 2 Unsorted list ADT Specification –ItemType : type ของ สมาชิกใน list –index: ตำแหน่งของสมาชิกใน list Operations –createList () –destroyList () –isEmpty () : boolean –getlength () : integer –insert (in index:integer, in newItem:ItemType) –remove (in index:integer) –retrieve (in index:integer, out dataItem: ItemType) Implementation –Array –pointer

3 21 August 2014 204351 Data Structures 3 Array-based implementation Data structure const int maxList = 100; -- public typedef int itemType; -- public itemType items [maxList]; -private int size; -- private Operations –Public – ชื่อ operations –Private – ชุดคำสั่งของแต่ละ operation

4 21 August 2014 204351 Data Structures 4 Array-based implementation Constructor Size = 0 insert(int index, itemType newItem) if (index>=1 and index<=size+1 and size<=maxList) { for (pos=size-1; pos>=index-1; --pos) items[pos+1] = items[pos]; items[index-1] = newItem; ++size; } getLength() return size;

5 21 August 2014 204351 Data Structures 5 Array-based implementation remove (int index) retrieve(int index, itemType& dataItem)

6 21 August 2014 204351 Data Structures 6 Pointer-based implementation Linked Lists Declare node ใน linked list -- private struct Node { itemType item Node *next; }; //end struct int size; Node *head; Allocate node แบบ dynamic Node *p; p = new Node; Node

7 21 August 2014 204351 Data Structures 7 Pointer-based implementation Insert ที่ head Insert ที่ตำแหน่งใดๆ Insert ที่ตำแหน่งท้ายสุด

8 21 August 2014 204351 Data Structures 8 Pointer-based implementation insert(int index, itemType newItem) newLength = getLength() + 1; if (index>=1 and index<=newLength) { newPtr = new Node; newPtr.item = newItem; size = newLength; if (index==1) { newPtr.next = head; head = newPtr; else { Node *prev = find(index-1); newPtr.next = prev.next; prev.next = newPtr; }

9 21 August 2014 204351 Data Structures 9 Pointer-based implementation Find (int index) // find a node at position index if (index getLength()) return null; else { cur = head; for (i=1; i<index; ++i) cur = cur.next; return cur

10 21 August 2014 204351 Data Structures 10 Delete Node ออกจาก Linked List Delete node แรก head=head->next; Delete node cur ที่อยู่ภายใน list prev->next=cur->next; Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

11 21 August 2014 204351 Data Structures 11 Pointer-based implementation remove (int index)

12 21 August 2014 204351 Data Structures 12 Display contents ใน Linked List Traverse operation cur = head; while (cur != NULL) { cur = cur.next; print (cur.item); }

13 21 August 2014 204351 Data Structures 13 Comparing Array-Based and Pointer-Based Implementations Size –Array-based : size fixed และต้องกำหนดล่วงหน้า Storage requirements –Array-based ใช้ memory น้อยกว่า pointer-based Access time –Array-based: constant access time –Pointer-based: access time ของnode ที่ i ขึ้นกับi Insertion and deletions –Array-based: ต้องมีการ shift data –Pointer-based: ต้องทำ list traversal

14 21 August 2014 204351 Data Structures 14 Dummy Head Nodes Dummy head node –จะปรากฏอยู่เสมอ แม้ว่า linked list จะ empty –Insertion และ deletion algorithms initialize prev ชี้ไปที่ dummy head node มากกว่าที่จะเป็น NULL

15 21 August 2014 204351 Data Structures 15 ADT sorted list Specification –ItemType : type ของ สมาชิกใน list –สมาชิกมีการจัดเรียงลำดับตามค่า key operations –createSortedList() –destroySortedList() –sortedIsEmpty(): boolean –sortedGetLength(): integer –sortedInsert(in newItem:itemType) –sortedRemove(in anItem:itemType) –sortedRetrieve(in index:integer, out dataItem:itemType)

16 21 August 2014 204351 Data Structures 16 Sorted linked list การหาตำแหน่งที่จะ insert หรือ delete สำหรับ sorted linked list find (itemType newItem) prev = null; cur = head; while(newItem>cur.item and cur!= null) prev = cur; cur = cur.next;

17 21 August 2014 204351 Data Structures 17 Circular Linked Lists node สุดท้าย ชี้ไปที่ node แรก ทุก node มี successor ไม่มี node ใดใน circular linked list มี ค่า NULL circular linked list

18 21 August 2014 204351 Data Structures 18 Doubly Linked Lists แต่ละ node มี pointer ชี้ไป predecessor และ successor ของ nodeนั้น Circular doubly linked list –pre pointer ของ dummy head node ชี้ไป ที่ node สุดท้าย –next pointer ของ node สุดท้าย ชี้ไปที่ dummy head node –ไม่ต้องมี special cases สำหรับการ insert และ delete

19 21 August 2014 204351 Data Structures 19 Doubly Linked Lists (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

20 21 August 2014 204351 Data Structures 20 Doubly Linked Lists การ delete node cur (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede; การ insert new nodeไว้ก่อน node cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

21 21 August 2014 204351 Data Structures 21 Application: DVD store Maintaining an inventory– list of movies titles Each title has the following information –title: DVD title –have_value : number of DVDs currently in stock –want_value: number of DVDs that should be in stock. If have_value < want_value, more DVDs are order.

22 21 August 2014 204351 Data Structures 22 Application: Maintaining an Inventory Operations on the inventory –List the inventory in alphabetical order by title –Add new title to inventory –Modify want_value for a specified title –Sell DVDs – decrease have_value for specified title –Order more DVDs if have_value < want_value –Find the inventory item associated with title

23 Application : Polynomial function Polynomial linked list 21 August 2014 204351 Data Structures 23


ดาวน์โหลด ppt 21 August 2014 204351 ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list.

งานนำเสนอที่คล้ายกัน


Ads by Google