J
jammie_linux
Hi,
Can anybody please tell me that why the following code is getting the
SIGABRT signal when the statement "node = new Node();" in the
append function executes ?
#include <iostream>
#include <string>
#include <new>
using namespace std;
template <class T>
class LinkedList
{
public:
void display();
bool isempty();
void append();
// void prepend();
//void remove();
~LinkedList();
LinkedList();
private:
// template <class T>
class Node
{
public:
Node():data(0),next(0)
{
}
T data;
Node *next;
};
Node * head, *tail;
};
template <class T>
void LinkedList<T> :: display()
{
if(isempty() == true)
{
cout << "List is empty"<<endl;
return;
}
Node *tmp;
tmp = head;
while(tmp != NULL)
cout << tmp->data;
return ;
}
template <class T>
bool LinkedList<T> :: isempty()
{
return head == NULL;
}
template <class T>
void LinkedList<T> :: append()
{
Node * node;
node = new Node(); // The ERROR is in this line.
T data;
cout <<"Enter the data to be entered"<<endl;
cin >>data;
node->data = data;
node->next = NULL;
if(head == NULL) // list was empty.
{
head = tail = node;
return ;
}
else // list was NOT empty.
{
tail->next = node;
tail = tail->next;
}
return ;
}
template <class T>
LinkedList<T>::LinkedList(): head(0), tail(0)
{
cout << "LinkedList created!"<<endl;
}
template <class T>
LinkedList<T>:: ~LinkedList()
{
cout << "~LinkedList() is useless"<<endl;
}
void low_on_memory()
{
cout<<"System low on memory."<<endl;
return;
}
int main(void)
{
set_new_handler(low_on_memory);
int choice;
LinkedList<string> list;
while(true)
{
cout<<"1: Display\n2:Append\n3: Exit\n ";
cin>>choice;
switch(choice)
{
case 1:
list.display();
break;
case 2:
list.append();
break;
case 3:
exit(1);
break;
default:
cout<<"Invalid Choice\n";
break;
}
}
return 0;
}
__________________________________________________________________________
Also, I was just wondering that is there is any use of virtual
function/s in a project being made by a single person and which(project
) will not be used again, not even for adding some more features in it
or to improve the efficiency of the project? The reason behind this
question is that I've heard that virtual function/s are used to replace
the old code by a new one, so if the project will never be used again
then what's the use of using virtual functions ?
Thanks
Can anybody please tell me that why the following code is getting the
SIGABRT signal when the statement "node = new Node();" in the
append function executes ?
#include <iostream>
#include <string>
#include <new>
using namespace std;
template <class T>
class LinkedList
{
public:
void display();
bool isempty();
void append();
// void prepend();
//void remove();
~LinkedList();
LinkedList();
private:
// template <class T>
class Node
{
public:
Node():data(0),next(0)
{
}
T data;
Node *next;
};
Node * head, *tail;
};
template <class T>
void LinkedList<T> :: display()
{
if(isempty() == true)
{
cout << "List is empty"<<endl;
return;
}
Node *tmp;
tmp = head;
while(tmp != NULL)
cout << tmp->data;
return ;
}
template <class T>
bool LinkedList<T> :: isempty()
{
return head == NULL;
}
template <class T>
void LinkedList<T> :: append()
{
Node * node;
node = new Node(); // The ERROR is in this line.
T data;
cout <<"Enter the data to be entered"<<endl;
cin >>data;
node->data = data;
node->next = NULL;
if(head == NULL) // list was empty.
{
head = tail = node;
return ;
}
else // list was NOT empty.
{
tail->next = node;
tail = tail->next;
}
return ;
}
template <class T>
LinkedList<T>::LinkedList(): head(0), tail(0)
{
cout << "LinkedList created!"<<endl;
}
template <class T>
LinkedList<T>:: ~LinkedList()
{
cout << "~LinkedList() is useless"<<endl;
}
void low_on_memory()
{
cout<<"System low on memory."<<endl;
return;
}
int main(void)
{
set_new_handler(low_on_memory);
int choice;
LinkedList<string> list;
while(true)
{
cout<<"1: Display\n2:Append\n3: Exit\n ";
cin>>choice;
switch(choice)
{
case 1:
list.display();
break;
case 2:
list.append();
break;
case 3:
exit(1);
break;
default:
cout<<"Invalid Choice\n";
break;
}
}
return 0;
}
__________________________________________________________________________
Also, I was just wondering that is there is any use of virtual
function/s in a project being made by a single person and which(project
) will not be used again, not even for adding some more features in it
or to improve the efficiency of the project? The reason behind this
question is that I've heard that virtual function/s are used to replace
the old code by a new one, so if the project will never be used again
then what's the use of using virtual functions ?
Thanks