stos.cpp:
#include <iostream>
#include "plik.h"
#include <stdlib.h>
using namespace std;
stack::stack()
{
first=NULL;
}
stack::stack(const stack& old)
{
if(old.first == NULL)
{
first = NULL;
}
else
{
node *wsk = old.first;
node *nwk;
node *lol = new node;
lol->val=wsk->val;
lol->next=NULL;
first = lol;
nwk = first;
wsk=wsk->next;
while(wsk != NULL)
{
while(nwk->next != NULL)
{
nwk = nwk->next;
}
node *tmp = new node;
tmp->val = wsk->val;
tmp->next = NULL;
nwk->next = tmp;
wsk = wsk->next;
}
}
}
stack::~stack()
{
while(first)
{
node* t=first->next;
delete first;
first=t;
};
}
void stack::clear()
{
while(first)
{
node* t=first->next;
delete first;
first=t;
};
}
void stack::push(int a)
{
node* nowy=new node;
nowy->next=first;
first = nowy;
first->val = a;
}
void stack::pokaz()
{
node *tmp;
tmp = first;
if(tmp == NULL)
cout << "stos jest pusty\n";
else
{ cout << "Stos:\n";
for(tmp=first; tmp!=NULL; tmp=tmp->next)
{
cout<< "Wartosc elementu: " << tmp->val << "\n";
}
}
}
int stack::pop()
{
node * p;
int a=0;
p = first;
if(p)
{
first = p->next;
a = p->val;
delete p;
cout << "Usunieto wierzcholek: " << a << "\n";
}
return a;
}
int main()
{
stack *a = new stack;
char c;
int liczba;
stack *g = new stack;
cin >> liczba;
g->push(liczba);
g->pokaz();
cin >> liczba;
a->push(liczba);
cin >> liczba;
a->push(liczba);
g = a;
cin >> liczba;
a->push(liczba);
cin >> liczba;
a->push(liczba);
cin >> liczba;
a->push(liczba);
//stack b(*a);
do
{
cout << "\n1 - dodaj element:\n2 - usun wierzcholek\n3 - wyczysc stos\n4 - pokaz stos\n5 - zakoncz program\n\n";
cin >> c;
switch(c)
{
case '1':
cin >> liczba;
a->push(liczba);
break;
case '2':
a->pop();
break;
case'3':
a->clear();
break;
case'4':
cout <<"stos a:\n";
a->pokaz();
cout << "\n\n";
cout <<"stos b:\n";
//b.pokaz();
cout << "\n\n";
cout <<"stos g:\n";
g->pokaz();
break;
case'5':
delete a;
exit(1);
break;
}
}while(c=5);
return 0;
}
plik.h:
#include <iostream>
using namespace std;
class stack
{
public:
stack();
~stack();
void clear();
void push(int a);
stack(const stack&);
private:
struct node
{
node *next;
int val;
};
public:
void pokaz();
int pop();
node first;
stack& operator=(const stack& old)
{
cout << "dziala";
if(first != NULL)
{
while(first)
{
node t=first->next;
delete first;
first=t;
};
cout << "dziala";
}
if(old.first == NULL)
{
first = NULL;
}else
{
node *wsk = old.first;
node *nwk;
node *lol = new node;
lol->val=wsk->val;
lol->next=NULL;
first = lol;
nwk = first;
wsk=wsk->next;
while(wsk != NULL)
{
while(nwk->next != NULL)
{
nwk = nwk->next;
}
node *tmp = new node;
tmp->val = wsk->val;
tmp->next = NULL;
nwk->next = tmp;
wsk = wsk->next;
}
}
};
};
Mój problem jest następujący wydaje mi sie ze operator "=" nie chce mi się przeciążyć ponieważ gdy piszę g = a; to nie drukuje się napis "działa" oraz nie tworzy się nowy stos tylko dla "g" przypisuje sie adres "a". Po operacji g = a; gdy dodam element do stosu "a" ten element dodaje sie również do stosu "g". Chciałbym aby po wywołaniu "=" stos g się czyścił a na jego miejsce skopiował się głęboko stos "a". podkreślam że konstruktor kopiujący działa bo nie ma takiego problemu jak użyję stack b(*a);
to dla "b" działa spoko i wtedy b jest nowym stosem z wartościami ze stosu "a". Wytłumaczy mi ktoś gdzie popełniam błąd?