hi guys. I created a simple code to build linked list.The use enters three data items , and these form a 3 elements single link list.
following are the various files
#include <iostream>
#include "Link_list_build.h"
usingnamespace std;
int ListBuild::sz=0;
ListBuild ::ListBuild() // default constructor
{
head=0; //Null pointer
tail=0;
prev=0;
}
ListBuild ::ListBuild(const ListBuild& lst) // copy constructor
{
}
ListBuild ::~ListBuild() // default destructor
{
remove() ; // call func to remove node memory
}
int ListBuild::findlength()const // determine current size of list
{
return sz;
}
void ListBuild ::insert(int newitem) // add item at start or inbetween
{
//int newlngth= findlength()+1;
// creates new node structure and returns pointer to it
list newnode = new node ;
if( newnode ==NULL)
cout<< " could not allocate memory";
else
{
sz= findlength();
newnode->d = newitem;
newnode->next=NULL;
if (sz==0) // allocate first node
{
head= newnode;
tail=head;
}
else // allocate rest nodes
{
tail->next = newnode;
tail=newnode;
}
sz++;
} // end of if( newnode ==NULL)
}
void ListBuild::findpr()const
{
if (sz==0)
cout<< "already empty list"<<endl;
elseif(sz==1)
{ } // do nothing
else
{
list prev = head;
for (int i=0;i<sz-1;i++)
{
prev= prev->next;
}
}
}
void ListBuild ::remove() // remove item
{
--sz;
if (sz==0)
cout<< "already empty list"<<endl;
elseif (sz==1) // delete first node
{
delete tail;
tail=NULL;
}
else // delete rest nodes
{
list curr;
curr= prev->next; // now points to node be deleted
prev->next = curr->next;
curr=NULL;
delete curr;
}
}
void ListBuild ::print()
{
list temp=NULL;
temp=head; // dont want to change head pointer, so create another temp //pointer
for (int i=0;i<sz;i++)
{
cout<< temp->d<<'\t';
temp=temp->next;
}
}
and the user file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "Link_list_build.h"
usingnamespace std;
int main()
{
ListBuild a,b,c ;
a.insert(10); // insert at start
a.print();
b.insert(20); // insert at end and build list
b.print();
c.insert(30);
c.print();
}
the code compiles and allocates first node. but, as i pass second node data, it reaches
tail->next = newnode;
within "insert" function and gives memory access violation.
any ideas why? I do not see why it should.
You have three lists here that share a single static sz. The second list finds sz=1 and tries to access tail->next, while tail doesn't point to anything. Why would you make it static? Clearly, that is a property of a single list..
this should mean single list, whose three instances or 3 objects are created(each object = each node).
or does it mean that i am creating three objects of three lists?
I think I am lost between objects and classes.
also, if i make sz as non static, how would i pass three data elements to the insert function,
if creating a,b,c creates three lists rather than three elements for single list?
ListBuild a,b,c; declares three lists the same way int a,b,c; would declare three integers.
Nodes are added in insert(), you don't need to declare them anywhere.