I've got a linked list class and for some reason when I increment an int in one object e.g linked1.size for some reason linked2.size also increments!
And ideas why this is? I didn't intentionally make it a static variable.
my code:
main()
{
Vlist v1;
v1.add(1,0);
v1.add(2,0);
Vlist v2;
}
Incrementing the size member variable occurs in the add()
function like this:
(*this).size++;
The result should be the v1.size==2 and v2.size==0, but instead v2.size==2!
This problem has been driving me crazy for hours- any help would be really appreciated!
The add function is as follows:
int Vlist::quietAdd(Vertex new_vertex, int i_loc)
{
Vnode* temp= first_node;
Vnode* newNode= NULL;
//check for unique
if (find(new_vertex)!=999)
return 0;
//check for non-negative i value
if (i_loc<0)
{
cout<<"Invalid index."<<endl;
return 0;
}
//and exit here?
else
{
temp = find(i_loc);
newNode= new Vnode();
if (size==0)
first_node= newNode;
//assigning to the new vnode the new Vertex value
(*newNode).updateVertex(new_vertex.getInt());
//the nxt pointer now points to the value it's replacing or NULL
(*newNode).updateNextPoint(temp);
if ((temp==NULL)&&size!=0)
{
//size-1 is used to get the pointer to the last value on the list
(*newNode).updatePrevPoint(find(size-1));
(*find((size-1))).updateNextPoint(newNode);
}
if (temp !=NULL)
{
//the new vnode's prev pointer now points to correct location
(*newNode).updatePrevPoint((*temp).getPrevPoint());
if ((*temp).getPrevPoint()!=NULL)
/*the vnode that used to point to the existing vnode now
points to new vnode*/
(*((*temp).getPrevPoint())).updateNextPoint(newNode);
//the old one's prev pointer points back to the new value
(*temp).updatePrevPoint(newNode);
}
/*if we've just put a new vnode at the start then it should be
pointed to by the "first vnode" pointer*/
if (i_loc==0)
first_node=newNode;
(*this).size++;
}
return 1;
}
//Vlist class definition
class Vlist
{
private:
int size;
Vnode* first_node;
public:
//copy constructor
Vlist(const Vlist& vl2):size(vl2.size), first_node(NULL)
{
for (int i=0;i<size;i++)
quietAdd(vl2.read(i),i);
}
Vertex getNext();
Vlist (): size(0) { first_node=0; }
~Vlist (){};//make deep!
bool empty();
Vnode* find(int i_loc) const;
int find(Vertex target)const;
void add(Vertex new_vertex, int i_loc);
int quietAdd(Vertex new_vertex, int i_loc);
Vertex remove(int i_loc);
Vertex read(int i_loc) const;
Vnode* getFirstNode() {return first_node;}
int getSize() const { return size;}
void setSize(int newSize) { size = newSize;}
char* print() const;
void delete_List();
};
class Vnode
{
private:
Vertex vertex;
Vnode* prev_node;
Vnode* nxt_node;
public:
Vnode ()
: prev_node(0), nxt_node(0)
{
vertex.update(0);
}
~Vnode (){}; //destructor
Vertex getVertex(){return vertex;}
int getVertexInt() { return vertex.getInt();}
Vertex getNext(){return (*nxt_node).getVertex();}
Vnode* getNextPoint() { return nxt_node; }
Vnode* getPrevPoint() { return prev_node; }
void updateNextPoint(Vnode* newP) { nxt_node = newP;}
void updatePrevPoint(Vnode* newP) {prev_node= newP;}
void updateVertex(Vertex vertexVal) {vertex.update(vertexVal.getInt());}
};