Sabtu, 17 April 2010

Cpp stackdinamik

#include
#include
#include

//----------------------

struct nilaiMatKul{
char nim[10];
char nama[50];
float nilai;
};

struct elemen{
nilaiMatKul elmt;
struct elemen *next;
};

struct stack{
struct elemen *top;
};

//----------------------

void createEmpty(stack *S){


(*S).top = NULL;

}

//----------------------

bool isEmpty(stack S){

bool hasil = false;

if(S.top == NULL){
hasil = true;
}

return hasil;

}

//----------------------

int countElement(stack S){


int hasil = 0;

if(S.top != NULL){
/* stack tidak kosong */

elemen *elmt;

/* inisialisasi */
elmt = S.top;

while(elmt != NULL){
/* proses */
hasil= hasil + 1;

/* iterasi */
elmt = elmt->next;
}

}

return hasil;

}

//----------------------

void push(char nim[], char nama[], float nilai, stack *S ){


elemen *elmt;
elmt = (elemen *) malloc (sizeof (elemen));
strcpy(elmt->elmt.nim, nim);
strcpy(elmt->elmt.nama, nama);
elmt->elmt.nilai = nilai;
elmt->next = (*S).top;
(*S).top = elmt;
elmt = NULL;

}

//----------------------

void pop(stack *S){

if((*S).top != NULL){
/* jika stack bukan stack kosong */
elemen *elmt = (*S).top;
(*S).top = (*S).top->next;
elmt->next = NULL;
free(elmt);
}

}

//----------------------

void printStack(stack S){

if(S.top != NULL){
printf("------isi stack------\n");
elemen *elmt = S.top;
int i = 1;

while(elmt != NULL){
printf("====================\n");
printf("elemen ke : %d\n", i);
printf("nim : %s\n", elmt->elmt.nim);
printf("nama : %s\n", elmt->elmt.nama);
printf("nilai : %f\n", elmt->elmt.nilai);

/* iterasi */
elmt = elmt->next;
i = i + 1;

}
printf("---------------------\n");
}
else{
/* proses jika stack kosong */
printf("stack kosong\n");
}

}

//----------------------

int main(){

struct stack S;

createEmpty(&S);
printStack(S);

printf("=================\n");

push("13507701", "Nana", 64.75, &S);
push("13507702", "Rudi", 75.11, &S);
push("13507703", "Dea", 84.63, &S);
printStack(S);

printf("=================\n");

pop(&S);
pop(&S);
printStack(S);

printf("=================\n");

return 0;

}

Cpp stackstatis
#include
#include

//----------------------

struct nilaiMatKul{
char nim[10];
char nama[50];
float nilai;
};

struct stack{
int top;
nilaiMatKul data[10];
};

//----------------------

void createEmpty(stack *S){


(*S).top = -1;

}

//----------------------

bool isEmpty(stack S){

bool hasil = false;

if(S.top == -1){
hasil = true;
}

return hasil;

}

//----------------------

bool isFull(stack S){

bool hasil = false;
if(S.top == 9){
hasil = true;
}

return hasil;

}

//----------------------

void push(char nim[], char nama[], float nilai, stack *S ){


if(isFull(*S) == true){
/*jika stack penuh*/
printf("stack penuh\n");
}
else{
if(isEmpty(*S) == true){
/* jika stack kosong */
(*S).top = 0;
strcpy((*S).data[0].nim, nim);
strcpy((*S).data[0].nama, nama);
(*S).data[0].nilai = nilai;
}
else{
/* jika stack tidak kosong */
(*S).top = (*S).top + 1;
strcpy((*S).data[(*S).top].nim, nim);
strcpy((*S).data[(*S).top].nama, nama);
(*S).data[(*S).top].nilai = nilai;
}
}

}

//----------------------

void pop(stack *S){

if((*S).top == 0){
/*jika stack berisi satu elemen*/
(*S).top = -1;
}
else{
if((*S).top != -1){
/*jika stack tidak kosong*/
(*S).top = (*S).top - 1;
}
}

}

//----------------------

void printStack(stack S){

if(S.top != -1){
printf("------isi stack------\n");
int i;
for(i=S.top;i>=0;i--){
printf("====================\n");
printf("elemen ke : %d\n", i);
printf("nim : %s\n", S.data[i].nim);
printf("nama : %s\n", S.data[i].nama);
printf("nilai : %f\n", S.data[i].nilai);
}
printf("---------------------\n");
}
else{
/* proses jika stack kosong */
printf("stack kosong\n");
}

}

//----------------------

int main(){

struct stack S;

createEmpty(&S);
printStack(S);

printf("=================\n");

push("13507701", "Nana", 64.75, &S);
push("13507702", "Rudi", 75.11, &S);
push("13507703", "Dea", 84.63, &S);
printStack(S);

printf("=================\n");

pop(&S);
pop(&S);
printStack(S);

printf("=================\n");

return 0;

}