I am using c++ to implement a single link list of integers. My program simply asks the user to fill the list with 5 integers, the program should then delete any even integer and print the list after deletion.
Here is my code:
#include <iostream>
using namespace std;
class IntSLLNode
{
public:
IntSLLNode() { next = 0; }
IntSLLNode(int i, IntSLLNode *ptr = 0)
{
info = i;
next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList
{
public:
IntSLList() {head = tail =0; }
void AddToTail(int);
void DeleteNode(int);
void DisplayList();
void deleteEven();
IntSLLNode * getHead()
{
return head;
}
private:
IntSLLNode *head, *tail;
};
void IntSLList::AddToTail(int el)
{
if (tail != 0) // if list not empty;
{ tail->next = new IntSLLNode(el);
tail = tail->next;
}
else
head = tail = new IntSLLNode(el);
}
void IntSLList::deleteEven()
{
IntSLLNode *current;
current=head;
int num;
while (current!=0)
{
num=current->info;
current=current->next;
if(num%2==0)
{
DeleteNode(num);
}
}
}
void IntSLList::DeleteNode(int el)
{
if(head !=0)
if(head==tail && el==head->info)
{
delete head;
head=tail=0;
}
else if(el==head->info)
{
IntSLLNode *tmp=head;
head=head->next;
delete tmp;
}
else
{
IntSLLNode *pred, *tmp;
for(pred=head, tmp=head->next;
tmp!=0 && !(tmp->info==el);
pred=pred->next, tmp=tmp->next);
if(tmp!=0)
{
pred->next=tmp->next;
if(tmp==tail)
tail=pred;
delete tmp;
}
}
}
void IntSLList::DisplayList()
{
IntSLLNode *current;
current=head;
if(current==0)
cout<<"Empty List!";
while (current!=0)
{
cout<<current->info<<" ";
current=current->next;
}
}
I got Unhandled exception at 0x002c1744 in ex4.exe: 0xC0000005: Access violation reading location 0xfeeefeee in statment int num=current->info; Can anyone suggest how to solve this?