Linked List KMITL 01076249 Data Structures Algorithms Linked
- Slides: 33
Linked List ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
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. 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 List
Ordered List – Unordered List To Buy : 1. Bread 2. Milk 3. Eggs 4. Fruit 5. Rice 6. Pasta 7. Butter 8. Juice Unordered List Scores : 1. Bruce 2. Tom 3. Ben 4. Max 5. Tim 6. Marry 7. Ron 8. Harry 2 3 5 7 7 8 9 10 Ordered List Ascending Order 98 n 5 – 4 n 4 + n 3 – 8 n 2 + 5 n + 7 Ordered List Decending Order ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
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 Unordered List / Ordered List สราง empty list returns boolean วา empty list หรอไม returns จำนวนของใน list returns วาม item ใน list หรอไม returns index ของ item กำหนดให item อยใน adds item เขา list ไม Returns คดวา item • add(item) ไมมอยกอนใน list • append(item) adds item ทาย list ไม Returns คดวา item ไมมอยกอนใน list add(item) adds item เขา list ตามลำดบ ไม Returns //Ordered List • insert(pos, item) adds item ท index pos ไม Returns คดวา item • remove(item) removes & return item คดวา 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
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 insert i ? : shift out delete : shift in C : Sequential Array head A B 0 1 C 2 D 3 A B C D 0 1 2 3 Python : List head 4 0 1 2 3 A B C 1 2 4 D None i 4 head 0 A B C 3 4 D ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List 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 ลำดบ order? Linked List Unfix Positions data next A 7 7 data next B 1 15 data next D - head 3 head 0 1 2 3 4 head A B C D None Python : List Logical คอใน ความคดของเรา เชน link แทนดวยลกศร แทนการเชอมโยงกน data next A node link data next B data C next data D Logical linked list physical (implementation) โครงสรางทใชในการสรางจรง เชน link อาจใช pointer หรอ index ของ array ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List next
Solve Inserting Problem ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
Solve Deleting Problem ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
tail head data next A link data next B data C next data next D node Linked List Data : 1. data 2. 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) 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 # mutator ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
List Class class list: def __init__(self, head = None): """ unordered singly linked list with head """ l 1 = list() def __init__(self): self. head = None """ unordered singly linked list with head & tail """ l 2 = list() def __init__(self): self. head = self. tail = None """ unordered singly linked list can set default list with head, tail & size """ if head == None: self. head = self. tail = None l 3 = list(head) 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 self. tail = t ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
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 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 class node: def __init__(self, data, next = None): self. data = data if next == None: self. next = None else: self. next = next l = list() l. append('A') self. head None p self. t ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
Insert After q insert. After(‘i’, q) 2 1 insert node data after a node pointed by q ? 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 ? p 2 1 ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
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 List
Linked List VS Sequential Array head Python : List 0 1 A B 2 C 3 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 List
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 ? 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 Circular List tail prev data next ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
Linked Stack top Check if it support every operations. Linked Queue front rear Can switch front & rear ? ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
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 ? I love x% Try to print 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 t 2 7 t 1 h 2 h 1 7 8 t 1 9 t 2 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 6 6 3 7 7 t 1 8 9 t 1 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 ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
Polynomial expression How about. . . ? A=5 x 3 + 4 x 2 - 7 x + 10 + B= x 3 + 2 x - 8 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 List
Multilists . 1 class หนงๆ มใครลงบาง 2. นร. คนหนงๆ ลง class ใดบาง 0 s 1 0 C 1 3 C 4 2 s 3 s 1 c 1 3 s 4 s 3 c 1 4 s 5 s 3 c 1 s 3 c 2 1 C 2 2 C 3 1 s 2 s 1 c 3 s 5 c 2 s 3 c 3 s 3 c 4 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 1 51 34 9876543210 2 3 8 27 1 12 2 921 0 15 6 7 9876543210 2 5 6 24 8 71 0 1 43 2 9876543210 25 16 3 output: 729 512 343 216 6 4 12 5 21 6 8 72 9 2 7 6 4 43 3 5 12 125 64 27 8 1 0 27 9 ��. ���������� ��. ��������� KMITL 01076249 Data Structures & Algorithms : Linked List
Radix Sort input: 125 0 1 343 1 0 512 343 729 27 64 512 125 216 8 27 9876543210 8 1 0 216 512 729 27 125 64 8 72 9 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
- Advantage of linked list
- Difference between an array and a linked list
- Perbedaan single linked list dan double linked list
- Data structures and algorithms iit bombay
- Cos 423
- Data structures and algorithms tutorial
- Information retrieval data structures and algorithms
- Data structures and algorithms bits pilani
- Ajit diwan iitb
- Algorithms + data structures = programs
- Data structures and algorithms
- Data structures and algorithms
- Waterloo data structures and algorithms
- Information retrieval data structures and algorithms
- Data structures and algorithms
- Kris kmitl
- Kris kmitl
- Cmit kmitl
- Kmitl color
- Kris kmitl
- Turnitin kmitl
- Kris kmitl
- Java data structures
- Polynomial manipulation using linked list
- Polynomial addition using linked list in c
- List of greedy algorithms
- List of recursive algorithms
- Function of homologous structure
- Stream data model
- Generic linked list in c
- Jenis linked list
- Singly vs doubly linked list
- Market basket analysis
- Xor linked list c++