Ok adesso compila, scrivendo:
codice:
namespace StruttureDati {
int *myStack;
int size;
#define CAPACITY 100
int top;
Stack::Stack() {
size=0;
top=0;
myStack=new int[CAPACITY];
// TODO Auto-generated constructor stub
}
Purtroppo non ho ben capito come implementare il metodo:
bool pop(int &a).
In Java mi basta assegnare ad una variabile il valore null, qui non lo posso fare e dovrei sempre sfruttare i puntatori...solo che esattamente non ho capito come fare a puntare alla locazione &a.
Tipo scrivendo cosģ:
codice:
bool pop(int &a)
{
if(&top==NULL)
{
printf("Error!!! Stack is empty!");
return false;
}
myStack[top]=NULL;
if(size!=0)
{
size--;
top=size;
}
return true;
}
Mi dą errore su:
codice:
myStack[top]=NULL;
Come mai? Essendo l' array un puntatore, in questo modo non assegno null al puntatore di una locazione dell' array?
Inoltre non riesco a costruire gli oggetti, per il fatto di avere un namespace, cosa cambia nella costruzione?
Come andrebbe questa versione secondo te?
codice:
/*
* Stack.cpp
*
* Created on: 15/nov/2011
* Author: Dario89
*/
#include "Stack.h"
#include <stdio.h>
#include <iostream>
using namespace std;
namespace StruttureDati {
int *myStack;
int size;
#define CAPACITY 100
int top;
Stack::Stack() {
size=0;
top=-1;
myStack=new int[CAPACITY];
// TODO Auto-generated constructor stub
}
bool push(int a)
{
if(size<CAPACITY){
myStack[size++]=a;
top++;
return true;
}
printf("Error!!! Stack is full!");
return false;
}
bool pop(int &a)
{
if(top==-1)
{
printf("Error!!! Stack is empty!");
return false;
}
myStack[top--]=0;
size--;
return true;
}
Stack::~Stack() {
delete [] myStack;
// TODO Auto-generated destructor stub
}
} /* namespace StruttureDati */
int main()
{
}