Linked List

Lengkapi Program Linked List berikut menjadi Program yang Lengkap,

Tambahkan function :

  •  insertBelakang
  • hapusDepan
  • hapusBelakang
  • Cetak

 


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
 
typedef struct TNode *list;
 
struct TNode
{
int data;
struct TNode *next;
};
 
struct TNode *head;
 
//fungsi inisialisasi head = null (tdk menunjuk ke simpul manapun)
void init()
{
head =NULL;
}
 
//utk cek kondisi linked list, isi/kosong.
int isEmpty()
{
if(head==NULL) return 1;
else return 0;
}
 
void insertDepan(char databaru)
{
//membuat simpul baru
TNode *baru;
baru =  new TNode;
baru->data = databaru;
baru->next = NULL;
 
if(isEmpty()==1)
{    //kondisi linked list kosong
head = baru;

}
else
{    //kondisi linkedlist tidak kosong
baru->next=head;
head=baru;
}
cout<<"Data Masuk :"<<head->data;
}

//fungsi tampil
void tampil()
{
     TNode *bantu;
     bantu=head;
     if(isEmpty()==0)
     {
        while(bantu!=NULL)
        {
           cout<<bantu->data<<"-";
           bantu=bantu->next;                
        }             
     }else cout<<"data masih kosong"<<endl;
          
}
 
int main()
{
char pilih;
int data;
 
init();
while(pilih!='Q')
{
system("cls");
cout<<"=== MENU UTAMA ==="<<endl;
cout<<"1. Tambah Simpul di Depan "<<endl;
cout<<"2. Tambah Simpul di Belakang "<<endl;
cout<<"3. Hapus Simpul di Depan "<<endl;
cout<<"4. Hapus Simpul di Belakang "<<endl;
cout<<"5. Baca Isi Linked List"<<endl;
cout<<"Q : Quit"<<endl;
cout<<"Masukan Pilihan Anda=";
cin>>pilih;
 
switch (pilih)
{
case '1' : cout<<"Masukan data =";
            cin>>data;
            insertDepan(data);
            getch();
            break;

case '5' :    cout<<"Tampilan Linked List"<<endl;
              tampil();
              getch();
              break;



}
 

}
return 1;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s