Linked List KMITL 01076249 Data Structures Algorithms Linked

  • Slides: 35
Download presentation
Linked List รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures &

Linked List รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

List ? http: //www. gograph. com/vector-clip-art/queue. html รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ

List ? http: //www. gograph. com/vector-clip-art/queue. html รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

List insert / delete To Buy : 1. Bread 2. Milk 3. Eggs 4.

List insert / delete To Buy : 1. Bread 2. Milk 3. Eggs 4. Fruit 5. Rice 6. Pasta 7. Butter 8. Juice head tail Already bought Eggs, Rice, Bread, Juice Oh I forgot Salmon รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Ordered List – Unordered List To Buy : 1. Bread 2. Milk 3. Eggs

Ordered List – Unordered List To Buy : 1. Bread 2. Milk 3. Eggs 4. Fruit 5. Rice 6. Pasta 7. Butter 8. Juice Scores : 1. Bruce 2. Tom 3. Ben 4. Max 5. Tim 6. Marry 7. Ron 8. Harry Unordered List รศ. ดร. บญธร เครอตราช 2 3 5 7 7 8 9 10 Ordered List Ascending Order รศ. กฤตวน ศรบรณ KMITL 98 n 5 – 4 n 4 + n 3 – 8 n 2 + 5 n + 7 Ordered List Decending Order 01076249 Data Structures & Algorithms : Linked List

Logical Abstract Data Type & Implementation Logical ADT : ขนกบ 1. Data : ของมลำดบ

Logical Abstract Data Type & Implementation Logical ADT : ขนกบ 1. Data : ของมลำดบ ทาย tail application มปลาย หว head และ/หรอ Data Implementation ? Python List 2. Methods : ขนกบ และ list เปน ordered list หรอไม รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

 • List() • is. Empty() • size() • search(item) • index(item) list สราง

• List() • is. Empty() • size() • search(item) • index(item) list สราง empty list Unordered List returns boolean วา empty list หรอไม returns จำนวนของใน list returns วาม item ใน list หรอไม returns index ของ item กำหนดให item อยใน • 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 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List • pop(pos) removes & return item ตวท index = pos list _size

Ordered List • List() • is. Empty() • size() • search(item) • index(item) list

Ordered List • List() • is. Empty() • size() • search(item) • index(item) list สราง empty list returns boolean วา empty list หรอไม returns จำนวนของใน list returns วาม item ใน list หรอไม returns index ของ item กำหนดให • add(item) adds item เขา list ตามลำดบ ไม Returns item อยใน • 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 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Example Python Code def __init__(self): self. item = [] def size(self): return len(self. item)

Example Python Code def __init__(self): self. item = [] def size(self): return len(self. item) def is. Empty(self): return self. item == [] def insert(self, i): # insert i ������ decending order if self. is. Empty(): 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) else: self. item[ind] += i รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Linked List 1. Data : __init__() : constructor ������� 2 underscores รศ. ดร. บญธร

Linked List 1. Data : __init__() : constructor ������� 2 underscores รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List 9

List : Sequential (Implicit) Array (Python List) Implementation : To Buy 1. A 2.

List : Sequential (Implicit) Array (Python List) Implementation : To Buy 1. A 2. B 3. C 4. D Problem : fix positions i A B C D C : Sequential Array insert i ? : shift out delete : shift in head A B 0 1 C 2 D 3 A B C D 0 1 2 3 Python : List head 4 0 1 A B 4 รศ. กฤตวน ศรบรณ KMITL C 4 D head A เครอตราช 3 i 0 รศ. ดร. บญธร 2 1 B 2 C 3 4 D 01076249 Data Structures & Algorithms : Linked List

C Linked List Problem : Fix Positions list A B C D 0 1

C Linked List Problem : Fix Positions list A B C D 0 1 2 3 4 List Phyton List Implicit (Sequential ) Array (C) 1 data next C 15 node 3 ลำดบ order? Linked List Unfix Positions data next A 7 7 data next B 1 data next 15 data next D - head 3 head data next A node link data next B C D physical (implementation) โครงสรางทใชในการสรางจรง เชน link แทนดวยลกศร แทนการเชอมโยงกน เครอตราช next Logical linked list Logical คอใน ความคดของเรา รศ. ดร. บญธร data เชน link อาจใช pointer หรอ index ของ array รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Solve Inserting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures

Solve Inserting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Solve Deleting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures

Solve Deleting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

head data next A link data next B data C next data next D

head data next A link data next B data C next data next D node Linked List Data : 1. node 2. link 3. head 4. tail ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL node class list class 01076249 Data Structures & Algorithms : Linked List 14

Node Class head data next A link data next B data next C data

Node Class head data next A link data next B data next C data next D node class node(): def __init__(self, data, next = None): self. data = data self. next = next def __str__(self): return str(self. data) def get. Data(self): return self. data # accessor def get. Next(self): return self. next # accessor def set. Deata(self, data): # mutator self. data = data def set. Next(self, next): self. next = next รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL # mutator 01076249 Data Structures & Algorithms : Linked List

List Class head data next A link data next B data C next data

List Class head data next A link data next B data C next data next D node class list(): """ unordered singly linked list with head """ def __init__(self): self. head = None """ unordered singly linked list with head & tail """ def __init__(self): self. head = self. tail = 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 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Methods . 1__init__() : �������. 2 size(): . 3 is. Empty(): . 4 append

Methods . 1__init__() : �������. 2 size(): . 3 is. Empty(): . 4 append () : add at the end. 5__str__(): . 6 add. Head() : �������. 7 remove(item): . 8 remove. Tail(): . 9 remove. Head() : . 10 is. In(item): / search(item). 11. . . รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List 17

Creating a List class node(): def __init__(self, data, next = None): self. data =

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 """ l = list() def __init__(self): self. head = None 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. next != None : t = t. next = p รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ t KMITL 01076249 Data Structures & Algorithms : Linked List

Insert After q insert node data after a node pointed by q insert. After(‘i’,

Insert After q insert node data after a node pointed by q insert. After(‘i’, q) 2 1 ? Why insert after ? Can you insert before ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Delete After delete a node after a node pointed by q delete. After(q) q

Delete After delete a node after a node pointed by q delete. After(q) q ? p รศ. ดร. บญธร เครอตราช 2 1 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

print list Design how to call. head p NULL output: B C D while

print list Design how to call. head p NULL output: B C D while p != None print(p) p = p. next } รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Dynamic VS Sequential Array Dynamic • Insertion / Deletion Shifting Problem. • Solved. •

Dynamic VS Sequential Array Dynamic • Insertion / Deletion Shifting Problem. • Solved. • Random Access. • Sequential Access. • Automatic Allocation. • Dynamic Allocation. • Lifetime : from defined until its scope finishes. • • Only keeps data. Lifetime : from allocated by malloc()/new until deallocated by free()/delete. • Need spaces for linkes. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Dummy Node To insert & delete at 1 st position change head ie. make

Dummy Node To insert & delete at 1 st position change head ie. make special case. p “Dummy Node” solves the problem. head dummy head Empty List has a dummy node. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ dummy KMITL 01076249 Data Structures & Algorithms : Linked List

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

Head & Tail Nodes tail head Circular List tail Why ptr to tail ? Why not ptr to head? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

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

Doubly VS Singly Linked List head tail prev data next previous prev data next Doubly Circular List tail prev data next รศ. ดร. บญธร เครอตราช prev data next รศ. กฤตวน ศรบรณ prev data next KMITL prev data next 01076249 Data Structures & Algorithms : Linked List

Linked Stack top Check if it support every operations. Linked Queue rear front Can

Linked Stack top Check if it support every operations. Linked Queue rear front Can switch front & rear ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Linked Queue front rear How do they link? Support every operations ? front rear

Linked Queue front rear How do they link? Support every operations ? front rear en. Queue ? (insert) de. Queue ? (delete) Every operations ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Lab : Bottom Up h Lift it x% Where to t 1 Lift_up ?

Lab : Bottom Up h Lift it x% Where to t 1 Lift_up ? I Try to print h 2. love x% h 2 you Take the bottom up. Opps ! very t 2 infinite loop ! much C++ รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Lab : Riffle Shuffle h 2 h 1 4 1 5 2 6 3

Lab : Riffle Shuffle h 2 h 1 4 1 5 2 6 3 t 2 7 8 t 1 h 2 h 1 t 1 9 8 9 • Riffle Shuffle each node of each packet from the top. • put the rest at the back of the result packet. • Lift up to 2 packets. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Lab : Riffle Shuffle h 1 h 2 4 1 5 2 t 2

Lab : Riffle Shuffle h 1 h 2 4 1 5 2 t 2 6 6 3 7 7 t 1 8 t 1 9 3 8 • Riffle Shuffle each node of each packet from the top. 9 • put the rest at the back of the result packet. Lift up to 2 packets. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Applications • Polynomial Expression • Multilists • Radix Sort รศ. ดร. บญธร เครอตราช รศ.

Applications • Polynomial Expression • Multilists • Radix Sort รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Polynomial expression How about. . . ? A=5 x 3 + 4 x 2

Polynomial expression How about. . . ? A=5 x 3 + 4 x 2 - 7 x + 10 + B= x 3 5 x 85 + 7 x + 1 + + 2 x - 8 x 76 - 8 C=6 x 3 + 4 x 2 - 5 x + 2 What data structure will you use? Array? Sparse -> Linked List ( has lots of 0 data ) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List

Multilists . 1 class หนงๆ ใดบาง 0 s 1 0 C 1 2 s

Multilists . 1 class หนงๆ ใดบาง 0 s 1 0 C 1 2 s 3 3 s 4 s 3 c 1 s 1 c 3 s 3 c 1 s 5 c 2 s 3 c 3 s 3 c 4 เครอตราช รศ. กฤตวน ศรบรณ ลง class 4 s 5 s 3 c 2 3 C 4 รศ. ดร. บญธร 1 s 2 s 1 c 1 1 C 2 2 C 3 มใครลงบาง 2. นร. คนหนงๆ s 4 c 3 s 4 c 4 KMITL 01076249 Data Structures & Algorithms : Linked List

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

Radix Sort input: 0 125 343 1 0 729 27 512 216 8 64 1 251 334 9876543210 8 1 0 927 612 72 215 521 9876543210 64 27 8 1 0 251 162 433 46 512 621 72 8 972 46 343 125 9876543210 output: 729 512 343 216 125 64 27 8 1 0 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 297 01076249 Data Structures & Algorithms : Linked List

Radix Sort input: 125 0 1 343 1 0 512 343 729 27 64

Radix Sort input: 125 0 1 343 1 0 512 343 729 27 64 512 125 216 8 27 64 8 729 9876543210 8 1 0 216 512 729 27 125 64 343 9876543210 64 27 8 1 0 125 216 343 512 729 9876543210 output: 729 512 343 216 125 64 27 8 1 0 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List