Linked List--Menu

Well, here is example of Linked List for Student record...

#include <conio.h>
#include <iostream.h>
#include <malloc.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>

struct List{
    int NIM;
    char Nama[20];
    float IPK;
    List *Next;
    };

List *Head, *Baru, *PNow, *PAkhir, *HNow, *HBfr;
int Pilih;

int main()
{
 Head = new(List);
 Head -> Next = NULL;

 do
 {
  //clrscr();
  cout<<"\n*************************\n";
  cout<<"*  M E N U   U T A M A           *\n";
  cout<<"* 1. Add Data (di akhir)         *\n";
  cout<<"* 2. Add Data (di awal)          *\n";      
  cout<<"* 3. View Data                   *\n";
  cout<<"* 4. Search Data                 *\n";
  cout<<"* 5. Delete Data                 *\n";
  cout<<"* 6. Min & Max Value             *\n";
  cout<<"* 7. Exit                        *\n";
  cout<<"**********************************\n";
  cout<<"Pilih Menu [1/2/3/4/5/6/7] : ";cin>>Pilih;

  switch(Pilih)
  {
   case 1 : // Add Data (di akhir)
           {
            char Lagi ='Y';

            do
            {
             Baru = new (List);
             cout<<"Masukan NIM              : ";cin>>Baru -> NIM;
             cout<<"Masukan Nama             : ";cin>>Baru -> Nama;
             cout<<"Masukan IPK             : ";cin>>Baru -> IPK;
             PAkhir = NULL;
             PNow = Head -> Next;
             if(PNow == NULL)
             {
               //List masih kosong
               Baru -> Next = PNow;
               Head -> Next = Baru;
             }
             else
             {
               //List sudah berisi data
               //Cari posisi elemen terakhir
               while ((PNow != NULL) && (PNow -> NIM != Baru -> NIM))
               {
                PAkhir = PNow;
                PNow = PNow -> Next;
               }
               //Tambahkan data di akhir list
               if (PAkhir == NULL)
               {
                Baru -> Next = PNow;
                Head -> Next = Baru;
               }
               else
               {
                Baru -> Next = PNow;
                PAkhir -> Next = Baru;
               }
             }
             cout<<"Input data Lagi [Y/T] : ";cin>>Lagi;
            }while (Lagi == 'Y' || Lagi == 'y');
            getch();break;
           }
   case 2 : // Add Data (di awal)
            {
           }         
   case 3 : // View Data
           {
            if (Head->Next == NULL)
             cout<<"List Kosong\n";
            else
            {
             PNow = Head -> Next;
             while (PNow != NULL)
             {
              cout<<"NIM               : "<<PNow -> NIM<<endl;
              cout<<"Nama              : "<<PNow -> Nama<<endl;
              cout<<"IPK         : "<<PNow -> IPK<<endl;
              PNow = PNow -> Next;
             }
            }
            getch();break;
           }
   case 4 : // Search Data
          {
            int count = 1;
            int NIM_Cari;

            cout<<"Masukan NIM yang dicari : ";cin>>NIM_Cari;
            PNow = Head -> Next;
            if (PNow == NULL) {
             cout<<"Data Tidak Ditemukan karena List Kosong";
            }    
            else
            {
             while ((PNow != NULL) && (PNow -> NIM != NIM_Cari)) {
              PNow = PNow -> Next;
              count++;
             }
             if (PNow == NULL || PNow->NIM != NIM_Cari) {
              cout<<"Data Tidak Ditemukan.";
             }   
             else {
              cout<<"NIM           : "<<PNow -> NIM<<endl;
              cout<<"Nama              : "<<PNow -> Nama<<endl;
              cout<<"IPK             : "<<PNow -> IPK<<endl;
              cout<<NIM_Cari<<" ditemukan di posisi ke-"<<count<<endl;
             }
            }   
            getch();break;
           }
   case 5 : // Delete Data
          {
            int NIM_Hapus;
            char Jawab;
            int found = 0;

            cout<<"Masukan NIM yang akan dihapus : ";cin>>NIM_Hapus;
            HNow = Head -> Next;
            HBfr = NULL;
            if (HNow == NULL)
             cout<<"Data Tidak bisa dihapus karena List Kosong";
            else
            {
             while (HNow != NULL && !found)
             {
                 if (HNow->NIM == NIM_Hapus)
                     found = 1;
                 else {   
                     HBfr = HNow;
                     HNow = HNow -> Next;
                 }   
             }
             if (!found)
              cout<<"Data Tidak Ditemukan.";
             else
              {
               cout<<"Yakin Nilai : "<<NIM_Hapus<<" Akan dihapus [Y/T] :";cin>>Jawab;
               if (Jawab == 'T' || Jawab == 't')
                cout<<"Proses Penghapusan Nilai "<<NIM_Hapus<<" dibatalkan"<<endl;
               else
               {
                if (Jawab == 'Y' || Jawab == 'y')
                {
                 if (HBfr == NULL)
                  Head -> Next = HNow -> Next;
                 else
                  HBfr -> Next = HNow -> Next;
                }
               }
             }
            }
            getch();break;
          
           }
   case 6 : // Min & Max Value
          {
           }
   case 7 : cout<<"End of Application!!!";getch();break;
   default : cout<<"Salah pilih !!!";getch();break;
  }

 }while (Pilih >= 1 && Pilih <= 5);
 return 0;
}


remember to always write the code...,do not copy and paste,"practice makes perfect..".

Individual tasks (SCL-4) :
1. Create a Pseducode algorithm
2. The task is gathered on march 15, 2016 (Next Tuesday) 

Popular posts from this blog

Menghitung NDVI, LST dan FCD untuk penelitian berbasis remote sensing

Introduction to Use Case Diagram - Case study: Facebook

Sequential Search