2 - Linked List Implementation I - 2101637541 - Vick Koesoemo Santoso
2 - Linked List Implementation I - 2101637541 - Vick Koesoemo Santoso
Single Linked List
Single linked list adalah
sekumpulan node yang terhubung ke node lain melalui pointer.
Single linked list Insert
(PushHead) = jika kita ingin
memasukkan node baru dari depan
struct tnode *node =
(struct tnode*)
malloc(sizeof(struct tnode));
node->value = x;
node->next = head;
head = node;
Single linked list delete
// if x is in head node
if ( head->value == x ) {
head = head->next;
free(curr);
}
}
// if x is in tail node
else if(tail->value ==
x){
while(curr->next!=tail) curr = curr->next;
free(tail); tail = curr;
tail->next = NULL;
}
// if x is not in head node, find the location
else {
while (
curr->next->value != x ) curr = curr->next;
struct tnode *del =
curr->next;
curr->next =
del->next;
free(del);
}
Polynomial
Representation
Circular
Single Linked List
Di circular linked list node terakhir berisi pointer ke
node pertama
Tidak ada nilai NULL
Doubly Linked
List
Jika
ingin menambahkan node baru dari belakang(tail).
struct tnode *node =
(struct
tnode*) malloc(sizeof(struct tnode));
node->value = x;
node->next
= NULL;
node->prev
= tail;
tail->next
= node;
tail = node;
Jika
ingin menambahkan node baru dari posisi tertentu diantara depan dan belakang.
struct tnode *a = ??;
struct tnode *b = ??;
// the new node will be inserted between a and b
struct tnode *node =
(struct
tnode*) malloc(sizeof(struct tnode));
node->value =
x;
node->next =
b;
node->prev =
a;
a->next =
node;
b->prev =
node;
Doubly linked list delete
1.
Jika node dihapus adalah satu satunya node.
free(head);
head
= NULL;
tail
= NULL;
2.
Jika node dihapus dari depan:
head = head->next;
free(head->prev);
head->prev = NULL;
3.
Jika node dihapus dari belakang:
tail = tail->prev;
free(tail->next);
tail->next = NULL;
4.
Jika node dihapus dari posisi tertentu
struct tnode *curr = head;
while ( curr->next->value != x ) curr =
curr->next;
struct tnode *a
= curr;
struct tnode *del = curr->next;
struct tnode *b = curr->next->next;
struct tnode *b = curr->next->next;
a->next = b;
b->prev = a;
free(del);
Circular
Doubly Linked List
Sama
seperti circular single linked list hanya saya setiap node memiliki 2 penunjuk
(pointer).
Header Linked
List
Header
linked list adalah type khusus linked list karena memiliki node header di awal linked
list.
Jadi
dalam Header linked list START tidak akan menunjuk ke node pertama dari linked
list tapi START akan berisi alamat node header.
Vick Koesoemo Santoso
2101637541
Komentar
Posting Komentar