Understanding Linked List
DefinisiDefinition of Linked List
A Linked List adalahis struktura linear data linearstructure yangconsisting terdiriof darielements elemen-elemencalled yangnodes. disebut node. SetiapEach node memilikihas duatwo bagianmain utama:parts:
-
Data –
menyimpanstoresnilaitheelemen.value of the element. -
Pointer/
ReferensiReference –menunjukpointsketo the next nodeberikutnya(or(atauthesebelumnyapreviouspadanode in a Doubly Linked List).
PerbedaanDifferences Between Linked List danand Array
Array | Linked List | |
---|---|---|
Insert/Delete |
ManfaatBenefits of Linked List
UkuranDynamicDinamisSize-
TidakNoperluneedmenentukantoukurandefinedisizeawalinsepertiadvancepadalike an array. -
DapatCanbertambahgrowatauorberkurangshrinksesuaiaccordingkebutuhantoprogram.program needs.
-
EfisiensiEfficiencypada Operasiin Insert/Delete Operations-
MenambahkanAddingatauormenghapusremovingelemenelementstidakdoesmemerlukannotpergeseranrequire shifting data. -
KompleksitasComplexitydapatcanmenjadibe O(1)jikaif the pointersudahisdiketahui.known.
-
MemanfaatkanMoreMemoriFlexibleSecaraMemoryLebih FleksibelUsage-
NodeNodesdapatcandisimpanbedistoredlokasiinmemoriscatteredyangmemorytersebar.locations. -
BergunaUsefulpadainsistemsystemsdenganwithmemorifragmentedyang terfragmentasi.memory.
-
DukunganSupportuntukforStrukturComplex DataKompleksStructures-
MenjadiFormsdasartheuntukbasisstrukturfor other datalainstructuressepertisuch as Stack, Queue, Deque,danandGraph.Graph. -
MemungkinkanEnablespembuatancreationvarianofsepertivariants like Circular Linked Listdanand Doubly LinkedList.List.
-
Two-Way Traversal
Dua Arah(padain Doubly Linked List)
Applications of Linked Lists
-
Dynamic Memory Allocation
-
Used to keep track of free and allocated memory blocks.
-
Undo/Redo Operations in Text Editors
-
Linked lists are used to storeStores changes andnavigatenavigates through editing history.
-
-
Graph Representation
-
ImplementEfficiently implements adjacency listsefficientlyin graphdatastructures.
-
-
Implementation of Other Data Structures
-
UsedServes as a base for stacks, queues, and deques.
-
Advantages of Linked List
-
InsertingEfficient insertion anddeleting nodes is efficientdeletion sincenoshifting of elements is not required as in arrays. -
Dynamic size allows growth or shrinkage at runtime.
-
Memory utilization is more efficient; no pre-allocation
means lessreduces wasted space. -
Suitable for applications
requiringwith large or frequently changing datasets. -
Uses non-contiguous memory blocks,
making ituseful in memory-limited systems.
Disadvantages of Linked List
-
Direct access to an element is not possible; traversal
is requiredfrom thestart.start is required. -
Each node requires extra memory to store a pointer.
-
More complex to implement and manage compared to arrays.
-
Pointer mismanagement can
lead tocause bugs, memory leaks, or segmentation faults.
ContohShort KodeCode SingkatExample
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void pushFront(Node*& head, int value) {
Node* newNode = new Node(); // Buat node baru
newNode->data = value; // Isi data
newNode->next = head; // Hubungkan ke head lama
head = newNode; // Jadikan node baru sebagai head
}
void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "NULL\n";
}
int main() {
Node* head = nullptr; // List kosong
pushFront(head, 10);
pushFront(head, 20);
pushFront(head, 30);
printList(head); // Output: 30 -> 20 -> 10 -> NULL
return 0;
}
PenjelasanCode KodeExplanation::
-
Node
adalahisstrukturthedasarbasic structure of a Linked List. -
pushFront
menambahkanadds a new nodebaruatditheawalbeginning of the list. -
printList
menelusuritraverses the listdanandmenampilkandisplayssemuaallelemen.elements.