Selasa, 09 Februari 2010

contoh program STACK

#include (iostream.h)
#include (stdlib.h)
#include (conio.h)
class elemen{
struct list{
int bil;
struct list * next;
};
public:
typedef struct list NODE;
typedef NODE * PNODE;

void tampil(PNODE atas);
void PUSH(PNODE *atas, PNODE baru);
void POP(PNODE *atas);
PNODE masuk(void);
};

void elemen::tampil(PNODE atas){
PNODE pos;
pos = atas;
cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", "; pos=pos->next;
}
}

void elemen::PUSH(PNODE *atas, PNODE baru){
if(*atas==NULL){
*atas = baru;
}else{
baru->next = *atas;
*atas = baru;
}
}

void elemen::POP(PNODE *atas){
PNODE PH;
PH = *atas;
if(*atas==NULL){
cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){
*atas = NULL;
free(PH);
}else{
*atas = (*atas)->next;
free(PH);
}
}
}

PNODE elemen::masuk(void){
PNODE baru;
baru=(NODE *)malloc(sizeof(NODE));
cout<<"\nbil : ";cin>>baru->bil;
baru->next=NULL;
return(baru);
}

void main(){
PNODE atas=NULL;
PNODE baru;
int pil=1;
elemen data;
clrscr();
while(pil!=4){
cout<<"\n\n1. PUSH STACK"; cout<<"\n2. LIHAT DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan : ";cin>>pil;
if (pil==1){
baru=data.masuk();
data.PUSH(&atas, baru);
}
if (pil==2)
data.tampil(atas);
if (pil==3){
data.POP(&atas);
}
}
}

Tidak ada komentar:

Posting Komentar