Contoh Program C++ Stack

Rabu, 10 Oktober 2012

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#define MAXSTACK 100
typedef int itemType;
typedef struct
{

int item[MAXSTACK];
int jml;
} Stack;
void init(Stack *s)
{
s->jml=0;
}
int kosong(Stack *s)
{
return (s->jml==0);
}
int penuh(Stack *s)
{
return (s->jml==MAXSTACK);
}
void isi(itemType x, Stack *s)
{
if(penuh(s))
printf(“\nMaaf data sudah penuh\n”);
else
{
s->item[s->jml]=x;
++(s->jml);
}
}
void ambil(Stack *s, itemType *x)
{
if(kosong(s))
printf(“\nMaaf data masih kosong\n”);
else
{
–(s->jml);
*x=s->item[s->jml];
s->item[s->jml]=0;
printf(“\nData %i berhasil diambil\n”,*x);
}
}
void tampil(Stack *s)
{
if(kosong(s))
printf(“\nMaaf Data masih kosong\n”);
else
printf(“\n”);
for(int i=s->jml-1;i>=0;i–)
{
printf(“Data: %d\n”,s->item[i]);
}
}
void hapus(Stack *s)
{
s->jml=0;
printf(“\nSemua data berhasil dihapus\n”);
}
void main()
{
int pil;
Stack tumpukan;
itemType data;
init(&tumpukan);
do
{
printf(“\nMENU: \n 1. Isi\n 2. Ambil\n 3. Lihat\n 4. Hapus\n 5. Keluar\n”);
printf(“Masukkan pilihan: “); scanf(“%i”,&pil);
switch(pil)
{
case 1:
printf(“\nMasukkan data: “); scanf(“%i”,&data);;
isi(data,&tumpukan);
break;
case 2:
ambil(&tumpukan,&data);
break;
case 3:
tampil(&tumpukan);
break;
case 4:
hapus(&tumpukan);
break;
}
}while(pil!=5);
getch();
}
Share this article :

0 komentar:

Speak up your mind

Tell us what you're thinking... !

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. NICE BLOG - All Rights Reserved
Template Created by Creating Website Inspired by Sportapolis Shape5.com
Proudly powered by Blogger