Linked List adalah struktur data yang berbeda dengan struktur data array. Linked list dapat
dibayangkan seperti rantai yang bersambung satu sama lainnya. Tiap-tiap rantai (node)
terhubung dengan pointer.
Gambar berikut memperlihatkan sebuah Linked List.
Sebelum kita membahas lebih jauh tentang Linked List, alangkah baiknya bila kita mengulang
sedikit tentang struct dan typedef. Struct atau structure dalam bahasa pemograman C/C++
digunakan untuk mendefinisikan tipe data yang memiliki anggota (member) bertipe tertentu.
Berikut contoh dan cara mendeklarasi sebuah struct:
struct tgl {
int hari;
int bulan;
int tahun;
}
struct tgl Tanggal;
Contoh di atas memperlihatkan bagaimana mendeklarasi sebuah struct dengan nama struct tgl
yang memiliki tiga member yaitu hari, bulan dan tahun yang bertipe int (integer). Kemudian,
sebuah variabel Tanggal dideklarasikan bertipe struct tgl. Untuk mempersingkat dan
menyederhanakan pendeklarasian sebuah struct, kata cadang typedef biasa digunakan.
Sesuai namanya, typedef digunakan untuk mendefinisikan sebuah tipe data menjadi suatu alias
tertentu. Perhatikan contoh berikut:
Dengan cara yang sama, pendeklarasian struct tgl di atas dapat disederhanakan menggunakan
kata cadang typedef menjadi:
typedef struct tgl {
int hari;
int bulan;
int tahun;
} Date;
Date Tanggal;
#include "linkedlist.h"
#include <stdlib.h>
#include <stdio.h>
void initList(List * lptr) {
lptr->head = NULL;
lptr->size = 0;
}
int addList(List * lptr, int data) {
NodePtr new;
new = malloc(sizeof(Node));
if(new == NULL) {
printf("Error dalam membuat alokasi memori\n");
return 0;
}
new->data = data;
new->next = lptr->head;
lptr->head = new;
lptr->size++;
return 1;
}
void displayList(List * lptr) {
NodePtr current = lptr->head;
printf("\n");
while(current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("null\n");
}
void freeList(List * lptr) {
NodePtr next=lptr->head;
NodePtr current=next;
while(current != NULL) {
next = current->next;
free(current);
current = next;
}
}
/* main.c */
#include <stdlib.h>
#include <stdio.h>
#include "linkedlist.h"
int main(void) {
int i, n, data;
List LL;
initList(&LL);
Daftar bertaut tunggal dengan tiap-tiap node yang terdiri atas dua elemen, data integer, dan elemen rujukan ke
node berikutnya
Daftar bertaut ganda dengan tiap-tiap node yang terdiri atas tiga elemen, data integer, dan dua elemen rujukan ke
node sebelum serta berikutnya
Sumber pustaka
Juan, Angel (2006) (pdf), Ch20 Data Structures; ID06 - PROGRAMMING with JAVA (slide
part of the book Big Java, by CayS. Horstmann), hlm. 3
"Definition of a linked list". National Institute of Standards and Technology. 16 Agustus
2004. Diakses pada 14 Desember 2004.
Antonakos, James L. (1999). Practical Data Structures Using C/C++. Prentice-Hall.
hlm. 165190. ISBN 0-13-280843-9.
Collins, William J. (2005) [2002]. Data Structures and the Java Collections Framework.
New York: McGraw Hill. hlm. 239303. ISBN 0-07-282379-8.
Cormen, Thomas H. (2003). Introduction to Algorithms. MIT Press. hlm. 205213 & 501
505. ISBN 0-262-03293-7.
Cormen, Thomas H. (2001). "10.2: Linked lists". Introduction to Algorithms (edisi ke-2md).
MIT Press. hlm. 204209. ISBN 0-262-03293-7.
Green, Bert F. Jr. (1961). "Computer Languages for Symbol Manipulation". IRE
Transactions on Human Factors in Electronics (2): 38.
McCarthy, John (1960). "Recursive Functions of Symbolic Expressions and Their
Computation by Machine, Part I". Communications of the ACM.
Knuth, Donald (1997). "2.2.3-2.2.5". Fundamental Algorithms (edisi ke-3rd). AddisonWesley. hlm. 254298. ISBN 0-201-89683-4.
Newell, Allen (1957). "Programming the Logic Theory Machine". Proceedings of the
Western Joint Computer Conference: 230240.
Parlante, Nick (2001). "Linked list basics". Stanford University. Diakses pada 21 September
2009.
Sedgewick, Robert (1998). Algorithms in C. Addison Wesley. hlm. 90109. ISBN 0-20131452-5.
Shaffer, Clifford A. (1998). A Practical Introduction to Data Structures and Algorithm
Analysis. New Jersey: Prentice Hall. hlm. 77102. ISBN 0-13-660911-2.
Wilkes, Maurice Vincent (1964). "An Experiment with a Self-compiling Compiler for a
Simple List-Processing Language". Annual Review in Automatic Programming (Pergamon Press)
4 (1).
Wilkes, Maurice Vincent (1964). "Lists and Why They are Useful". Proceeds of the ACM
National Conference, Philadelphia 1964 (ACM) (P-64): F11.
Shanmugasundaram, Kulesh (2005-04-04). "Linux Kernel Linked List Explained". Diakses
pada 21 September 2009.