Download cur

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
2. ADT List
Unsorted list ADT
- list implementation
- Sorted List
- Circular list
- Doubly linked list
-
5 May 2017
204351 ดรุณี ศมาวรรตกุล
1
Unsorted list ADT
• Specification
ิ ใน list
– ItemType : type ของ สมาชก
ิ ใน list
– index: ตาแหน่งของสมาชก
• 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
5 May 2017
204351 Data Structures
2
Array-based implementation
• Data structure
const int maxList = 100; -- public
typedef int itemType; -- public
itemType items [maxList];-private
int size; -- private
• Operations
ื่ operations
– Public – ชอ
– Private – ชุดคาสงั่ ของแต่ละ operation
5 May 2017
204351 Data Structures
3
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 May 2017
204351 Data Structures
4
Array-based implementation
• remove (int index)
• retrieve(int index, itemType&
dataItem)
5 May 2017
204351 Data Structures
5
Pointer-based implementation
Linked Lists
• Declare node ใน linked list -- private
struct Node
{ itemType item
Node *next;
}; //end struct
int size;
Node *head;
Node
• Allocate node แบบ dynamic
Node *p;
p = new Node;
5 May 2017
204351 Data Structures
6
Pointer-based implementation
• Insert ที่ head
• Insert ทีต
่ าแหน่งใดๆ
• Insert ทีต
่ าแหน่งท ้ายสุด
5 May 2017
204351 Data Structures
7
Pointer-based implementation
•
insert(int index, itemType newItem)
5 May 2017
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;
}
}
}
204351 Data Structures
8
Pointer-based implementation
• Find (int index) // find a node at position index
if (index<1 or index>getLength())
return null;
else
{
cur = head;
for (i=1; i<index; ++i)
cur = cur.next;
return cur
5 May 2017
204351 Data Structures
9
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;
5 May 2017
204351 Data Structures
10
Pointer-based implementation
• remove (int index)
5 May 2017
204351 Data Structures
11
Display contents ใน Linked List
• Traverse operation
cur = head;
while (cur != NULL)
{
cur = cur.next;
print (cur.item);
}
5 May 2017
204351 Data Structures
12
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
5 May 2017
204351 Data Structures
13
Dummy Head Nodes
• Dummy head node
– จะปรากฏอยูเ่ สมอ แม ้ว่า linked list จะ
empty
– Insertion และ deletion algorithms
initialize prev ชไี้ ปที่ dummy head node
มากกว่าทีจ
่ ะเป็ น NULL
5 May 2017
204351 Data Structures
14
ADT sorted list
• Specification
ิ ใน list
– ItemType : type ของ สมาชก
ิ มีการจัดเรียงลาดับตามค่า key
– สมาชก
• operations
–
–
–
–
–
–
–
5 May 2017
createSortedList()
destroySortedList()
sortedIsEmpty(): boolean
sortedGetLength(): integer
sortedInsert(in newItem:itemType)
sortedRemove(in anItem:itemType)
sortedRetrieve(in index:integer, out
dataItem:itemType)
204351 Data Structures
15
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;
5 May 2017
204351 Data Structures
16
Circular Linked Lists
• node สุดท ้าย ชไี้ ปที่ node แรก
• ทุก node มี successor
• ไม่ม ี node ใดใน circular linked list มี
ค่า NULL
circular linked list
5 May 2017
204351 Data Structures
17
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
5 May 2017
204351 Data Structures
18
Doubly Linked Lists
(a) A circular doubly linked list with a dummy head node
(b) An empty list with a dummy head node
5 May 2017
204351 Data Structures
19
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;
5 May 2017
204351 Data Structures
20
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.
5 May 2017
204351 Data Structures
21
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
5 May 2017
204351 Data Structures
22
Application : Polynomial function
• Polynomial linked list
5 May 2017
204351 Data Structures
23
Related documents