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

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

Linked List.

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


งานนำเสนอเรื่อง: "Linked List."— ใบสำเนางานนำเสนอ:

1 Linked List

2 List List ?

3 List head insert / delete tail To Buy : Bread Milk Eggs Fruit Rice
Pasta Butter Juice head tail insert / delete Already bought Eggs, Rice, Bread, Juice Oh I forgot Salmon

4 Ordered List – Unordered List
To Buy : Bread Milk Eggs Fruit Rice Pasta Butter Juice Unordered List Ordered List Ascending Order Scores : Bruce 2 Tom 3 Ben 5 Max 7 Tim 7 Marry 8 Ron 9 Harry Ordered List Decending Order 98n5 – 4n4 + n3 – 8n2 + 5n + 7

5 Logical Abstract Data Type & Implementation
Logical ADT : ขึ้นกับ application Data : ของมีลำดับ มีปลาย หัว head และ/หรือ ท้าย tail Methods :ขึ้นกับ และ list เป็น ordered list หรือไม่ Data Implementation ? Python List

6 List() สร้าง empty list
Unordered List List()  สร้าง empty list isEmpty()  returns boolean ว่า empty list หรือไม่ size()  returns จำนวนของใน list search(item)  returns ว่ามี item ใน list หรือไม่ index(item)  returns index ของ item กำหนดให้ item อยู่ใน list add(item)  adds item เข้า list ไม่ Returns คิดว่า item ไม่มีอยู่ก่อนใน list append(item)  adds item ท้าย list ไม่ Returns คิดว่า item ไม่มีอยู่ก่อนใน list insert(pos,item)  adds item ที่ index pos ไม่ Returns คิดว่า item ไม่มีอยู่ก่อนใน list remove(item)  removes & return item คิดว่า item มีอยู่ใน list pop()  removes & return item ตัวสุดท้าย list _size >= 1 pop(pos)  removes & return item ตัวที่ index = pos list _size >= 1

7 List() สร้าง empty list
Ordered List List()  สร้าง empty list isEmpty()  returns boolean ว่า empty list หรือไม่ size()  returns จำนวนของใน list search(item)  returns ว่ามี item ใน list หรือไม่ index(item)  returns index ของ item กำหนดให้ item อยู่ใน list add(item)  adds item เข้า list ตามลำดับ ไม่ Returns remove(item)  removes & return item จาก list คิดว่า item มีอยู่ใน list pop()  removes & return item ตัวสุดท้าย list _size >= 1 pop(pos)  removes & return item ตัวที่ index = pos list _size >= 1

8 Example Python Code def __init__(self): self.item = [] def size(self): return len(self.item) def isEmpty(self): return self.item == [] def insert(self, i): # insert i เพื่อให้ได้ decending order if self.isEmpty(): self.item.append(i) else: ind = 0 while ind < self.size() and self.item[ind] > i: ind += 1 if ind == self.size() or self.item[ind] != i: self.item.insert(ind,i) self.item[ind] += i

9 Linked List 1. Data : __init__() : constructor ให้ค่าตั้งต้น
2 underscores 2 underscores

10 List : Sequential (Implicit) Array (Python List) Implementation
To Buy : A B C D Problem : fix positions i A B C D C : Sequential Array Python : List insert i ? : shift out head A 1 2 3 4 B C D head 1 2 3 4 A B C D delete : shift in head A 1 2 3 4 i B C D head 1 2 3 4 A C D B

11 Implicit (Sequential ) Array (C)
C Linked List Problem : Fix Positions List Unfix Positions Linked List ลำดับ order? list A B C D 1 2 3 4 data 1 C next 15 data 3 A next 7 data 7 B next 1 data 15 D next - Phyton List Implicit (Sequential ) Array (C) node head 3 head data next A B C D link node Logical linked list Logical คือในความคิดของเรา เช่น link แทนด้วยลูกศร แทนการเชื่อมโยงกัน physical (implementation) โครงสร้างที่ใช้ในการสร้างจริง เช่น link อาจใช้ pointer หรือ index ของ array

12 Solve Inserting Problem

13 Solve Deleting Problem

14 Linked List Data : node link node class head tail ? list class A B C D
next A B C D link node Linked List Data : node link head tail ? node class list class

15 Node Class A B C D link head node def getData(self): # accessor
next A B C D link node def getData(self): # accessor return self.data def getNext(self): # accessor return self.next def setDeata(self, data): # mutator self.data = data def setNext(self, next): # mutator self.next = next class node(): def __init__(self, data, next = None): self.data = data self.next = next def __str__(self): return str(self.data)

16 List Class A B C D link head node class list():
data next A B C D link node class list(): """ unordered singly linked list with head """ def __init__(self): self.head = None def __init__(self, head = None): """ unordered singly linked list can set default list with head, tail & size """ if head == None: self.head = self.tail = None self.size = 0 else: self.head = head t = self.head self.size = 1 while t.next != None: # locating tail & find size t = t.next self.size += 1 """ unordered singly linked list with head & tail """ def __init__(self): self.head = self.tail = None

17 Methods 1. __init__() : ให้ค่าตั้งต้น 6. addHead() : ให้ค่าตั้งต้น
2. size(): 7. remove(item): 3. isEmpty(): 8. removeTail(): 4. append () : add at the end 9. removeHead() : 5. __str__(): 10. isIn(item): / search(item)

18 Creating a List class node(): def __init__(self, data, next = None):
self.data = data self.next = next class list(): """ unordered singly linked list with head """ def __init__(self): self.head = None l = list() head None p def append(self, data): """ add at the end of list""" p = node(data) if self.head == None: # null list self.head = p else: t = self.head while : t = t.next t.next = p t t.next != None

19 ? 2 1 Insert After insert node data after a node pointed by q
insertAfter(‘i’,q) q 1 2 ? Why insert after ? Can you insert before ?

20 ? 2 1 Delete After delete a node after a node pointed by q
deleteAfter(q) q ? 2 p 1

21 output: B C D while p != None print(p) p = p.next } print list head p
Design how to call. head p NULL output: B C D while p != None print(p) p = p.next }

22 Dynamic VS Sequential Array
Insertion / Deletion Shifting Problem. Random Access. Automatic Allocation. Lifetime : from defined until its scope finishes. Only keeps data. Solved. Sequential Access. Dynamic Allocation. Lifetime : from allocated by malloc()/new until deallocated by free()/delete. Need spaces for linkes.

23 dummy dummy Dummy Node p “Dummy Node” solves the problem. head head
To insert & delete at 1st position change head ie. make special case. p “Dummy Node” solves the problem. dummy head dummy head Empty List has a dummy node.

24 Why ptr to tail ? Why not ptr to head?
Head & Tail Nodes tail head Circular List tail Why ptr to tail ? Why not ptr to head?

25 Doubly VS Singly Linked List
tail head prev data next previous Doubly Circular List tail prev data next

26 Check if it support every operations.
Linked Stack top Check if it support every operations. Linked Queue rear front Can switch front & rear ?

27 Linked Queue front rear front rear Every operations ? front rear
How do they link? Support every operations ? front rear enQueue ? (insert) deQueue ? (delete) Every operations ?

28 Lab : Bottom Up h I t1 Try to print h2. love h2 you Opps !
Lift it x% Where to Lift_up ? x% I t1 Try to print h2. love h2 you Take the bottom up. Opps ! infinite loop ! very much t2 C++

29 Lab : Riffle Shuffle h1 h2 h1 h2 4 1 4 1 5 2 5 2 t2 t2 6 3 6 3 7 7
Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

30 Lab : Riffle Shuffle h1 h1 h2 h2 4 1 4 1 5 5 2 2 t2 t2 6 6 3 3 7 7 Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

31 Polynomial Expression
Applications Polynomial Expression Multilists Radix Sort

32 Polynomial expression
How about ... ? 5x x + 1 x + A=5x3 + 4x2 - 7x + 10 B= x x - 8 + C=6x3 + 4x2 - 5x + 2 What data structure will you use? Array? Array? Sparse -> Linked List ( has lots of 0 data )

33 1. class หนึ่งๆ มีใครลงบ้าง 2. นร. คนหนึ่งๆ ลง class ใดบ้าง
Multilists 1. class หนึ่งๆ มีใครลงบ้าง นร. คนหนึ่งๆ ลง class ใดบ้าง C1 C2 C3 C4 s s s s s5 1 2 3 s1 c1 s3 c1 s3 c2 s5 c2 s3 c3 s4 c3 s1 c3 s3 c4 s4 c4

34 Radix Sort input: 1 512 343 64 125 216 27 8 729 8 729 1 216 27 512 125 343 64 64 27 8 1 125 216 343 512 729 output:

35 Radix Sort input: 64 8 216 512 27 729 1 343 125 64 8 216 512 27 729 1 343 125 64 8 216 512 27 729 1 343 125 output:


ดาวน์โหลด ppt Linked List.

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


Ads by Google