fbpx

Struktur Data : Contoh Program Double Linked List dalam Bahasa C

๐Ÿ“‹ Daftar Isi

Buatlah program daftar nilai struktur data kelas Anda dengan menggunakan double linked list.


Source Code

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

struct node {
    int nilai;
    char nama[20];
    char nim[20];
    struct node *next;
    struct node *prev;
};

typedef struct node *mynode;

mynode create_node(int nilai, char nama[20], char nim[20]){
    mynode new_node;
    new_node=(mynode)malloc(sizeof(struct node));
    new_node->nilai=nilai;
    strcpy(new_node->nama,nama);
    strcpy(new_node->nim,nim);
    new_node->next=NULL;
    new_node->prev=NULL;
    return(new_node);
}

mynode insert_node(mynode head, int nilai,char nama[20], char nim[20]){
    mynode new_node=create_node(nilai,nama,nim);
    //head masih null
    if (head==NULL){
        head=new_node;
        return(head);
    }
    //nyari posisi
    mynode cursor=head;
    mynode precursor;

    //tambah head
    if (cursor->nilai<=nilai){
        new_node->next=head;
        head->prev=new_node;
        head=new_node;
    } else {
        while(cursor->next!=NULL){
            if (cursor->nilai>nilai){
                precursor=cursor;
                cursor=cursor->next;
            } else break;
        }

        if(cursor->nilai>nilai){ // tambah tail
            cursor->next=new_node;
            new_node->prev=cursor;
        }else if (cursor->nilai<=nilai) {//tambah tengah
            precursor->next=new_node;
            new_node->next=cursor;
            cursor->prev=new_node;
            new_node->prev=precursor;

        }

    }
    return(head);
}


mynode remove_node (mynode head, int nilai){
  mynode cursor = head;

  if (cursor == NULL){
      printf ("List masih kosong");
      return (head);
    }

  if (cursor->nilai == nilai){
      if (head->next == NULL){
        head = NULL;
        return (head);
	}
      head = head->next;
      cursor->next = NULL;
      head->prev = NULL;
      free (cursor);
    }
  else{
      while (cursor->nilai > nilai){
        if (cursor->next != NULL)
            cursor = cursor->next;
        else
            break;
	}

      if (cursor->nilai == nilai){
	  //node yang dihapus adalah tail
	  if (cursor->next == NULL){
	      mynode tail = cursor->prev;
	      tail->next = NULL;
	      cursor->prev = NULL;
	      free (cursor);
	    }
	  //node yang dihapus ada diantara node
	  else{
	      cursor->prev->next = cursor->next;
	      cursor->next->prev = cursor->prev;
	      cursor->prev = NULL;
	      cursor->next = NULL;
	      free (cursor);
	    }
	}
    }
  return (head);
}

void tampil_node(mynode head){
    mynode n=head;
    printf("-------------------------------------------\n");
    printf("|      Nama          |     NIM    | Tugas |\n");
    printf("-------------------------------------------\n");
    while(n!=NULL){
        printf("| %-18s | %-10s |  %-4d |\n",n->nama,n->nim,n->nilai);
        printf("-------------------------------------------\n");
        n=n->next;

    }
}

void menu(){
    printf("\n Silahkan Pilih Menu:\n");
    printf("1. Tambah Data Nilai Mahasiswa\n");
    printf("2. Hapus Data Mahasiswa\n");
    printf("3. Tampilkan Node\n");
    printf("4. Keluar\n\n");
}

int main(){
    int pilih,nilai;
    char nama[20];
    char nim[20];
    mynode head=NULL;

    do {
        menu();
        do{
            printf("Pilihan Anda        : ");
            scanf("%d", &pilih);
        }while((pilih<1)||(pilih>4));

        switch (pilih){
            case 1:
                printf("Nama Mahasiswa      : ");
                scanf("%s",nama);
                printf("NIM                 : ");
                scanf("%s",nim);
                printf("Nilai Struktur Data : ");
                scanf("%d",&nilai);
                head=insert_node(head,nilai,nama,nim);
                break;
            case 2:
                printf("Nilai mahasiswa yang ingin di hapus : ");
                scanf("%d",&nilai);
                head=remove_node(head,nilai);
                break;
            case 3:
                tampil_node(head);
        }
    }while (pilih!=4);
}


Output


Materi Lengkap

Silakan baca juga beberapa artikel menarik kami tentang Double 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 !!