Pages

Senin, 16 Januari 2012

STACK : BINER [TUGAS]


#include<stdio.h>
#include<conio.h>
#include<iostream.h>

int MAXSTACK;
typedef int itemtype;

typedef struct{
 itemtype item[300];
 int count; }stack;

void initstack(stack *s){
 s->count = 0; }

int empty(stack *s){
 return (s->count == 0);
}

int full(stack *s){
 return (s->count == MAXSTACK);
}

void push(itemtype x, stack *s){
 if(full(s))
 cout<< "stack penuh !\n";
 else{
 s->item[s->count]=x;
 ++(s->count);
 }
}

int pop(stack *s){
 if(empty(s))
 cout<<"stack kosong\n";
 else {
 --(s->count);
 return (s->item[s->count]);
 }
 return 0;
}
//deklarasi
int i, n, m, o;
int input;
stack tumpukan;

main()
{

 initstack(&tumpukan);
 cout << "masukkan bilangan desimal = ";cin>>input;

for(o=1,n=input;n>0;n=n/2,o++)
 {
 MAXSTACK=o;
 m=n%2;
 push(m,&tumpukan);
 }
for(i=MAXSTACK;i>0;i--)
 {
 cout<<pop(&tumpukan);
 }
 getch();
}


Print Friendly and PDF

Artikel Terkait:

0 komentar:

Posting Komentar

Related Posts Plugin for WordPress, Blogger...