1 / 29

Linked List

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?

luka
Download Presentation

Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List Entin Martiana

  2. 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 *

  3. 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

  4. [0] [1] [2] array Array A B C node Linked list linked A B C The Linked List data structure Linked lists are unbounded (maximum number of items limited only by memory)

  5. Array vs Linked List Array : int A[3] Linked List : struct list *A; A(data 3) A(data 2) A(data 1) A[2] A[1] A[0] Linked lists are unbounded (maximum number of items limited only by memory)

  6. Deklarasi struct simpul { char nama[25]; int nrp; struct simpul *next; }; struct simpul *ujung; simpul nama nrp data pointer yg menunjuk simpul lain next

  7. Membangun Linked List Apa yang harus dilakukan? • Deklarasi • Memory allocation • Mengisi data • Menyiapkan untuk dihubungkan dengan data baru berikutnya

  8. Bagaimana? • 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; } ujung tampung nama1 nrp1 next NULL

  9. Selanjutnya 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=tampung; tampung=ujung; } nama2 nrp2 next tampung tampung ujung nama2 nrp2 nama1 nrp1 next next NULL

  10. Selanjutnya ujung • ujung=(struct dtnilai*)malloc(sizeof(struct dtnilai)); • printf("Nama :");scanf("%s",&ujung->nama); • printf("NRP :");scanf("%d",&ujung->nrp); • if(j<>0) { ujung->next=tampung; tampung=ujung; } nama3 nrp3 next ujung tampung tampung nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next NULL

  11. Sampai iterasi keempat ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL tampung

  12. Membaca (Menampilkan) ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL tampil tampil tampil tampil tampil tampil = ujung; while (tampil<>NULL) // fungsi menampilkan tampil = tampil -> next;

  13. Mencari simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL cari cari cari cari = ujung; while (cari->nama!=nama2) { cari = cari->next; }

  14. Menyisipkan sebagai simpul terakhir ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 NULL next next next next cari cari cari cari cari = ujung while (cari->next !=NULL) cari = cari->next; cari->next=baru; baru namax nrpx next NULL

  15. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL hapus hapus = ujung;

  16. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL hapus while (hapus->nama != nama2) { sbl = hapus; hapus=hapus->next; }

  17. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL sbl hapus while (hapus->nama != nama2) { sbl=hapus; hapus=hapus->next; }

  18. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL hapus sbl while (hapus->nama != nama2) { sbl=hapus; hapus=hapus->next; }

  19. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL hapus sbl sbl->next=hapus->next;

  20. Menghapus simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama1 nrp1 next next next NULL sbl free(hapus);

  21. Menyisipkan setelah simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL cari baru cari = ujung namax nrpx next

  22. Menyisipkan setelah simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL cari baru while (cari->nama!=nama3) cari = cari->next; baru->next = cari->next; namax nrpx next

  23. Menyisipkan setelah simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL cari baru cari->next = baru; namax nrpx next

  24. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL cari baru cari = ujung namax nrpx next

  25. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL stl cari baru cari = ujung; while (cari->nama!=nama1) stl=cari; cari=cari->next; namax nrpx next

  26. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL stl cari baru cari = ujung; while (cari->nama!=nama1) stl=cari; cari=cari->next; namax nrpx next

  27. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL stl cari baru cari = ujung; while (cari->nama!=nama1) stl=cari; cari=cari->next; namax nrpx next

  28. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL stl cari baru baru->next = cari; namax nrpx next

  29. Menyisipkan sebelum simpul ttt. ujung nama4 nrp4 nama3 nrp3 nama2 nrp2 nama1 nrp1 next next next next NULL stl cari baru stl->next = baru; namax nrpx next

More Related