Anda di halaman 1dari 9

Program Studi Teknik Elektro ITB

Nama Kuliah (Kode) : Praktikum Pemecahan Masalah dengan C (EL2208)


Tahun / Semester : 2021-2022 / Genap
Modul : 6 – Linked List
Nama Asisten / NIM : Reynaldo Averill Adji Putra / 13219071
Nama Praktikan / NIM : Muhammad Mikhail Ghrilman / 13220021

Tugas Pendahuluan
1. Linked list adalah urutan struktur data, yang dihubungkan bersama melalui links. Linked
list adalah urutan link yang berisi item. Setiap link berisi koneksi ke link lain. Linked list
adalah struktur data kedua yang paling banyak digunakan setelah array. Linked list
adalah kumpulan node yang dialokasikan secara dinamis, diatur sedemikian rupa
sehingga setiap node berisi satu nilai dan satu pointer. Pointer selalu menunjuk ke
anggota daftar berikutnya. Jika penunjuknya NULL, maka itu adalah simpul terakhir
dalam daftar. Perbedaan array dan linked list adalah masing-masing elemen array hanya
berisi data saja, sedangkan linked list berisi data dan pointer atau node untuk
menghubungkan ke data berikutnya.
2.

Jenis Perbedaan Cara Kerja


Linked
List
Singly Ini adalah jenis
Linked paling
List sederhana dari
linked list di
mana setiap
node berisi
beberapa data
dan pointer ke
node
berikutnya dari
tipe data yang
sama. Node
berisi pointer
ke node
berikutnya
berarti node
tersebut
menyimpan
alamat node
berikutnya
secara
berurutan.
Sebuah singly
linked list
memungkinkan
Praktikum Pemecahan Masalah dengan C (EL2208) | Tahun 2021-2022 | Semester Genap

traversal data
hanya dalam
satu cara

Doubly Doubly linked


Linked list adalah jenis
List linked list yang
lebih kompleks
yang berisi
pointer ke
node
berikutnya
serta
sebelumnya
secara
berurutan,
Oleh karena
itu, ini berisi
tiga bagian
yaitu data,
pointer ke
node
berikutnya ,
dan pointer ke
node
sebelumnya.
Ini akan
memungkinkan
kita untuk
melintasi list ke
arah belakang
juga

Circular Circular linked


Linked list adalah
List node terakhir
berisi pointer
ke node
pertama dari
list. Saat
melintasi
circular linked
list , kita dapat
mulai dari node
mana pun dan
melintasi list ke
segala arah
maju dan
mundur sampai
Halaman 2 dari 9
kita mencapai
node yang
sama dengan
yang kita mulai.
Dengan
demikian,
circular linked
list tidak
memiliki awal
dan akhir.

3.

Inserting node :

// A complete working C program to demonstrate all insertion methods on Linked


List
#include <stdio.h>
#include <stdlib.h>

// A linked list node


struct Node
{
int data;
struct Node *next;
};

/* Given a reference (pointer to pointer) to the head of a list and


an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

/* 2. put in the data */


new_node->data = new_data;

/* 3. Make next of new node as head */


new_node->next = (*head_ref);

/* 4. move the head to point to the new node */


(*head_ref) = new_node;
}

/* Given a node prev_node, insert a new node after the given


prev_node */
void insertAfter(struct Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}

/* 2. allocate new node */


struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
Praktikum Pemecahan Masalah dengan C (EL2208) | Tahun 2021-2022 | Semester Genap

/* 3. put in the data */


new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */


new_node->next = prev_node->next;

/* 5. move the next of prev_node as new_node */


prev_node->next = new_node;
}

/* Given a reference (pointer to pointer) to the head


of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

struct Node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */


new_node->data = new_data;

/* 3. This new node is going to be the last node, so make next of


it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */


while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */


last->next = new_node;
return;
}

// This function prints contents of linked list starting from head


void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}

/* Driver program to test above functions*/


int main()
{
/* Start with the empty list */
struct Node* head = NULL;

// Insert 6. So linked list becomes 6->NULL


append(&head, 6);

// Insert 7 at the beginning. So linked list becomes 7->6->NULL

Halaman 4 dari 9
push(&head, 7);

// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL


push(&head, 1);

// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL


append(&head, 4);

// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL


insertAfter(head->next, 8);

printf("\n Created Linked list is: ");


printList(head);

return 0;
}

Deleting node :

// A complete working C program to demonstrate deletion in linked list


#include <stdio.h>
#include <stdlib.h>

// A linked list node


struct Node {
int data;
struct Node* next;
};

/* Given a reference (pointer to pointer) to the head of a


list and an int, inserts a new node on the front of the
list. */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

/* Given a reference (pointer to pointer) to the head of a


list and a key, deletes the first occurrence of key in
linked list */
void deleteNode(struct Node** head_ref, int key)
{
// Store head node
struct Node *temp = *head_ref, *prev;

// If head node itself holds the key to be deleted


if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}

// Search for the key to be deleted, keep track of the


// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
Praktikum Pemecahan Masalah dengan C (EL2208) | Tahun 2021-2022 | Semester Genap

// If key was not present in linked list


if (temp == NULL)
return;

// Unlink the node from linked list


prev->next = temp->next;

free(temp); // Free memory


}

// This function prints contents of linked list starting


// from the given node
void printList(struct Node* node)
{
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}

// Driver code
int main()
{
/* Start with the empty list */
struct Node* head = NULL;

push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);

puts("Created Linked List: ");


printList(head);
deleteNode(&head, 1);
puts("\nLinked List after Deletion of 1: ");
printList(head);
return 0;
}

Soal Pemrograman :

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int nim;
int nilai;
struct Node* next;
} Node;

void add(Node **head, int nim, int nilai) {


Node *pNew = (Node *) malloc(sizeof(Node));
pNew->nim = nim;
pNew->nilai = nilai;
pNew->next = NULL;

if (pNew != NULL) {
if (*head == NULL) {
*head = pNew;

} else {

Halaman 6 dari 9
Node *p = *head;

while (p->next != NULL) {


p = p->next;
}

p->next = pNew;
}
}
}

void print(Node *head) {


Node *p = head;

while (p != NULL) {
printf("NIM: %d, Nilai: %d\n", p->nim, p->nilai);
p = p->next;
}
}

void searchMax(Node *head) {


if (head != NULL) {
Node *p = head;
Node *pMax = p;

while (p != NULL) {
if (p->nilai > pMax->nilai) {
pMax = p;
}

p = p->next;
}

printf("Nilai tertinggi:\n");
printf("NIM: %d, Nilai: %d\n", pMax->nim, pMax->nilai);
}
}

void searchMin(Node *head) {


if (head != NULL) {
Node *p = head;
Node *pMin = p;

while (p != NULL) {
if (p->nilai < pMin->nilai) {
pMin = p;
}

p = p->next;
}

printf("Nilai terendah:\n");
printf("NIM: %d, Nilai: %d\n", pMin->nim, pMin->nilai);
}
}

int main() {
Node *head = NULL;

add(&head, 10018001, 90);


add(&head, 10018002, 80);
add(&head, 10018003, 68);
add(&head, 10018004, 89);
Praktikum Pemecahan Masalah dengan C (EL2208) | Tahun 2021-2022 | Semester Genap
add(&head, 10018005, 92);
add(&head, 10018006, 80);

print(head);
searchMax(head);
searchMin(head);
free(head);

return 0;
}

Referensi:
• https://www.tutorialspoint.com/data_structures_algorithms/linked_list_program_in_c.ht
m#:~:text=A%20linked%20list%20is%20a,used%20data%20structure%20after%20array.
• https://www.geeksforgeeks.org/types-of-linked-list/
• https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/?ref=lbp
• https://www.geeksforgeeks.org/linked-list-set-3-deleting-node/?ref=lbp

Halaman 8 dari 9

Anda mungkin juga menyukai