B
bejiz
Hello,
I have written a short program for practising linked lists. But there
is surely something wrong for when I compile there is a unhandled
exception and it does not print if I try to add more than one element
to the list. Is this a problem with the function of the internal class
Node ?
Thanks for any help.
Here is the code:
#include<iostream>
using namespace std;
class UseNodes
{
private:
class Node
{
public:
Node()
{
Data = 1;
Next = 0;
}
Node(int a)ata(a){}
~Node(){};
Node* GetNext()const{ return Next;}
void SetNext( Node* A){ Next = A;}
int GetData()const{ return Data;}
void setData(int a) { Data = a;}
private:
Node* Next;
int Data;
};
Node* Head;
Node* Tail;
public:
UseNodes(){Head = 0; Tail = 0; }
~UseNodes(){};
void AddNode(int a)
{
Node* Temp = new Node(a);
if(Head==0)
{
Head = Temp;
Tail = Temp;
}
else
{
Node* Current = new Node();
Current = Head;
while(Current)
{
Current = Current->GetNext();
}
Current->SetNext(Temp);
Tail = Temp;
}
}
void Print()const
{
int a = 2;
Node* Temp = new Node(a);
if(Head==0)
cout << " Empty list.\n " ;
else
{
Temp = Head;
cout << Head->GetData() << endl;
while(Temp!=Tail->GetNext())
{
cout << Temp->GetData() << endl;
Temp = Temp->GetNext() ;
}
}
}
};
int main()
{
UseNodes A;
A.AddNode(29);
A.AddNode(7777);
A.Print();
getchar();
return 0;
}
I have written a short program for practising linked lists. But there
is surely something wrong for when I compile there is a unhandled
exception and it does not print if I try to add more than one element
to the list. Is this a problem with the function of the internal class
Node ?
Thanks for any help.
Here is the code:
#include<iostream>
using namespace std;
class UseNodes
{
private:
class Node
{
public:
Node()
{
Data = 1;
Next = 0;
}
Node(int a)ata(a){}
~Node(){};
Node* GetNext()const{ return Next;}
void SetNext( Node* A){ Next = A;}
int GetData()const{ return Data;}
void setData(int a) { Data = a;}
private:
Node* Next;
int Data;
};
Node* Head;
Node* Tail;
public:
UseNodes(){Head = 0; Tail = 0; }
~UseNodes(){};
void AddNode(int a)
{
Node* Temp = new Node(a);
if(Head==0)
{
Head = Temp;
Tail = Temp;
}
else
{
Node* Current = new Node();
Current = Head;
while(Current)
{
Current = Current->GetNext();
}
Current->SetNext(Temp);
Tail = Temp;
}
}
void Print()const
{
int a = 2;
Node* Temp = new Node(a);
if(Head==0)
cout << " Empty list.\n " ;
else
{
Temp = Head;
cout << Head->GetData() << endl;
while(Temp!=Tail->GetNext())
{
cout << Temp->GetData() << endl;
Temp = Temp->GetNext() ;
}
}
}
};
int main()
{
UseNodes A;
A.AddNode(29);
A.AddNode(7777);
A.Print();
getchar();
return 0;
}