Sei sulla pagina 1di 2

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

#define MAX 10
using namespace std;

typedef int element;

struct pila{
element e[MAX];
int sp;
};

typedef struct pila* ppila;

bool push(ppila p, element x);


ppila newStack();
void showStack(ppila p);
bool pop(ppila p, element &x);
bool isFullStack(ppila p);
bool isEmptyStack(ppila p);

ppila newStack(){
ppila p = new struct pila;
p->sp = 0;
return p;
}

bool push(ppila p, element x){


if(!isFullStack(p)){
p->e[p->sp] = x;
p->sp++;
return true;
} else {
return false;
}
}

void showStack(ppila p){

for(int i=0; i<p->sp; i++){


cout << p->e[i] << endl;
}

bool pop(ppila p, element &x){


if(!isEmptyStack(p)){
p->sp--;
x = p->e[p->sp];
return true;
} else {
return false;
}
}

bool isFullStack(ppila p){


return (p->sp == MAX);
}

bool isEmptyStack(ppila p){


return (p->sp == 0);
}

int main()
{
ppila puntatorePila = newStack();

push(puntatorePila, 5);
push(puntatorePila, 9);

showStack(puntatorePila);

element uscita;
pop(puntatorePila,uscita);

cout << "USCITA:" << uscita <<endl;


showStack(puntatorePila);

return 0;
}

Potrebbero piacerti anche