Kemarin kita udach belajar banyak tentang pengulangan atau yang kita sebut dengan Looping,,sekarang dengan meggunakan teknik loping kita membuat Animasi..
Langsung ajaa kita ke Source Code :
#include <iostream.h>
#include <stdio>
#include <conio.h>
#include <malloc.h>
#include <dos.h>
delay (int d);
delay (int d)
{
for (int x=0; x<d*100; x++)
{
for (int y=0; y<d*100; y++)
{}
}
}
void animasi()
{
_setcursortype(_NOCURSOR);
//kiri delay
textcolor(4);
gotoxy(29,11);cprintf(">");
delay(100);
textcolor(6);
gotoxy(30,11);cprintf(">");
delay(100); //smkin gd nile na smkn lmbat
textcolor(7);
gotoxy(31,11);cprintf(">");
//tengah-tengah
textcolor(3);
gotoxy(33,11);cprintf("Program Linked List");
//kanan delay
textcolor(4);
gotoxy(53,11);cprintf("<");
delay(100);
textcolor(6);
gotoxy(54,11);cprintf("<");
delay(100);
textcolor(7);
gotoxy(55,11);cprintf("<");
//mulai stikom bali
sleep(2);
textcolor(1);
gotoxy(46,13);cprintf("I");
delay(100);
textcolor(0);
gotoxy(46,13);cprintf(" ");
textcolor(2);
gotoxy(45,13);cprintf("L");
delay(100);
textcolor(0);
gotoxy(45,13);cprintf(" ");
textcolor(3);
gotoxy(44,13);cprintf("A");
delay(100);
textcolor(0);
gotoxy(44,13);cprintf(" ");
textcolor(4);
gotoxy(43,13);cprintf("B");
delay(100);
textcolor(0);
gotoxy(43,13);cprintf(" ");
textcolor(5);
gotoxy(42,13);cprintf("M");
delay(100);
textcolor(0);
gotoxy(42,13);cprintf(" ");
textcolor(6);
gotoxy(41,13);cprintf("O");
delay(100);
textcolor(0);
gotoxy(41,13);cprintf(" ");
textcolor(7);
gotoxy(40,13);cprintf("K");
delay(100);
textcolor(0);
gotoxy(40,13);cprintf(" ");
textcolor(8);
gotoxy(39,13);cprintf("I");
delay(100);
textcolor(0);
gotoxy(39,13);cprintf(" ");
textcolor(9);
gotoxy(38,13);cprintf("T");
delay(100);
textcolor(0);
gotoxy(38,13);cprintf(" ");
textcolor(10);
gotoxy(37,13);cprintf("S");
delay(100);
textcolor(0);
gotoxy(37,13);cprintf(" ");
//balik
gotoxy(36,13);cprintf("S");
delay(100);
textcolor(10);
gotoxy(36,13);cprintf("S");
textcolor(15);
gotoxy(37,13);cprintf("T");
delay(100);
textcolor(9);
gotoxy(37,13);cprintf("T");
textcolor(15);
gotoxy(38,13);cprintf("I");
delay(100);
textcolor(8);
gotoxy(38,13);cprintf("I");
textcolor(15);
gotoxy(39,13);cprintf("K");
delay(100);
textcolor(7);
gotoxy(39,13);cprintf("K");
textcolor(15);
gotoxy(40,13);cprintf("O");
delay(100);
textcolor(6);
gotoxy(40,13);cprintf("O");
textcolor(15);
gotoxy(41,13);cprintf("M");
delay(100);
textcolor(5);
gotoxy(41,13);cprintf("M");
textcolor(15);
gotoxy(42,13);cprintf("B");
delay(100);
textcolor(4);
gotoxy(42,13);cprintf("B");
textcolor(15);
gotoxy(43,13);cprintf("A");
delay(100);
textcolor(3);
gotoxy(43,13);cprintf("A");
textcolor(15);
gotoxy(44,13);cprintf("L");
delay(100);
textcolor(2);
gotoxy(44,13);cprintf("L");
textcolor(15);
gotoxy(45,13);cprintf("I");
delay(100);
textcolor(1);
gotoxy(45,13);cprintf("I");
textcolor(15);
sleep(5);
clrscr();
delay(100);
clrscr();
}
typedef struct Node *Pnode;
struct Node
{
int info;
Pnode next;
};
void inisialisasi(Pnode *Head,Pnode *Tail)
{
*Head = *Tail =NULL;
}
Pnode CreateNode(int Data)
{
Pnode node_baru;
node_baru = (Pnode) malloc (sizeof (Node));
node_baru->info = Data;
node_baru->next = NULL;
return (node_baru);
}
void InsertFirst(Pnode *Head,Pnode *Tail, int Data)
{
Pnode node_baru;
node_baru = CreateNode(Data);
if(*Head==NULL)
{*Head = *Tail = node_baru;}
else
{node_baru->next = *Head;
*Head = node_baru;}
}
void DeleteFirst(Pnode *Head,Pnode *Tail)
{
Pnode bantu;
if (*Head == NULL)
{gotoxy(21,8);cout<<"List Kosong";}
else if (*Head == *Tail)
{
free(*Head);
*Head=*Tail=NULL;
gotoxy(21,10);cout<<"Node Berhasil Dihapus, Saat ini List Kosong";
}
else
{
bantu = *Head;
*Head = (*Head)->next;
free(bantu);
gotoxy(21,12);cout<<"Node Awalberhasil dihapus";
}
}
void InsertLast(Pnode *Head,Pnode *Tail, int Data)
{
Pnode node_baru;
node_baru = CreateNode(Data);
if(*Head==NULL)
{*Head = *Tail = node_baru;}
else
{(*Tail)->next = node_baru;
*Tail = node_baru;}
}
void DeleteLast (Pnode *Head,Pnode *Tail)
{
Pnode bantu;
if (*Head == NULL)
{gotoxy(21,8);cout<<"List Kosong";}
else if (*Head == *Tail)
{
free(*Head);
*Head=*Tail=NULL;
gotoxy(21,10);cout<<"Node berhasil dihapus, saat in ilist kosong";
}
else
{
bantu = *Head;
while (bantu->next != *Tail)
{
bantu = bantu->next;
}
free(*Tail);
bantu->next = NULL;
*Tail = bantu;
gotoxy(21,12);cout<<"Node Akhir Berhasil Dihapus";
}
}
//void Print(Pnode Head)
//{
//Pnode bantu;
//if (Head != NULL)
//{
// bantu = Head;
//while (bantu != NULL)
//{
// cout<<bantu->info<<" ->";
// bantu = bantu->next;
// }
// }
// else
// {cout<<"List Kosong";}
//}
void InsertAfter (Pnode *Head , Pnode *Tail, int Data, int posisi)
{
Pnode node_baru,bantu;
int jml_node,x;
if(*Head==NULL)
{InsertFirst(Head,Tail,Data);}
else
{
jml_node =1;
bantu = *Head;
while(bantu->next != NULL)
{ bantu = bantu->next;
jml_node++; }
if (posisi<=1)//insert di awal
{InsertFirst (Head ,Tail ,Data);}
else if (posisi>jml_node) //insert di akhir
{InsertLast (Head, Tail, Data);}
else
{
node_baru = CreateNode (Data);
bantu = *Head ;
x=1;
while (x < (posisi-1))
{bantu = bantu->next;
x++;}
node_baru->next = bantu->next;
bantu->next = node_baru;
}
}
}
void DeleteAfter (Pnode *Head, Pnode *Tail, int posisi)
{
Pnode bantu, bantu2;
int jml_node, x;
if (*Head == NULL)
{cout<<"List Kosong";}
else
{
jml_node =1;
bantu = *Head;
//menghitung jumlah node
while (bantu->next !=NULL)
{ bantu = bantu->next;
jml_node++;}
if((posisi<1) || (posisi > jml_node))
{cout<<"posisi diluar jangkauan."<<endl;}
else if (posisi==1)
{DeleteFirst (Head, Tail);}
else if (posisi==jml_node)
{DeleteLast (Head, Tail);}
else
{
bantu = *Head;
x=1;
while (x<(posisi-1))
{ bantu = bantu->next;
x++;}
bantu2=bantu->next;
bantu->next = bantu2->next;
free (bantu2);
}
}
}
int Searching (Pnode Head, int data)
{
Pnode bantu;
int posisi=1, ketemu=0;
bantu = Head;
while ((bantu !=NULL) && (ketemu==0))
{
if (bantu->info==data)
{ketemu=1;}
else
{
bantu=bantu->next;
posisi++;
}
}
if (ketemu == 1)
return posisi;
else
return 0;
}
void main ()
{
int c,a;
char bes=196,b=219,d=179;
int pilihan,baru,data,posisi;
Pnode head, tail;
inisialisasi (&head, &tail);
do
{
animasi();
lagi:
clrscr();
for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
gotoxy(62,1+c);printf("%c",d);
gotoxy(63,1+c);printf("%c",b);
}
textcolor(10);gotoxy(21,8);cprintf("1. Insert Node di awal List\n");
textcolor(9);gotoxy(21,9);cprintf("2. Insert Node di akhir List\n");
textcolor(8);gotoxy(21,10);cprintf("3. Mengapus Node awal List\n");
textcolor(7);gotoxy(21,11);cprintf("4. Menghapus Node akhir dari List\n");
textcolor(6);gotoxy(21,12);cprintf("5. Menambahkan Data pada posisi tertentu\n");
textcolor(5);gotoxy(21,13);cprintf("6. Menghapus Data pada posisi tertentu\n");
textcolor(4);gotoxy(21,14);cprintf("7. Mencari Data dalam list\n");
// cout<<"8. Print Data dalam List"<<endl;
cout<<endl;
textcolor(3);gotoxy(21,18);cprintf("Pilihan = ");
cin>>pilihan;
clrscr();
switch(pilihan)
{
case 1:
{
for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
}
gotoxy(28,2);cout<<"Insert Node di awal List";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
cout<<endl;
gotoxy(21,10);cout<<"Data yang akan di tambahkan = ";
cin>>baru;
InsertFirst(&head,&tail,baru);
gotoxy(21,12);cout<<"Data Berhasil Ditambahkan"<<endl;
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
getch ();
break; }
case 2:
{for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
} gotoxy(28,2);cout<<"Insert Node di akhir List";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
cout<<endl;
gotoxy(21,10);cout<<"Data yang akan di tambahkan = ";
cin>>baru;
InsertLast(&head,&tail,baru);
gotoxy(21,12);cout<<"Data Berhasil Ditambahkan"<<endl;
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
getch ();
break;}
case 3:
{for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
} gotoxy(28,2);cout<<"Mengapus Node awal List";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{gotoxy(21,8);cout<<"List Kosong";}
cout<<endl;
DeleteFirst(&head,&tail);
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
getch ();
break;}
case 4:
{for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
} gotoxy(28,2);cout<<"Menghapus Node akhir dari List";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{gotoxy(21,8);cout<<"List Kosong";}
cout<<endl;
DeleteLast(&head,&tail);
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
getch ();
break;}
case 5:
{for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
} gotoxy(28,2);cout<<"Menambahkan Data pada posisi tertentu";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{gotoxy(21,8);cout<<"List Kosong";}
cout<<endl;
gotoxy(21,10);cout<<"Insert Data = ";
cin>>baru;
gotoxy(21,12);cout<<"Insert Posisi = ";
cin>>posisi;
InsertAfter(&head,&tail, baru, posisi);
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
//Print (head);
getch ();
break;}
case 6:
{for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
}gotoxy(28,2);cout<<"Menghapus Data pada posisi tertentu";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{gotoxy(21,8);cout<<"List Kosong";}
cout<<endl;
gotoxy(21,10);cout<<"Posisi yang akan dihapus : ";
cin>>posisi;
DeleteAfter(&head, &tail, posisi);
gotoxy(21,14);cout<<"Kondisi List saat ini : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{cout<<"List Kosong";}
//Print(head);
getch ();
break;}
case 7:
{
for(a=1;a<53;a++)
{
gotoxy(14+a,3);printf("%c",bes);
gotoxy(14+a,4);printf("%c",b);
gotoxy(14+a,22);printf("%c",bes);
gotoxy(14+a,21);printf("%c",b);
}
for(c=1;c<23;c++)
{
gotoxy(18,1+c);printf("%c",b);
gotoxy(19,1+c);printf("%c",d);
}gotoxy(28,2);cout<<"Mencari Data dalam list";
Pnode bantu;
gotoxy(21,8);cout<<"List Sebelumnya : ";
if (head != NULL)
{
bantu = head;
while (bantu != NULL)
{
cout<<bantu->info<<" ->";
bantu = bantu->next;
}
}
else
{gotoxy(21,8);cout<<"List Kosong";}
cout<<endl;
if (head==NULL)
{gotoxy(21,8);cout<<"List Kosong";}
else
{
gotoxy(21,10);cout<<"data yang ingin dicari : ";
cin>>data;
posisi = Searching (head, data);
if (posisi==0)
{ gotoxy(21,12);cout<<"data yang dicari tidak ada";}
else
{ gotoxy(21,14);cout<<"data ada pada posisi : "<<posisi;}
}
getch ();
break;}
//case 8:
//{ Print(head);
// getch ();
// break;}
default:
{ cout<<"tidak ada dalam pilihan"<<endl;
goto lagi;}
}
}while(pilihan>=1 && pilihan<=8);
getch (); }
copy Source Code d'atas...
Untuk penjelasannya,,bisa kirim ke "Contact Me" di atas..
Thax
Posting Komentar