Linked List KMITL 01076249 Data Structures Algorithms Linked
- Slides: 33
Linked List รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
List http: //www. gograph. com/vector-clip-art/queue. html รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
List insert / delete To Buy : 1. Bread 2. Milk 3. Eggs 4. Fruit 5. Rice Salmon 6. Pasta 7. Butter 8. Juice head tail Already bought Eggs, Rice, Bread, Juice Oh I forgot Salmon รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
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
Logical Abstract Data Type & Implementation Logical ADT : ขนกบ application 1. Data : ของมลำดบ มปลาย หว head และ/หรอ ทาย tail 2. Methods : ขนกบ Data Implementation ? Python List และ list เปน ordered list หรอไม รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Unordered List / Ordered List • List() • is. Empty() • size() • search(item) • index(item) สราง empty list returns boolean วา empty list หรอไม returns จำนวนของใน list returns วาม item ใน list หรอไม returns index ของ item กำหนดให • add(item) ไมมอยกอนใน • append(item) ไมมอยกอนใน # ordered list • add(item) insert(pos, item) ไมมอยกอนใน • remove(item) adds item เขา list ไม Returns คดวา list adds item ทาย list ไม Returns คดวา list adds item เขา list ตามลำดบ ไม Returns adds item ท index pos ไม Returns คดวา list removes & return item คดวา # unordered list • pop() • pop(pos) รศ. ดร. บญธร item อยใน list item มอยใน list removes & return item ตวสดทาย list _size >= 1 removes & return item ตวท index = pos list _size >= 1 เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
List Implementation : C Sequential (Implicit) Array, Python List : 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 C D 0 1 2 3 Python : List head 4 0 1 A B C 4 1 2 รศ. กฤตวน ศรบรณ KMITL 4 D None head A เครอตราช 3 i 0 รศ. ดร. บญธร 2 B C 3 4 D 01076249 Data Structures & Algorithms : Linked None
Linked List Problem : Fix Positions list A B C D 0 1 2 3 List 4 C List Implicit (Sequential ) Array (C) 1 data next C 15 node 3 head ลำดบ order? Linked List Unfix Positions data next A 7 7 data next B 1 15 data next D - 3 head 0 1 2 3 4 head A B C D None Python : List data next A node เชน link แทนดวยลกศร แทนการเชอมโยงกน เครอตราช next B data C next data D Logical linked list physical (implementation) โครงสรางทใชในการสรางจรง Logical คอใน ความคดของเรา รศ. ดร. บญธร link data เชน link อาจใช pointer หรอ index ของ array รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked next
Solve Inserting Expensive Shifting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Solve Deleting Expensive Shifting Problem รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
tail head data next A link data next data B next C data next D node Linked List Data : 1. data 2. next (link) 3. head 4. tail ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL node class list class 01076249 Data Structures & Algorithms : Linked List 11
Node Class / List Class 1. Data : __init__() : constructor ������� 2 underscores รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List 12
Node Class tail head data next A link data next B data next C data next D node p p = node('A', None) class node: def __init__(self, data, next = None): self. data = data if next is None: self. next = None else: self. next = next def __str__(self): return str(self. data) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
List Class class list: def __init__(self, head = None): """ unordered singly linked list with head """ def __init__(self): l 1 = list() self. head = None """ unordered singly linked list can set default list with head, tail & size """ if head == None: head tail None l 3 = list() self. head = self. tail = None self. size = 0 """ unordered singly linked list with head & tail """ def __init__(self): else: t tail ? l 4 = list(head) head l 2 = list() self. head = head t = self. head = self. tail = None self. size = 1 while t. next != None: # locating tail & find size t = t. next self. size += 1 self. tail = t รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
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 15
Creating a List class node: def __init__(self, data, next = None): self. data = data if next == None: self. next = None else: self. next = next node('A', None) class list: """ unordered singly linked list with head """ def __init__(self): self. head = None 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 l = list() l. append('A') self. head None p self. t รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
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
Delete After delete a node after a node pointed by q delete. After(q) q ? p รศ. ดร. บญธร เครอตราช 2 1 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
print list Design how to call. head p NULL output: B C D p is not None: while p != None: print(p. data) p = p. next } รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Linked List VS Sequential Array head Python : List 0 1 A B 2 3 C 4 D None Sequential Array Linked List • Solved. Random Access. • Sequential Access. • C array : Automatic Allocation. Python List array : Dynamic Allocation • Node : Dynamic Allocation. • Lifetime : C-array, Python List • from defined until its scope finishes. • Node Lifetime : from allocated (C : malloc()/new, python: instantiate obj) until C: deallocated by free()/delete, Python : no reference. • Only keeps data. • Need spaces for linkes. • Insertion / Deletion • รศ. ดร. บญธร เครอตราช Shifting Problem. รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
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
Head & Tail Nodes tail head Circular List tail Why ptr to tail ? Why not ptr to head? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Doubly VS Singly Linked List head prev data next previous prev data next tail 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
Linked Stack top Check if it support every operations. Linked Queue rear front Can switch front & rear ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
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
Lab : Bottom Up Lift it x% Where to h t 1 Lift_up ? I Try to print h 2. love x% h 2 Take the you bottom up. Opps ! very t 2 infinite loop ! much C++ รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Lab : Riffle Shuffle h 1 t 1 4 1 5 2 6 3 h 2 h 1 t 2 4 1 5 2 6 3 7 7 8 8 t 1 9 9 h 2 t 2 • 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
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
Applications • Polynomial Expression • Multilists • Radix Sort รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked
Polynomial expression A=5 x 3 + 4 x 2 - 7 x + 10 + B= x 3 + 2 x - 8 + How about. . . ? 5 x 85 + 7 x + 1 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
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
Radix Sort input: 125 343 1 0 729 27 512 216 8 64 0 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
Radix Sort input: 125 0 343 1 1 512 0 729 343 64 27 125 512 216 8 27 64 8 9876543210 8 1 0 216 512 729 27 125 64 343 9876543210 64 27 8 1 0 125 9876543210 216 343 512 729 output: 729 343 216 125 ศรบรณ 64 27 8 KMITL 1 001076249 Data Structures & Algorithms : Linked รศ. ดร. บญธร 512 เครอตราช รศ. กฤตวน 729
- In linked list the successive elements
- Singly linked list vs doubly linked list
- Fungsi linked list
- Ajit diwan
- Cos 423 princeton
- Data structures and algorithms tutorial
- Information retrieval data structures and algorithms
- Data structures and algorithms bits pilani
- Ajit diwan iit bombay
- Algorithms + data structures = programs
- Data structures and algorithms
- Data structures and algorithms
- Waterloo data structures and algorithms
- Signature file structure in information retrieval system
- Data structures and algorithms
- Kris kmitl
- Kris kmitl
- Cmit kmitl
- Kmitl color
- Kris kmitl
- Turnitin kmitl
- Kris kmitl
- Java data structures
- Addition of two polynomials using linked list in c
- Polynomial addition using linked list in c
- List of greedy algorithms
- Simple recursive algorithms
- Homologous structures and analogous structures
- Muthukrishnan data stream algorithms
- Generic linked list in c
- Jenis linked list
- Singly vs doubly linked list
- Tan is a lover of linked list
- Xor linkedlist