Contoh Program Linklist

#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

int x;
void exit()
{
gotoxy (30,40);
cout<<” Look Out “;

for (x=50; x>1; x–)
{
gotoxy (x+30,40);
cout<<” ^^Terimakasih^^ “;
delay(170);

}
clrscr();

}

class list{

struct barang{
int no;
char nama[25];
int harga;

struct barang *next;

};

private:

typedef struct barang NODE;
typedef NODE *PNODE;

public:

int menu ();
PNODE list_add();
void insert_list(PNODE  *P_head,PNODE P_new);
void delete_list(PNODE *P_head);
void find_list(PNODE P_head, int cari);
void display_list(PNODE P_head);

}data;

void main()
{
textcolor (LIGHTBLUE);
clrscr();

gotoxy (25,20);
cout<<” Welcome to My Program LINK LIST “;
delay(1700);
clrscr();
gotoxy (60,1);
cout<<” Copyright by Kel. 6 “;

list::PNODE P_head=NULL;
list::PNODE P_new;
int cari;

int pilih=-1;

while (pilih!=5)
{
pilih=data.menu();

switch(pilih)
{
case 1:{
clrscr();

P_new=data.list_add();
if(P_new==NULL)
{
cout<<” Gagal “;

}
else
{
data.insert_list(&P_head,P_new);

}
break;
}

case 2:{
clrscr();
data.delete_list(&P_head);
break;
}

case 3:{
clrscr();
cout<<” No yg dicari : “;cin>>cari;
cout<<endl;
data.find_list(P_head,cari);
break;
}

case 4:{
clrscr();
data.display_list(P_head);
break;
}
case 5:{
exit();
cout<<endl;
break;
}
default:return;

}

}

getch();

}

int list::menu()
{

int pilih;
gotoxy(30,5);cout<<” \n “;
gotoxy(30,6);cout<<” MENU LINK LIST \n “;
gotoxy(30,7);cout<<” \n “;
cout<<endl;
gotoxy(25,10);cout<<” 1. Tambah Data Barang \n “;delay(80);
gotoxy(25,11);cout<<”    \n “;delay(80);
cout<<endl;
gotoxy(25,13);cout<<” 2. Hapus Data Barang  \n “;delay(80);
gotoxy(25,14);cout<<”      \n “;delay(80);
cout<<endl;
gotoxy(25,16);cout<<” 3. Cari Data Barang   \n “;delay(80);
gotoxy(25,17);cout<<”       \n “;delay(80);
cout<<endl;
gotoxy(25,19);cout<<” 4. Tampil Data Barang \n “;delay(80);
gotoxy(25,20);cout<<”    \n “;delay(80);
cout<<endl;
gotoxy(25,22);cout<<” 5. Exit Program       \n “;delay(80);
gotoxy(25,23);cout<<”           \n “;delay(80);
cout<<endl;
gotoxy(25,25);cout<<” Enter a Value [1-5] :”;cin>>pilih;delay(80);
return (pilih);

}

list::PNODE list ::list_add()
{
PNODE P_new;
P_new=(NODE*)malloc(sizeof(NODE));
if (P_new!=NULL)
{
cout<<” Nomor : “;cin>>P_new->no;
cout<<endl;
cout<<” Nama Barang : “;cin>>P_new->nama;
cout<<endl;
cout<<” Harga : “;cin>>P_new->harga;
cout<<endl;
P_new->next=NULL;

}
cout<<” Press Enter to Continue to MENU “;
getch();
clrscr();

return (P_new);

}

void list::insert_list(PNODE *P_head,PNODE P_new)
{
if (*P_head==NULL)
{
*P_head=P_new;
return;

}
else
{
P_new->next=*P_head;
*P_head=P_new;

}
cout<<” Press Enter to Continue to MENU “;
getch();
clrscr();
return;

}
void list::delete_list(PNODE *P_head)
{
PNODE posisi;
posisi=*P_head;

if(posisi==NULL)
{
cout<<” Empty List “;
}
else
{
*P_head=(*P_head)->next;
free(posisi);

}

cout<<” Data telah terhapus “;
cout<<endl;
cout<<” Press Enter to Continue to MENU “;
getch();
clrscr();

}
void list::find_list(PNODE P_head, int cari)
{
PNODE posisi;
posisi=P_head;

if (posisi==NULL)
{
cout<<” Empty List “;
}
else
{
cout<<”  \n “;
cout<<” Data Barang  \n “;
cout<<”   \n “;
cout<<endl;
cout<<”  \n “;
cout<<” Nomor “<<“Nama \t\t”<<“Harga”<<“\n”;
cout<<”  \n “;
cout<<endl;
while (posisi!=NULL )
{

if (posisi->no==cari)
{
cout<<”  “<<posisi->no<<“\t”<<posisi->nama<<“\t\t”<<posisi->harga<<“\n”;

}

posisi=posisi->next;  // sebagai counter list
}

}
cout<<” Press Enter to Continue to MENU”;
getch();
clrscr();
}
void list::display_list(PNODE P_head)
{
PNODE posisi;
posisi=P_head;
if (posisi==NULL)
{
cout<<” Empty List “;

}
else
{
cout<<”  \n “;
cout<<” Data Barang  \n “;
cout<<”   \n “;
cout<<endl;
cout<<”  \n “;
cout<<” Nomor “<<“Nama \t\t”<<“Harga”<<“\n”;
cout<<”  \n “;
cout<<endl;
while (posisi!=NULL)
{
cout<<”  “<<posisi->no<<“\t”<<posisi->nama<<“\t\t”<<posisi->harga<<“\n”;
cout<<endl;
posisi=posisi->next;  // sebagai counter list
}

}
cout<<endl;
cout<<” Press Enter to Continue to MENU “;
cout<<endl;
getch();
clrscr();

}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s