Sabtu, 06 Oktober 2012

link list


LINK LIST

1. Menambah di depan
void insertDepan(int databaru){
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = baru;
if(isEmpty()==1){
head=baru;
head->next=head;
}
else {
bantu = head;
while(bantu->next!=head){
bantu=bantu->next;
}
baru->next = head;
head = baru;
bantu->next = head;
}
printf(”Data masuk\n“);
}


2. Menambah elemen dibelakang
void insertBelakang (int databaru){
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = baru;
if(isEmpty()==1){
head=baru;
head->next=head;
}
else {
bantu = head;
while(bantu->next != head){
bantu=bantu->next;
}
bantu->next = baru;
baru->next = head;
}
printf(”Data masuk\n“);
}

3. Menghapus elemen di depan
void hapusDepan (){
TNode *hapus,*bantu;
if (isEmpty()==0){
int d;
hapus = head;
d = head->data;
if(head->next != head){
bantu = head;
while(bantu->next!=head){
bantu=bantu->next;
}
head = head->next;
delete hapus;
bantu->next = head;
}else{
head=NULL;
}
printf(“%d terhapus\n“,d);
} else printf(”Masih kosong\n“);
}

4. Menghapus elemen dibelakang
void hapusBelakang(){
TNode *hapus,*bantu;
if (isEmpty()==0){
int d;
hapus = head;
if(head->next == head){
head = NULL;
}else{
bantu = head;
while(bantu->next->next != head){
bantu = bantu->next;
}
hapus = bantu->next;
d = bantu->data;
bantu->next = head;
delete hapus;
}
printf(“%d terhapus\n“,d);
} else printf(”Masih kosong\n“);
}

5. Mencari elemen
Ketemu  = false;
N=1;
While ((N<ukuran) and (not ketemu)) do
If (array[n]=kunci)then
Ketemu = true {data ketemu}
i = n {pada posisi ke – i}
end if
else n = n+1 {cek data berikutnya}
end while
if ketemu then pencarianLinier = I {data ketemu pada posisi ke-i}
else pencarianLinier = -1 {data tidak ketemu}
end if 

program


#include <cstdlib>
#include <iostream>

using namespace std;

class hit{
      private:
              double s,l,p,t,luba,voba,luku,voku;
              double as,ts,tp,lp,vp,ls,ks;
              double pi,r,vobo,lubo;
      public:
             double lubaku();
             double lupris();
             double lubol();
             };
           
double hit::lubaku(){
    cout<<"input lebar:";
    cin>>l;
    cout<<"input panjang:";
    cin>>p;
    cout<<"input tinggi:";
    cin>>t;
    cout<<"input sisi kubus:";
    cin>>s;
    luba=2*((p*l)+(p*t)+(l*t));
    voba=p*l*t;
    luku=6*(s*s);
    voku=s*s*s;
    cout<<"Volum Kubus: "<<voku<<endl;
    cout<<"Volume Balok: "<<voba<<endl;
    cout<<"Luas Permukaan Kubus: "<<luku<<endl;
    cout<<"Luas Permukaan Balok: "<<luba<<endl;
    cout<<endl;
    }
   
double hit::lupris(){
    cout<<"input alas segitiga: ";
    cin>>as;
    cout<<"input tinggi segitiga: ";
    cin>>ts;
    cout<<"input tinggi prisma: ";
    cin>>tp;
    ls=0.5*(as*ts);
    ks=(2*ts)+as;
    lp=(2*ls)+(ks*tp);
    vp=ls*ts;
    cout<<"Volume Prisma: "<<vp<<endl;
    cout<<"Luas Permukaan Prisma: "<<lp<<endl;
    cout<<endl;
}

double hit::lubol(){
       cout<<"input jari-jari: ";
       cin>>r;
       pi=3.14;
       lubo=4*pi*(r*r);
       vobo=pi*(r*r*r);
       cout<<"Luas Permukaan Bola: "<<lubo<<endl;
       cout<<"Volume Bola: "<<vobo<<endl;
       cout<<endl;
       }

   
int main(int argc, char *argv[])
{
    hit c;
    c.lubaku();
    c.lupris();
    c.lubol();
    system("PAUSE");
    return EXIT_SUCCESS;
}

.Subprogram x pangkat n dengan cara rekursif




If (n=0) or (n-1) then factorial = 1
Else factorial = n*factorial(n-1)

Fungsi iteratif dan rekursif untuk menghitung jumlah deret 1, 3, 7, 15, 31, . . . (rumus=L(n) = 2*L(n-1) +1)




·         Rekursif

#include <iostream.h>
Long L(Int n)
{ if ((n==0 || (n==1)) return(1);
Else return (n*L(n-1)+1);
}

main (){
int n;
long hasil;
cout<<”input n:”;
cin>>n;
hasil=L(n);
cout<<”Nilai”<<n<<”!=”<<hasil;
return 0;
}

Mencetak bilangan 1-10 secara Iteratif & Rekursif



·         iteratif

#include <iostream.h>
Main(){
Int I;
For(i=1;i<=10;i++)
Cout<<” “<<i;
Return 0;
}

fibonanci rekursih dan iteratif

fibonanci rekursif


#include <cstdlib>
#include <iostream>

using namespace std;

int recursiveFibo (int n)
{
if (n == 1 || n == 2)
return 1;
else
return recursiveFibo(n-1) + recursiveFibo(n-2);
}



fibonanci iteratif

#include <cstdlib>
#include <iostream>

using namespace std;

int iterativeFibo (int n)
{
if (n == 1 && n == 2)
return 1;
else
{
int f1 = 1;
int f2 = 1;

int fn;

for (int i = 3; i <= n; i++)
{
fn = f1 + f2;
f1 = f2;
f2 = fn;
}

return fn;
}
}
    //system("PAUSE");
    //return EXIT_SUCCESS;
//}