You are on page 1of 5

‫االسم ‪ :‬فيصل زيد حامد النعيمي‬

‫المرحلة ‪ :‬الثانية‬
‫المادة ‪ :‬هياكل بيانات‬
Write a program in C++ to implement stack by using array of
structures.

#include <iostream>

#include <stdlib.h>

using namespace std;

///////////////////////////////////////////

#define max 6

struct stackst

int stackarray[max];

int top;

};

///////////////////////////////////////////

class myclass

public:

void clear(stackst *s);

int pop(stackst *s);

void push(stackst *s,int x);

int full(stackst *s);


int empty(stackst *s);

myclass();

virtual ~myclass();

};

///////////////////////////////////////////

int main()

myclass my0bj;

stackst s;

my0bj.push(&s,7);

my0bj.push(&s,14);

my0bj.push(&s,21);

my0bj.push(&s,28);

my0bj.push(&s,35);

my0bj.push(&s,42);

my0bj.push(&s,49);

for(int i=0;i=max;i++)

my0bj.pop(&s);

return(0);

}
///////////////////////////////////////////

void myclass::clear(stackst *s)

s->top=-1;

int myclass::full(stackst *s)

if(s->top==max)

return(1);

else

return(0);

////////////////////

int myclass::empty(stackst *s)

if(s->top==-1)

return(1);

else

return(0);

//////////////////

void myclass::push(stackst *s,int x)


{

s->top++;

if(full(s))

cout<<"Stack full......"<<endl;

exit(0);

s->stackarray[s->top]=x;

//////////////////

int myclass::pop(stackst *s)

if(empty(s))

cout<<"Stack empty......"<<endl;

exit(0);

return(s->stackarray[s->top--]);

You might also like