linked list problem

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class ListBuild
{
private:
	struct node {
		         int d;
			 node * next;  // next pointer
	};
	typedef node * list;  // list == node*
	list head ,tail,prev;           // pointer to head and tail static member
    static int sz;              // current size of list,static member
public:
	ListBuild () ;                    // default constructor 
	ListBuild (const ListBuild & Lst);   // copy constructor
	~ListBuild() ;                   // default destructor 
	int findlength()const;        // find size
	void insert(int newitem);    // add node
	void findpr()const;           // previous pointer that tail
	void remove();  // remove node
        void print();      // print list
};

Implementation file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include "Link_list_build.h"
using namespace 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;
	   else if(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;
			 else if (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"
using namespace 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.

thanks
kul
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..
hi,
ListBuild a,b,c ;

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?

thanks
kul
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.
1
2
3
4
5
6
7
ListBuild a;
a.insert(10);
a.print();
a.insert(20);
a.print();
a.insert(30);
a.print();
should work.
hi hamsterman,

thanks a ton. I understood. with three declarations, i was calling default constructor three times, and creating 3 lists.

thanks again'
kulsub
Topic archived. No new replies allowed.