fbpx

Struktur Data : Contoh Program Single Linked List dalam Bahasa C

Buatlah fungsi untuk membalik nilai dari head ke tail!

Contoh: 5->4->3->2->1 menjadi 1->2->3->4->5

Hint:   

  • Hanya nilai saja, memory address (pointer node) tetap sama
  • Buat temporary pointer node sebagai bantuan: prev, current, next dan loop dari head ke tail

Fungsi yang digunakan dalam program berikut adalah

//reverse urutan nilai
ptrnode reverse(ptrnode head)
{
    struct node *current, *prev, *next;
    current = head;
    prev = NULL;
    while (current != NULL)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
}

Source Code

Berikut ini source code lengkapnya

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

//Deklarasi pointer dengan tipe data struct node
struct node
{
    int value;
    struct node *next;
};

typedef struct node *ptrnode;

//fungsi untuk membuat simpul baru
ptrnode createNode(int nilai)
{
    ptrnode p;
    p = (ptrnode)malloc(sizeof(struct node));
    p->value = nilai;
    p->next = NULL;
    return(p);
}
//fungsi untuk menambahkan simpul di paling ujung kiri (paling awal)
ptrnode insert_head(ptrnode head, int nilai)
{
    //head = (ptrnode)malloc(sizeof(struct node));
    ptrnode new_node = createNode(nilai);
    new_node->next = head;
    head = new_node;
    return(head);
}
//fungsi untuk menambahkan simpul di paling ujung kanan (paling akhir)
ptrnode insert_tail(ptrnode head, int nilai)
{
    //iterasi mencari node terakhir
    ptrnode tail = head;
    while(tail->next != NULL)
        tail = tail->next;
    //buat simpul baru
    ptrnode new_node = createNode(nilai);
    tail->next = new_node;

    return(head);
}
//fungsi menambahkan simpul setelah simpul tertentu
ptrnode insert_after(ptrnode head, int nilai, int prev_nilai)
{
    //mencari simpul sebelumnya, mulai dari simpul pertama
    ptrnode cursor = head;
    while (cursor->value != prev_nilai)
        cursor = cursor->next;

    ptrnode new_node = createNode(nilai);
    new_node->next = cursor->next;
    cursor->next = new_node;

    return(head);
}
//fungsi menambahkan simpul sebelum simpul tertentu
ptrnode insert_before(ptrnode head, int nilai, int next_nilai)
{
    if (head->value == next_nilai)
        head = insert_head(head, nilai);
    else
    {
        ptrnode cursor, prevcursor;
        cursor = head;
        do
        {
            prevcursor = cursor;
            cursor = cursor->next;
        }
        while (cursor->value != next_nilai);

        ptrnode new_node = createNode(nilai);
        new_node->next = cursor;
        prevcursor->next = new_node;
    }
    return(head);
}
//menghapus simpul paling kiri
ptrnode remove_first(ptrnode head)
{
    if (head==NULL)
        return;

    ptrnode first = head;
    head = head->next;
    first->next = NULL;

    free(first);

    return(head);
}
//meghapus simpul paling kanan
ptrnode remove_last(ptrnode head)
{
    if (head == NULL)
        return;

    ptrnode tail = head;
    ptrnode prevtail = NULL;
    while (tail->next != NULL)
    {
        prevtail = tail;
        tail = tail->next;
    }

    prevtail->next = NULL;
    free(tail);

    return(head);
}
//menghapus node diantara dua node
ptrnode remove_middle(ptrnode head, int nilai)
{
    ptrnode cursor = head;
    while (cursor != NULL)
    {
        if(cursor->next->value == nilai)
            break; //keluar dari iterasi
        cursor = cursor->next;
    }

    if (cursor != NULL)
    {
        ptrnode tmp = cursor->next;
        cursor->next = tmp->next;
        tmp->next = NULL;
        free(tmp);
    }

    return(head);
}
//menghapus/free linked list
ptrnode dispose(ptrnode head)
{
    if(head == NULL)
        return;

    while(head != NULL)
    {
        ptrnode tmp = head;
        head = head->next;

        tmp->next = NULL;
        free(tmp);
    }

    printf("Semua node (simpul) telah dihapus\n");
    return(head);
}
//menghitung jumlah node
ptrnode count(ptrnode head)
{
    int count = 0;
    struct node *p;
    p = head;

    while (p != NULL)
    {
        p = p->next;
        count++;
    }
    printf("Jumlah node adalah : %d\n", count);
}
void tampilnilai(ptrnode head)
{
    int i=1;
    ptrnode n = head;
    printf("Daftar Nilai linked list :\n");
    while (n != NULL)
    {
        printf("Node ke %d : %d \n", i, n->value);
        n = n->next;
        i++;
    };
    printf("\n");
}
ptrnode insert_ascending(ptrnode head, int nilai)
{
    struct node *ptr;
	struct node *temp = (struct node *)malloc(sizeof(struct node));
	temp->value = nilai;
	temp->next = NULL;
    if(head == NULL)
    {
        //akan dijalankan jika linked list kosong
        head = temp;
    }
    else if (temp->value <= head->value)
    {
        //akan dijalankan jika nilai baru kurang dari head
        temp->next = head;
        head = temp;
    }
    else
    {
        //iterasi setiap elemen di liinked list
        //untuk mencari nilai node yang yang nilainya kurang dari nilai
        ptr = head;
        while(ptr->next != NULL && ptr->next->value < temp->value)
        {
            ptr = ptr->next;

        }
        //ptr menjadi node dimana nilainya lebih kecil dari nilai
        temp->next = ptr->next;
        ptr->next = temp;
    }
    return(head);
}
//reverse urutan nilai
ptrnode reverse(ptrnode head)
{
    struct node *current, *prev, *next;
    current = head;
    prev = NULL;
    while (current != NULL)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
}

main()
{
    int pilih, val, val1;
    ptrnode head = NULL;
    //head = (ptrnode)malloc(sizeof(struct node));
    //head->value = 10;
    //head->next = NULL;
    while(1)
    {
        printf("===============LINKED LIST===============\n");
        printf(" 1. Insert nilai di urutan awal\n");
        printf(" 2. Insert nilai urutan belakang\n");
        printf(" 3. Insert nilai secara ascending\n");
        printf(" 4. Insert nilai setelah nilai tertentu\n");
        printf(" 5. Insert nilai sebelum nilai tertentu\n");
        printf(" 6. Hapus nilai di urutan awal\n");
        printf(" 7. Hapus nilai di urutan akhir\n");
        printf(" 8. Hapus nilai tertentu\n");
        printf(" 9. Tampil nilai\n");
        printf("10. Tampil nilai secara reversed\n");
        printf("11. Jumlah Node\n");
        printf("12. Free Linked List\n");
        printf("13. Keluar\n");
        printf("Pilihan Anda = "); scanf("%d", &pilih);

        switch (pilih)
        {
            case 1 :
            {
                printf("Masukkan nilai = ");scanf("%d", &val);
                head = insert_head(head, val);
                system("cls");
                break;
            }
            case 2 :
            {
                printf("Masukkan nilai = ");scanf("%d", &val);
                head = insert_tail(head, val);
                system("cls");
                break;
            }
            case 3 :
            {
                printf("Masukkan nilai = ");scanf("%d", &val);
                head = insert_ascending(head, val);
                system("cls");
                break;
            }
            case 4 :
            {
                printf("Masukkan nilai = ");scanf("%d", &val);
                printf("Ingin memasukkan nilai tersebut setelah = "); scanf("%d", &val1);
                head = insert_after(head, val, val1);
                system("cls");
                break;
            }
            case 5 :
            {
                printf("Masukkan nilai = ");scanf("%d", &val);
                printf("Ingin memasukkan nilai tersebut sebelum = "); scanf("%d", &val1);
                head = insert_before(head, val, val1);
                system("cls");
                break;
            }
            case 6 :
            {
                head = remove_first(head);
                system("cls");
                break;
            }
            case 7 :
            {
                head = remove_last(head);
                system("cls");
                break;
            }
            case 8 :
            {
                printf("Masukkan nilai yang ingin di hapus = ");scanf("%d", &val);
                head = remove_middle(head, val);
                system("cls");
                break;
            }
            case 9 :
            {
                system("cls");
                tampilnilai(head);
                //system("cls");
                break;
            }
            case 10 :
            {
                system("cls");
                head = reverse(head);
                break;
            }
            case 11 :
            {
                system("cls");
                head = count(head);
                break;
            }
            case 12 :
            {
                system("cls");
                head = dispose(head);
                exit(1);
            }
            case 13 :
            {
                exit(1);
            }
            default :
            {
                printf("Pilihan tersebut belum tersedia");
                break;
            }
        }
    }

    return 0;
}

Output


Materi Lengkap

Silakan baca juga beberapa artikel menarik kami tentang Single Linked List, daftar lengkapnya adalah sebagai berikut.


Tonton juga video pilihan dari kami berikut ini

Bagikan ke teman-teman Anda

Contact Us

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
error: Content is protected !!
Up