Jumat, 29 Januari 2016

Struktur Data - Linked List

Linked List merupakan Program yang dapat menampilkan menu aneka macam operasi insert dan delete pada linked list, dan menjalankan operasi sesuai menu yang dipilih. Gunakan fungsi tampil isi linked list yang telah dibuat sebelumnya untuk mengecek kebenaran operasi yang dilakukan.
Menu akan terus ditampilkan dan diproses selama pengguna belum memilih menu untuk keluar program. Masukan berupa NIM dan Nama (char).
             Tampilan program seperti ini :

Dengan source code sebagai berikut :


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

 typedef struct{
            char NIM[20];
            char Nama[30];
}mhs;

typedef struct Node{
        mhs data;
        struct Node *next;
};
 typedef struct Node Simpul;


 Simpul *P, *HEAD, *TAIL;

 void inisialisasi(){
            HEAD==NULL;
 }

 //buat simpul
 void buatSimpul (mhs x){
            P = (Simpul *) malloc(sizeof(Simpul));
            if (P != NULL){
            strcpy(P->data.NIM,x.NIM);
    strcpy(P->data.Nama,x.Nama);
            }
            else
            printf("PEMBUATAN SIMPUL GAGAL\n");
 }

 //buat simpul awal
 void awal()
{
     if (HEAD == NULL)
     {
               HEAD = P;
               TAIL = P;
               P->next = NULL;
     }
     else
         printf("LINKED LIST SUDAH ADA\n");
}

 //insert belakang
void insertBelakang()
{
     TAIL->next = P;
     TAIL = P;
     P->next = NULL;
}

//insert depan
void insertDepan()
{
     if (HEAD != NULL)
     {
               P->next = HEAD;
               HEAD = P;
     }
     else
         printf("Linked list belum ada");
}

//insert setelah x
void insertSetelah(mhs x)
{
     Simpul *Q;
     Q = HEAD;
     do
        Q = Q->next;
     while (strcmp(P-> data.NIM,x.NIM)==1);
     P->next = Q->next;
     Q->next = P;
}

//insert sebelum x
void insertSebelum(mhs x)
{
     Simpul *Q, *sebelumQ;
     if (strcmp(HEAD->data.NIM,x.NIM)==0)
        insertDepan();
     else
     {
         Q = HEAD;
         do{
                sebelumQ = Q;
                Q = Q->next;}
         while (strcmp(Q->data.NIM,x.NIM)==1);
         P->next = Q;
         sebelumQ->next = P;
     }
}

//delete depan
void deleteDepan()
{
     Simpul *Q;
     Q = HEAD;
     HEAD = Q->next;
     free(Q);
}

//delete belakang
void deleteBelakang()
{
     Simpul *Q;
     Q = HEAD;
     while(Q->next != TAIL)
          Q = Q->next;
     free(TAIL);
     TAIL = Q;
     Q->next = NULL;
}
 //delete simpul x
 void deleteSimpul(mhs x)
{
     Simpul *Q, *sebelumQ;
     if (HEAD == NULL)
        printf("LINKED LIST KOSONG\n");
     else if (strcmp(HEAD->data.NIM,x.NIM)==0){
          deleteDepan();
     } else
     {
        Q = HEAD;
        do {
           sebelumQ = Q;
           Q = Q->next;
        } while (strcmp(Q->data.NIM,x.NIM)==1);
        sebelumQ->next = Q->next;
        free(Q);
     }
}

//menampilkan linked list/simpul
/*void tampil()
{
     printf("\n");
     Simpul *baca;
     baca = HEAD;
     do{
          printf("%s \n", baca->data);
          baca = baca->next;
     } while (baca != NULL);
}
*/

//menampilkan linked list/simpul
            void tampil (){
                        printf("\nISI LINKED LIST :\n\n");
                        Simpul *Q;
                        Q = HEAD;
                        do{
                                    printf("NIM: %s \n", Q->data.NIM);
          printf("NAMA: %s \n", Q->data.Nama);
                                    Q=Q->next;
                        } while (Q!=NULL);
            }

           
main(){
            int pil;
            mhs X;
            inisialisasi();
           
            printf("PROGRAM LINKED LIST\n");
            printf("-----------------------\n");
            printf("1. BUAT SIMPUL AWAL\n");
            printf("2. INSERT DEPAN\n");
            printf("3. INSERT SETELAH X\n");
            printf("4. INSERT SEBELUM X\n");
            printf("5. INSERT BELAKANG\n");
            printf("6. DELETE DEPAN\n");
            printf("7. DELETE SIMPUL X\n");
            printf("8. DELETE BELAKANG\n");
            printf("9. TAMPIL DATA\n");
           
            do{
            printf("PILIH MENU :");
            scanf("%i", &pil);
                                    switch(pil){
            case 1 :
                        printf("\n");
                        printf("MASUKKAN NIM: ");
                                                scanf("%s",&X.NIM);
                                                printf("MASUKKAN NAMA:");
                                                scanf("%s",&X.Nama);
                                                printf("\n----------------\n");
                                                buatSimpul(X);
                                                awal(); break;
                                               
                                    case 2 :
                                                printf("\n");
                                                printf("MASUKKAN NIM\t: ");
                                                scanf("%s",&X.NIM);
                                                printf("MASUKKAN NAMA\t:");
                                                scanf("%s",&X.Nama);
                                                printf("\n----------------\n");
                                                buatSimpul(X);
                                                insertBelakang(); break;
                                               
                                    case 3 :
                                                printf("\n");
                        printf("MASUKKAN NIM\t: ");
                                                scanf("%s",&X.NIM);
                                                printf("MASUKKAN NAMA\t:");
                                                scanf("%s",&X.Nama);
                                                printf("\n----------------\n");
                                                buatSimpul(X);
                                                insertDepan(); break;
                                               
           case 4 :
                                                printf("\n");
                        printf("MASUKKAN NIM: ");
                                                scanf("%s",&X.NIM);
                                                printf("MASUKKAN NAMA:");
                                                scanf("%s",&X.Nama);
                                                buatSimpul(X);          
                                                printf("MASUKKAN DATA SETELAH ");
                                                printf("\nMASUKKAN NIM: ");
                                                scanf("%s",&X.NIM);
                                                printf("\n----------------\n");                                          
                                                insertSetelah(X); break;
                                               
            case 5 :
                        printf("\n");
                        printf("MASUKKAN NIM: ");
                                                scanf("%s",&X.NIM);
                                                printf("\nMASUKKAN NAMA:");
                                                scanf("%s",&X.Nama);
                                                buatSimpul(X);
                                                printf("MASUKKAN DATA SEBELUM ");
                                                printf("\nMASUKKAN NIM: ");
                                                scanf("%s",&X.NIM);           
                                                printf("\n----------------\n");                              
                                                insertSebelum(X); break;
                                               
                                    case 6 :
                                                deleteDepan(); break;
                                               
                                    case 7 :
                                                deleteBelakang(); break;
                                               
                                    case 8 :
                                                printf("\n");
                        printf("\nSIMPUL YANG AKAN DIHAPUS: ");
                                                printf("\nMASUKKAN NIM:");
                                                scanf("%s",&X.NIM);
                                                printf("\n----------------\n");
                                                deleteSimpul(X); break;
                                               
                                    case 9 :
                                                tampil(); break;
                                               
                                    case 10 :
                                                printf("\n");
                printf("\n"); break;
               
        default :
            printf("\n");
            printf("MASUKKAN ANDA SALAH, SILAHKAN ULANGI LAGI"); break;
        }
            getch(); } while(pil!=10);
            return 0;
}          

0 komentar:

Posting Komentar