Ho fatto alcune correzioni, probabilmente quelle istruzioni dopo il break causavano il problema, praticamente quando inserivo il numero dell' istruzione andava a capo senza fare nulla....ora funziona. Una cosa sola, questo č una parte del vecchio esercizio sullo stack che avevo postato, non so se ricordi, non so come usare i distruttori, ho provato a crearli nelle varie classi, potresti dirmi come li faresti tu o se sono scorretti?
Classi linkedlist cpp e hpp:
codice:
class LinkedList
{
protected:
Node* head;
// Node* tail;
int size;
public:
LinkedList();
~LinkedList();
void setHead(Node* n);
Node* getHead();
//void setTail(Node* n);
// Node* getTail();
int getSize();
void addNode(int element);
void deleteNode();
};
#endif /* LINKEDLIST_HPP */
codice:
#include "LinkedList.hpp"
LinkedList:: LinkedList()
{
head=NULL;
size=0;
}
LinkedList::~LinkedList(){
delete head;
}
void LinkedList::setHead(Node* n){head=n;}
Node* LinkedList::getHead(){return head;}
// void LinkedList:: setTail(Node* n){tail=n;}
// Node* LinkedList::getTail(){return tail;}
int LinkedList::getSize(){return size;}
void LinkedList::addNode(int element){
Node* nuovo=new Node(element);
if(size==0)
{
head=nuovo;
}
else
{
nuovo->setNext(head);
head=nuovo;
}
size++;
}
void LinkedList::deleteNode(){
if(size>0)
{
Node* tmp=getHead();
Node* succ=tmp->getNext();
head=succ;
delete tmp;
//delete succ;
size--;
}
}
Classi nodo:
codice:
#ifndef NODE_HPP
#define NODE_HPP
#include <stdio.h>
class Node
{
protected:
int element;
Node* next;
public:
Node(int element);
~Node();
int getElement();
void setElement(int element);
void setNext(Node * next);
Node* getNext();
};
#endif /* NODE_HPP */
codice:
#include "Node.hpp"
Node:: Node(int number)
{
element=number;
next=NULL;
}
Node:: ~Node(){
delete next;
}
int Node:: getElement(){return element;}
void Node::setElement(int number){element=number;}
void Node::setNext(Node* succ){next=succ;}
Node * Node:: getNext(){return next;}
Classe stack:
codice:
#ifndef STACK_HPP
#define STACK_HPP
#include "LinkedList.hpp"
#include "Node.hpp"
#include <string>
using namespace std;
class Stack
{
protected:
LinkedList* lista;
int size;
Node* top;
public:
Stack();
~Stack();
string Top();
int getsize();
void push(int nuovo);
void pop();
string toString();
};
#endif /* STACK_HPP */
codice:
#include "Stack.hpp"
#include <string.h>
#include <sstream>
//#include "LinkedList.hpp"
//#include "Node.hpp"
LinkedList* stack;
Stack::Stack()
{
stack=new LinkedList();
top=stack->getHead();
}
Stack::~Stack(){
delete stack;
}
string Stack::Top()
{
string conv;
stringstream out;
string stampa;
if(stack->getSize()>0)
{
int number=stack->getHead()->getElement();
out << number;
conv=out.str();
stampa+=conv;
}
else
{
stampa="Error!!! Stack is Empty!";
}
return stampa;
}
int Stack::getsize(){return stack->getSize();}
void Stack::pop()
{
if(stack->getSize()>0)
stack->deleteNode();
else
printf("Error!!! Stack is empty");
}
void Stack::push(int nuovo){
stack->addNode(nuovo);
top=stack->getHead();
}
string Stack::toString()
{
string str="[";
int num;
if(stack->getSize()==0) return str+"]";
Node*tmp=stack->getHead();
for(; tmp->getNext()!=NULL; tmp=tmp->getNext())
{
string conv;
stringstream out;
num=tmp->getElement();
out << num;
conv=out.str();
str+=conv+", ";
}
string conv;
stringstream out;
num=tmp->getElement();
out << num;
conv=out.str();
str+=conv+"]";
return str;
}
codice:
#include <cstdlib>
#include "Stack.hpp"
#include <stdio.h>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int op;
Stack *myStack=new Stack();
bool finito=false;
while(!finito)
{
int el;
printf ("Che operazione vuoi eseguire?\n");
cout << "1)push: 2)pop 3)top: 4)Fornisci dimensione: 5)Stampa struttura:\n";
cin >> op;
switch(op)
{
case 1:
cout << "Che numero vuoi inserire?\n" << endl;
cin >> op;
myStack->push(op);
break;
case 2:
myStack->pop();
break;
case 3:
cout <<myStack->Top()<< endl;
break;
case 4:
el=myStack->getsize();
printf("Il numero di elementi e' %d.\n", el);
break;
case 5:
cout <<myStack->toString() << endl;
break;
default:
cout << "Errore...il programma verra' terminato...\n" <<endl;
finito=true;
break;
}
if(finito==false)
{
cout << "Vuoi continuare? 1)si 2)no\n" << endl;
cin >> op;
if(op==2)
finito=true;
else if(op!=1 && op!= 2)
cout<<"Errore!!! Il programma verra' terminato\n"<<endl;
}
}
}