Selasa, 15 November 2011

List Linier Type2 #C++

langkah 1
--------------------------------------------------------------------------
#define BOOLEAN_H

#define true 1
#define false 0
#define boolean unsigned char
--------------------------------------------------------------------------
simpan scrip diatas pake nama --> "boolean.h"
==========================================================================

langkah 2
--------------------------------------------------------------------------
#define info(P) (*P).info
#define nil NULL
#define next(P) (P)->next
#define first(L) ((L).first)

#include <iostream.h>
#include <stdlib.h>
#include "boolean.h"

#define infotype int

typedef struct telmtlist *address;
typedef struct telmtlist{
infotype info;
address next;
} elmtlist;

typedef struct {
address first;
} list;
/* Primitif-primitif ==================================================*/

address alokasi (infotype x){
//alokasi nilai P
address P;
P = (address) malloc (sizeof(elmtlist));
if (P==nil){
return nil;
}else{
info(P)=x;
next(P)=nil;
return (P);
}
}
/*--------------------------------------------------------------------------*/
void dealokasi (address P){
//dealokasi nilai P
free(P);
}

/*--------------------------------------------------------------------------*/

boolean IsEmpty (list L){
//mengecek list kosong
return (first(L) == nil);
}
/*--------------------------------------------------------------------------*/
void CreateEmpty (list *L){
//membuat list baru;
first(*L)=nil;
}

/*--------------------------------------------------------------------------*/

void Transversal1(list L){
//transversal list 1
address P;
int jml=0;
if(first(L)==nil){
cout<<"List Kosong";
}else{
P=first(L);
while(next(P)!=P){
jml+=info(P);
P=next(P);
}
jml+=info(P);
cout<<"\njumlah semua elemen = "<<jml;
}
}
/*--------------------------------------------------------------------------*/
void Transversal2(list L){
//transversal list 2
address P;
int jml=0;

P=first(L);
while(next(P)!=P){
jml+=info(P);
P=next(P);
}
jml+=info(P);
cout<<"\njumlah semua elemen = "<<jml;
}
/*--------------------------------------------------------------------------*/
address search(list L, infotype x){
//menseasrch address nilai x
if(first(L)!=nil){
address P=first(L);
boolean found=false;
while(next(P)!=P && found!=true){
if(info(P)==x){
found=true;
}else{
P=next(P);}
}
if(info(P)!=x){
P=nil;
}
 return(P);}
}
/*--------------------------------------------------------------------------*/
void ListSearch1(list L,address P, boolean *found){
/*Search by Address
P dan L terdefinisi
*/
if (first(L)!=nil){
address Px=first(L);
*found=false;
while(next(Px)!=Px && *found!=true){
if(Px==P){
*found=true;
}else{
Px=next(Px);}
}
if(Px==P){
*found=true;
}
}
}
/*--------------------------------------------------------------------------*/
void ListSearch2(list L,infotype x, address *P, boolean *found){
/*Search by Value
P dan L terdefinisi
*/
if (first(L)!=nil){
address Px=first(L);
*found=false;
while(next(Px)!=Px && *found!=true){
if(info(Px)==x){
*found=true;
*P=Px;
}else{
Px=next(Px);}
}
if(info(Px)==x){
*found=true;
*P=Px;
}
}
}
/*--------------------------------------------------------------------------*/
void InsertFirst (list *L, address P){
//menambahkan elemen di awal
if(first(*L)==nil){
first(*L)=P;
next(P)=P;
}else{
next(P)= first(*L);
first(*L)=P;}
}
/*--------------------------------------------------------------------------*/
void InsVfirst (list *L, infotype x){
 InsertFirst(&(*L),alokasi(x));
}

/*--------------------------------------------------------------------------*/
void InsertAfter (address P, address prec){
if(next(prec)==prec){
next(prec)=P;
next(P)=P;
}else{
next (P) = next (prec);
next(prec) = P;}
}
/*--------------------------------------------------------------------------*/
void InsertLast (list *L, address P){
if(first(*L)==nil){
InsertFirst(&*L,P);
}else{
address last = first(*L);
while(next(last)!=last){
last=next(last);
}
next(last)=P;
next(P)=P;
}

}
/*--------------------------------------------------------------------------*/
void InsVlast (list *L, infotype x){
 InsertLast(&(*L),alokasi(x));
}

/*--------------------------------------------------------------------------*/
void DelFirst (list *L, address *P){
if(first(*L)!=nil){
if(next(first(*L))==first(*L)){
(*P) = first(*L);
first(*L)=nil;
}else{
(*P) = first(*L);
first(*L)=next(first(*L));}
}
}
/*--------------------------------------------------------------------------*/
void DelVfirst(list *L,infotype *E){
address P;
DelFirst(&(*L),&P);
*E=info(P);
dealokasi(P);
}
/*--------------------------------------------------------------------------*/
void DelLast (list *L, address *P){
if(first(*L)==nil){
}
else if (next(first(*L))==first(*L)){
DelFirst(&(*L),&(*P));
}
else{
address last= first(*L);
while(next(next(last))!=next(last)){
last=next(last);
}
(*P)=next(last)  ;
next(last)=last;

}}
/*--------------------------------------------------------------------------*/
void DelVlast(list *L, infotype *E){
address P;
DelLast(&(*L),&P);
*E=info(P);
dealokasi(P);
}

/*--------------------------------------------------------------------------*/
void DelAfter (list *L, address *pdel, address prec){
if(next(next(prec))==next(prec)){
(*pdel)=next(prec);
DelLast(&(*L),&(*pdel));
}else{
(*pdel)=next(prec);
next(prec)=next(next(prec));}
}
/*--------------------------------------------------------------------------*/
void PrintInfo (list L){
//mencetak semua info elemen pada list
if(first(L)==nil){
cout<<"list kosong !!";
}else{
address P =first(L);
while(next(P)!=P){
cout<<info(P)<<" -> ";
P=next(P);
}
cout<<info(P)<<" <- ";

}
}

/*--------------------------------------------------------------------------*/
infotype NbElmt (list L){
if(first(L)==nil){
return 0;
}else{
address P = first(L);
infotype bnyk=0;
while(next(P)!=P){
bnyk++;
P=next(P);
}
bnyk++;
return (bnyk);
}
}
/*--------------------------------------------------------------------------*/
/*======================max min =====================*/
infotype max (list L){
//mengembalikan nilai maksimum
if(first(L)==nil){
return 0;
}else{

address P =first(L);
infotype max = info(P);

while(next(P)!=P)
{
if(info(P)>max){
 max = info(P);
}
P=next(P);
}
if(info(P)>max){
 max = info(P);
}
return (max);
}
}
/*--------------------------------------------------------------------------*/
address maxadd (list L){
//mengembalikan address nilai maksimum
if(first(L)==nil){
return nil;
}else{

address P =first(L);
infotype max = info(P);
address maxadd = P;
while(next(P)!=P)
{
if(info(P)>max){
  max = info(P);
  maxadd=P;
  }
P=next(P);
}
if(info(P)>max){
 max = info(P);
 maxadd=P;
 }
return (maxadd);
}
}

/*--------------------------------------------------------------------------*/
infotype min (list L){

if(first(L)==nil){
return 0;
}else{

address P =first(L);
infotype min = info(P);

while(next(P)!=P){
if(info(P)<min){
 min = info(P);
}
P=next(P);
}
if(info(P)<min){
 min = info(P);
}

return (min);
}

}
/*--------------------------------------------------------------------------*/
address minadd (list L){

if(first(L)==nil){
return nil;
}else{

address P =first(L);
infotype min = info(P);
address minadd=P;
while(next(P)!=P){
if(info(P)<min){
 min = info(P);
 minadd=P;
}
P=next(P);
}
if(info(P)<min){
 min = info(P);
 minadd=P;
}

return (minadd);
}

}
/*--------------------------------------------------------------------------*/
void DelAll (list *L){
address P=first(*L);
address Px;
while (next(P)!=P){
DelFirst(&(*L),&Px);
dealokasi(Px);
P=next(P);
}
DelFirst(&(*L),&Px);
dealokasi(Px);
}
/*--------------------------------------------------------------------------*/
void invers (list *L){
if(first(*L)!=nil && next(first(*L))!=first(*L)){
address P= next(first(*L));
address last = first (*L);
address prec=first(*L);

while (next(P)!=P){
  prec=P;
  P=next(P);
  next(prec)=first(*L);
  first(*L)=prec;
}
 prec=P;
  next(prec)=first(*L);
  first(*L)=prec;
next(last)=last;
}
}

/*--------------------------------------------------------------------------*/
void CopyList (list *L1,list *L2 ){
first(*L2)=first(*L1);
}
/*--------------------------------------------------------------------------*/
void pindah (list L, list *L3){
if(first(L)!=nil){
address last = first (L);
while (next(last)!=last){
InsVlast(&(*L3),info(last));
last=next(last);
}
InsVlast(&(*L3),info(last));
}
}
/*--------------------------------------------------------------------------*/
void KoncatList(list L1,list L2,list *L3){
pindah(L1,&(*L3));
pindah(L2,&(*L3));
}
/*--------------------------------------------------------------------------*/
void KoncatList1(list *L1,list *L2, list *L3){
if(first(*L1)==nil){
 first(*L3)=first(*L2);
}else if (first(*L2)==nil){
first(*L3)=first(*L1);
}else{
address last =first(*L1);
while (next(last)!=last){
last=next(last);
}
next(last)=first(*L2);
first(*L3)=first(*L1);
first(*L1)=nil;
first(*L2)=nil;
}
}
/*--------------------------------------------------------------------------*/
void PecahList (list L, list *L1,list *L2){
if(first(L)!=nil){
int tengah=NbElmt(L)/2;
int i=0;
address last=first(L);

while(next(last)!=last){
 if(i<=tengah){
 InsVlast(&(*L1),info(last));
 }else{
 InsVlast(&(*L2),info(last));
 }
 i++;
 last=next(last);
}

if(i<=tengah){
 InsVlast(&(*L1),info(last));
 }else{
 InsVlast(&(*L2),info(last));
}


}
}

/*--------------------------------------------------------------------------*/

--------------------------------------------------------------------------
simpan scrip diatas dengan nama file --> "list.h"
==========================================================================

langkah 3
--------------------------------------------------------------------------
#include "list.h"
#include <conio.h>

void cek(list *L){
int b;
int x;
int i;

if(IsEmpty(*L)){
cout<<"\nmasukkan banyak el list : ";cin>>b;

for(i=0;i<b;i++){
cout<<"nilai : ";cin>>x;
InsVlast(&(*L),x);
}}

}

void main(){
list L ;
CreateEmpty(&L);
list L2;
CreateEmpty(&L2);
list L3;
CreateEmpty(&L3);

int pil;
do{
int b=0;
int x=0;
int i=0;
address P=nil;
address prec=nil;
boolean found=false;
cout<<"--Menu List Type2--\n\n";
cout<<"1.IsEmpty\n";
cout<<"2.ListTransversal1\n";
cout<<"3.ListTransversal2\n";
cout<<"4.ListSearch1\n";
cout<<"5.ListSearch2\n";
cout<<"6.InsertFirst\n";
cout<<"7.InsertVFirst\n";
cout<<"8.InsertAfter\n";
cout<<"9.InsertLast\n";
cout<<"10.InsertVlast\n";
cout<<"11.DeletFirst\n";
cout<<"12.DeleteVfirst\n";
cout<<"13.DeleteAfter\n";
cout<<"14.DeleteLast\n";
cout<<"15.DeleteVlast\n";
cout<<"16.PrintInfo\n";
cout<<"17.NbElmt\n";
cout<<"18.Max\n";
cout<<"19.Maxaddress\n";
cout<<"20.Min\n";
cout<<"21.MinAddress\n";
cout<<"22.DellAll\n";
cout<<"23.InverList\n";
cout<<"24.CopyList\n";
cout<<"25.KoncatList\n";
cout<<"26.KoncatList1\n";
cout<<"27.PecahList\n";
cout<<"99.Keluar\n";
cout<<"pilihan menu : ";
cin>>pil;
clrscr();
switch(pil){
/*-------------------------------------------------------------------------*/
case 1:
cout<<"mengecek list kosong atau tidak.\n" ;
if(IsEmpty(L)){
cout<<"list Kosong";
}else{
cout<<"list tidak Kosong";
}
break;
/*-------------------------------------------------------------------------*/
case 2:
cout<<"Transversal1\ndengan proses(P) menjumlahkan semua el.list\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
Transversal1(L);
break;
/*-------------------------------------------------------------------------*/
case 3:
cout<<"Transversal2\ndengan proses(P) menjumlahkan semua el.list\nList tdak bleh kosong\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
Transversal2(L);
break;
/*-------------------------------------------------------------------------*/

case 4:
cout<<"ListSearch1\nSearch by Address\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di cari = ";cin>>x;
P =search(L,x);
boolean found;
ListSearch1(L,P,&found);
if(found){
 cout<<"address ada";
}else{
cout<<"address tidak ada";
}
break;
/*-------------------------------------------------------------------------*/
case 5:
cout<<"ListSearch2\nSearch by Value\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di cari = ";cin>>x;
ListSearch2(L,x,&P,&found);
if(found){
 cout<<"nilai di temukan\ndengan address = "<<P;
}else{
cout<<"nilai tidak ada";
}
break;
/*-------------------------------------------------------------------------*/
case 6:
cout<<"InsertFirst\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di Input = ";cin>>x;
P=alokasi(x);
InsertFirst(&L,P);
cout<<"setelah Insert First\n";
cout<<"list : ";PrintInfo(L);

break;
/*-------------------------------------------------------------------------*/
case 7:
cout<<"InsertVFirst\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di Input = ";cin>>x;
InsVfirst(&L,x);
cout<<"setelah Insert First\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/
case 8:
cout<<"InsertAfter\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di Input = ";cin>>b;
P=alokasi(b);
cout<<"\nmasukkan nilai setelah = ";cin>>x;
prec=search(L,x);
if(prec!=nil){
InsertAfter(P,prec);
cout<<"setelah Insert After\n";
cout<<"list : ";PrintInfo(L);
}else{
cout<<" nilai tidak ada ";
}
break;
/*-------------------------------------------------------------------------*/

case 9:
cout<<"InsertLast\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di Input = ";cin>>x;
P=alokasi(x);
InsertLast(&L,P);
cout<<"setelah Insert Last\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/
case 10:
cout<<"InsertVLast\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nmasukkan nilai yang di Input = ";cin>>x;
InsVlast(&L,x);
cout<<"setelah Insert last\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/

case 11:
cout<<"DeleteFirst\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
DelFirst(&L,&P);
cout<<"\nsetelah Delete First\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/


case 12:
cout<<"DeleteVFirst\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
DelVfirst(&L,&x);
cout<<"\nsetelah Delete First\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nnilai yang di hapus = "<<x;
break;
/*-------------------------------------------------------------------------*/


case 13:
cout<<"DeleteAfter\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
cout<<"\nhapus setelah nilai : ";cin>>x;
prec=search(L,x);
if(prec!=nil){
DelAfter(&L,&P,prec);
cout<<"\nsetelah Delete After\n";
cout<<"list : ";PrintInfo(L);
}else{
cout<<"nilai tidak ditemukan ";
}
break;
/*-------------------------------------------------------------------------*/

case 14:
cout<<"DeleteLast\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
DelLast(&L,&P);
cout<<"\nsetelah Delete Last\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/


case 15:
cout<<"DeleteVLast\n";
cek(&L);
cout<<"list : ";PrintInfo(L);
DelVlast(&L,&x);
cout<<"\nsetelah Delete First\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nnilai yang di hapus = "<<x;
break;
/*-------------------------------------------------------------------------*/

case 16:
cek(&L);
cout<<"Print Info\n";
PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/

case 17:
cek(&L);
cout<<"NbElmt\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nBanyak el. = "<<NbElmt(L);
break;
/*-------------------------------------------------------------------------*/

case 18:
cek(&L);
cout<<"Max\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nMax el. = "<<max(L);
break;
/*-------------------------------------------------------------------------*/

case 19:
cek(&L);
cout<<"amax address\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nAddress max el. = "<<maxadd(L);
cout<<"\nMax el. = "<<max(L);
break;
/*-------------------------------------------------------------------------*/

case 20:
cek(&L);
cout<<"Min\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nMin el. = "<<min(L);
break;
/*-------------------------------------------------------------------------*/

case 21:
cek(&L);
cout<<"Min Address\n";
cout<<"list : ";PrintInfo(L);
cout<<"\nAddress msin el. = "<<minadd(L);
cout<<"\nMin el. = "<<min(L);
break;
/*-------------------------------------------------------------------------*/

case 22:
cout<<"DelAll\n";
cek(&L);
cout<<"list : ";PrintInfo(L);

DelAll(&L);
cout<<"\nsetelah di hapus semua\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/

case 23:
cout<<"Invers List\n";
cek(&L);
cout<<"list : ";PrintInfo(L);

invers(&L);
cout<<"\nsetelah Invers\n";
cout<<"list : ";PrintInfo(L);
break;
/*-------------------------------------------------------------------------*/

case 24:
cout<<"Copy List\n";
cek(&L);
cout<<"list : ";PrintInfo(L);

CopyList(&L,&L2);
cout<<"\nsetelah di kopi\n";
cout<<"list1 : ";PrintInfo(L);
cout<<"\nlist2 : ";PrintInfo(L2);
first(L2)=nil;
break;
/*-------------------------------------------------------------------------*/

case 25:
cout<<"Koncat List\n";
cek(&L);
cout<<"list1 : ";PrintInfo(L);

cek(&L2);
cout<<"\nlist2 : ";PrintInfo(L2);

KoncatList(L,L2,&L3);
cout<<"\nsetelah di Koncat\n";
cout<<"list1 : ";PrintInfo(L);
cout<<"\nlist2 : ";PrintInfo(L2);
cout<<"\nlist3 : ";PrintInfo(L3);

break;
/*-------------------------------------------------------------------------*/

case 26:
if(!IsEmpty){
DelAll(&L3);
cout<<"\nlist3 : ";PrintInfo(L3);
}

cout<<"Koncat List1\n";
cek(&L);
cout<<"list1 : ";PrintInfo(L);

cek(&L2);
cout<<"\nlist2 : ";PrintInfo(L2);

KoncatList1(&L,&L2,&L3);
cout<<"\nsetelah di Koncat\n";
cout<<"list1 : ";PrintInfo(L);
cout<<"\nlist2 : ";PrintInfo(L2);
cout<<"\nlist3 : ";PrintInfo(L3);

break;
/*-------------------------------------------------------------------------*/

case 27:
cout<<"Pecah List\n";
cek(&L);
cout<<"list : ";PrintInfo(L);

PecahList(L,&L2,&L3);
cout<<"\nsetelah di Pecah\n";
cout<<"list2 : ";PrintInfo(L2);
cout<<"\nlist3 : ";PrintInfo(L3);

break;
/*-------------------------------------------------------------------------*/



}

getch();
clrscr();
}while(pil!=99);

cout<<"by : Angga Kesuma, 09101003051 " ;
}
--------------------------------------------------------------------------
simpan scrip diatas dengan nama "mainlist.cpp"

dan hasilnya adalah --> http://www.mediafire.com/?awe4m7a7c8u5f8m
kalo masih pusing cek ini --> http://www.mediafire.com/?99p8sp84epa5r6c

Tidak ada komentar:

Posting Komentar