Breaking News

Sunday, 13 December 2015

Stack

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

#define max 100

typedef struct {
int top;
                  int info[max];
                  int size;
} stack;

void createStack(stack *s)
{
s->top = NULL;
}

void stackSize (stack *s, int x)
{
s->size = x;
}

bool isFull (stack *s)
{
return (s->top == s-> size);
}

bool isEmpty (stack *s)
{
return (s->top == NULL);
}

void push (stack *s, int x)
{
s->top = s->top + 1;
   s->info[s->top] = x;
}

void pop (stack *s, int *x)
{
*x = s->info[s->top];
   s->top= s->top - 1;
}

void display(stack *s)
{
int x;
do{
      pop(s,&x);
      cout<<x<<endl;
   }while (!isEmpty(s));
}
int main()
{
   stack s;
   int x;

   createStack(&s);
   stackSize(&s,5);

   do {
cout<<"Masukkan nilai stack : ";
      cin>>x
      push(&s,x)
   }while(!isFull(&s));

   display(&s);

return 0;
getch();
}

No comments:

Post a Comment

Designed By Fakhri Akbar