Linked List Entin Martiana Malloc Memory Allocation malloc
- Slides: 29
Linked List Entin Martiana
Malloc • Memory Allocation (malloc) adalah sebuah fungsi fasilitas untuk memesan tempat secara berurutan untuk tipe data dinamis(pointer) • Nilai balik dari memory allocation adalah void *
Problem • Bagaimana jika kita pesan 50000 alamat berurutan? – Akan selalu gagal. – Untuk itu kita lakukan memory allocation untuk setiap satu data. • Bagaimana agar data pertama tetap berhubungan dengan data kedua? – Kita gunakan pointer untuk menghubungkan
The Linked List data structure array Array [0] [1] [2] A B C node linked Linked list A B C Linked lists are unbounded (maximum number of items limited only by memory)
Array vs Linked List Array : int A[3] Linked List : struct list *A; A(data 3) A[2] A[1] A(data 2) A[0] A(data 1) Linked lists are unbounded (maximum number of items limited only by memory)
Deklarasi struct simpul { char nama[25]; int nrp; struct simpul *next; }; struct simpul *ujung; simpul data pointer yg menunjuk simpul lain nama nrp next
Membangun Linked List Apa yang harus dilakukan? 1. Deklarasi 2. Memory allocation 3. Mengisi data 4. Menyiapkan untuk dihubungkan dengan data baru berikutnya
Bagaimana? 1. 2. 3. 4. 5. struct simpul *ujung; ujung=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama : "); scanf("%s", &ujung->nama); printf("NRP : "); scanf("%d", &ujung->nrp); if(j==0) { ujung->next=NULL; tampung=ujung; nama 1 } tampung nrp 1 next NULL
Selanjutnya ujung 1. 2. 3. 4. ujung=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama : "); scanf("%s", &ujung->nama); printf("NRP : "); scanf("%d", &ujung->nrp); if(j<>0) { nama 2 nrp 2 next ujung->next=tampung; tampung=ujung; } ujung tampung nama 2 nama 1 nrp 2 nrp 1 next NULL
Selanjutnya ujung 1. 2. 3. 4. ujung=(struct dtnilai*)malloc(sizeof(struct dtnilai)); printf("Nama : "); scanf("%s", &ujung->nama); printf("NRP : "); scanf("%d", &ujung->nrp); if(j<>0) { nama 3 nrp 3 next ujung->next=tampung; tampung=ujung; } ujung tampung nama 3 nama 2 nama 1 nrp 3 nrp 2 nrp 1 next NULL
Sampai iterasi keempat ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next tampung NULL
Membaca (Menampilkan) ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next tampil = ujung; while (tampil<>NULL) // fungsi menampilkan tampil = tampil -> next; tampil NULL tampil
Mencari simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari = ujung; while (cari->nama!=nama 2) { cari = cari->next; } cari NULL
Menyisipkan sebagai simpul terakhir ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari = ujung while (cari->next !=NULL) cari = cari->next; cari->next=baru; cari NULL cari baru namax nrpx next NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next hapus = ujung; NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next hapus while (hapus->nama != nama 2) { sbl = hapus; hapus=hapus->next; } NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next sbl hapus while (hapus->nama != nama 2) { sbl=hapus; hapus=hapus->next; } NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next sbl while (hapus->nama != nama 2) { sbl=hapus; hapus=hapus->next; } hapus NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next sbl->next=hapus->next; hapus NULL
Menghapus simpul ttt. ujung nama 4 nama 3 nama 1 nrp 4 nrp 3 nrp 1 next sbl free(hapus); NULL
Menyisipkan setelah simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari baru cari = ujung namax nrpx next NULL
Menyisipkan setelah simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari baru while (cari->nama!=nama 3) cari = cari->next; baru->next = cari->next; namax nrpx next NULL
Menyisipkan setelah simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari baru namax cari->next = baru; nrpx next NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next cari baru cari = ujung namax nrpx next NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next stl cari baru cari = ujung; while (cari->nama!=nama 1) stl=cari; cari=cari->next; namax nrpx next NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next stl cari baru cari = ujung; while (cari->nama!=nama 1) stl=cari; cari=cari->next; namax nrpx next NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next stl baru cari = ujung; while (cari->nama!=nama 1) stl=cari; cari=cari->next; namax nrpx next cari NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next stl cari baru->next = cari; namax nrpx next NULL
Menyisipkan sebelum simpul ttt. ujung nama 4 nama 3 nama 2 nama 1 nrp 4 nrp 3 nrp 2 nrp 1 next stl cari baru stl->next = baru; namax nrpx next NULL
- Singly vs doubly linked list
- Singly linked list vs doubly linked list
- List adalah
- Contiguous allocation vs linked allocation
- Linked list malloc
- Malloc lab segregated free list
- Buddy memory allocation
- Pseudocode with functions
- Example of dynamic memory allocation
- Knuth's boundary tags
- Disadvantages of dynamic array
- Dynamic memory allocation
- Example of dynamic memory allocation
- Dynamic memory allocation in data structure
- Single partition allocation
- Dynamic memory allocation in data structure
- Dynamic c programming
- Paged segmentation
- Worst fit memory allocation
- In contiguous memory allocation has no cure
- Paged memory management
- What are two goals of multitasking memory allocation
- Contiguous memory management in os
- Non contiguous memory allocation
- Paging in non contiguous memory allocation
- Non contiguous memory allocation
- Dynamic memory allocation in java
- Bitmap and linked list in os
- Memory allocation
- Calloc free