What is wrong here?

Hello guys! Please take a look at this program; I do not know what the actual problem is!
You forgot a semicolon.
 
;

There.
Last edited on
I'm having a problem posting the problem! I'll have this resolved soon.


OK now, I'm back!
The posting problem I was having earlier (at work) was due to the length of my program, the editor refused to load it! So now, I have decided to split the program into two so that it can be loaded on here.

I have two classes as shown in the program below: one specifies the structure of the node of a linked-list; the other defines the linked-list. The problem I am having concerns the statements (annotated with comments) in line 58 and line 59 in function main(). With only the statement in line 58, the program terminates properly; but once I add another insertLast() statement, as shown, in line 59, the program ends abruptly (pretty much "hanging-up"). The question I have is :what is really going on here?


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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
using namespace std; 


template<class elemType>
class nodeType
{
public:
	const nodeType<elemType>& operator=(const nodeType<elemType>&);
        void setInfo(const elemType& elem);
	elemType getInfo() const;
	void setLink(nodeType<elemType>* ptr);
	nodeType<elemType>* getLink() const;
	nodeType();
	nodeType(const elemType& elem, nodeType<elemType>* ptr);
	nodeType(const nodeType<elemType>& otherNode);
	~nodeType();
	
private:
	elemType info;
	nodeType<elemType>* link;
};



template<class elemType>   		
class linkedListType
{
public:
    const linkedListType<elemType>& operator=(const linkedListType<elemType>&);      //overloading the assignment operator
    void initializeList();
    bool isEmptyList();
    void print();        
    int length() const;	   
    void destroyList();
    void insertLast(const elemType& newItem);
    linkedListType();                                                  //default constructor   
    linkedListType(const linkedListType<elemType>& otherList);         //copy constructor
    ~linkedListType();    											   //destructor
	                                             
	     
protected:                             
    int count; 						
    nodeType<elemType>* first;            
    nodeType<elemType>* last;           

private:
    void copyList(const linkedListType<elemType>& otherList);	
};




int main()
{
    linkedListType<int> myList; 

    myList.insertLast(11);		//program exits correctly when it is only this statement   
    myList.insertLast(49);		//when this statement is included, program does not exit properly; a value other than 0 is returned by main()
       
	// wait until user is ready before terminating program
    system("PAUSE");
    return 0;
}



//---------------- class nodeType member functions' definitions ---------------------------

template<class elemType>
const nodeType<elemType>& nodeType<elemType>::operator=(const nodeType<elemType>& otherNode)
{
	if (this != &otherNode)
	{
		if (link != NULL)
		    delete link;
		    
		info = otherNode.info;    
		link = new nodeType<elemType>; 
		link = otherNode.link;		
	}  
	return *this;
}


template<class elemType>
nodeType<elemType>::nodeType(const nodeType<elemType>& otherNode)
{
	if (link != NULL)
        delete link;
		    
	info = otherNode.info;    
	link = new nodeType<elemType>; 
	link = otherNode.link;		
}



template<class elemType>
nodeType<elemType>::~nodeType()
{
	delete link;
}


template<class elemType>
nodeType<elemType>::nodeType(const elemType& elem, nodeType<elemType>* ptr)
{
    info = elem;
    link = ptr;
}



template<class elemType>  
nodeType<elemType>::nodeType()
{
    info = elemType();
    link = NULL;
}



template<class elemType>
void nodeType<elemType>::setLink(nodeType<elemType>* ptr)
{
	link = ptr;		
}



template<class elemType>
nodeType<elemType>* nodeType<elemType>::getLink() const
{
	return link;
}



template<class elemType>
void nodeType<elemType>::setInfo(const elemType& elem)
{
	info = elem;
}



template<class elemType>
elemType nodeType<elemType>::getInfo() const
{
	return info;
}


Last edited on
Here is the remainder of the program: it's just the definitions of the member functions of class linkedListType.


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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//-------------- class linkedListType member functions' definitions ------------

template<class elemType>
bool linkedListType<elemType>::isEmptyList()
{
    return(first == NULL);
}


template<class elemType>
linkedListType<elemType>::linkedListType()         //default constructor
{
    count = 0;
	first = NULL;
    last = NULL;
}


template<class elemType>
void linkedListType<elemType>::destroyList()
{
     nodeType<elemType> *temp;       //pointer to deallocate the memory occupied by the node
     
     while(first != NULL)            //while there are nodes in the list
     {
         temp = first;               //set temp to the current node
         first = first->getLink();        //advance first to the next node
         delete temp;                //deallocate the memory occupied by temp
     }
     last = NULL;                    //initialize last to NULL; first has already been set to NULL
                                     //by the while loop
     count = 0; 
}


template<class elemType>
void linkedListType<elemType>::initializeList()
{
     destroyList();                  //if the list has any nodes, delete them
}



template<class elemType>
void linkedListType<elemType>::print()
{
     nodeType<elemType> *current;       //pointer to traverse the list
     
     current = first;                   //set current so that it points to the first node
     
     while (current != NULL)            //while there is more data to print
     {
         cout<<current->getInfo()<<"  ";     //forced the overloading of the << operator for extPersonType objects
         current = current->getLink();       //advance pointer to the next node (mine!)
     }     
}



template<class elemType>
int linkedListType<elemType>::length() const
{    
     return count;
}


  
    //destructor
template<class elemType>
linkedListType<elemType>::~linkedListType()
{
     destroyList(); 
}



    //copy the list	   
template<class elemType>
void linkedListType<elemType>::copyList(const linkedListType<elemType>& otherList)
{
     nodeType<elemType> *newNode;              //pointer to create a node
     nodeType<elemType> *current;              //pointer to traverse the list
     
     if (first != NULL)						   //if the list is nonempty, make it empty; DSUC++
         destroyList(); 
     
     if (otherList.first == NULL)              //otherList is empty
     {
         first = NULL;
         last = NULL;
         count = 0; 		//DSUC++
     } 
     else
     {
         current = otherList.first;            //current points to the list to be copied
         
         count = otherList.count;              //DSUC++
         
             //copy the first node
         first = new nodeType<elemType>;       //create the (first (MINE)) node
         first->info = current->info;          //copy the info
         first->link = NULL;                   //set the link field of the node to NULL
         last = first;                         //make last point to the first node (now both point to the same node (MINE))
         current = current->link;              //make current point to the next node
         
             //copy the remaining list
         while (current != NULL)
         {
             newNode = new nodeType<elemType>;        //create a node
             newNode->info = current->info;           //copy the info
             newNode->link = NULL;                    //set the link of newNode to NULL
             last->link = newNode;                    //attach newNode after last
             last = newNode;                          //make last point to the actual last node
             current = current->link;                 //make current point to the next node (still of object otherList (MINE))
         }// end while
     }//end else
}



    //copy constructor
template<class elemType>
linkedListType<elemType>::linkedListType(const linkedListType<elemType>& otherList)
{
	first = NULL;
	copyList(otherList);
}


 
    //overloading the assignment operator
template<class elemType>
const linkedListType<elemType>& linkedListType<elemType>::operator=(const linkedListType<elemType>& otherList)
{
     if (this != &otherList)		//avoid self-copy
     {
         copyList(otherList);
	 }
     
     return *this;
}



template<class elemType>
void linkedListType<elemType>::insertLast(const elemType& newItem)
{
     nodeType<elemType> *newNode;             //pointer to create the new node
     
     newNode = new nodeType<elemType>;        //create the new node               
     newNode->setInfo(newItem);               //store the new item in the node
     newNode->setLink(NULL);                  //set the link field of newNode to NULL
         
     if (this->first == NULL)                 //if the list is empty, ...
     {    
         this->first = newNode;               //...newNode is both the first and 
         this->last = newNode;                //...last node
         this->count++;						      //increment count
     }
     else               //the list is not empty, insert newNode after last
     {
         this->last->setLink(newNode);          //insert newNode after last
         this->last = newNode;                //make last point to the actual last node in the list
         this->count++;						      //increment count
     }
}
The easiest is to set a breakpoint in the destroyList function and step through it.
One problem I found was
1
2
3
4
5
6
while (first != NULL)            //while there are nodes in the list
  {
    temp = first;               //set temp to the current node
    first = first->getLink ();        //advance first to the next node
    delete temp;                //deallocate the memory occupied by temp
  }


The second time it was called first->getLink() returned an invalid pointer and when it tried to delete it it crashed.
Topic archived. No new replies allowed.