Hi there.
I have made two classes. Below is Node Class.
Code:
class Node
{
public:
int id;
char name[20];
Node *Next;
};
Below is Link Class.
Code:
class Link
{
private:
Node *Head;
Node *Runner;
Node *Ptr;
public:
Link();
//~Link();
int Add(int id, char name[20]);
void Display();
};
Below is my Add Function. If condition will run only when there is no node exist. Head is pointing to NULL at this time. If there is one or more than one NODES exist, then else condition will be executed.
Code:
int Link::Add(int id, char name[20])
{
if (Head == NULL)
{
Head = new (Node);
Head->id = id;
strcpy(Head->name, name);
Head->Next = Head;
return 0;
}
else
{
Runner = Head;
do
{
Runner = Runner->Next;
Runner = new (Node);
Runner->id = id;
strcpy(Runner->name, name);
Runner->Next = NULL;
Head = Runner;
return -1;
}
while(Runner != NULL);
}
}
Below is my display Code.
Code:
void Link::Display()
{
Runner = Head;
if (Runner != NULL)
{
cout << "Id : " <<Runner->id<<endl;
cout << "Name: " <<Runner->name<<endl;
Runner = Runner->Next;
}
}
This code takes the input and prints only the most recent input. All i want to do is to make a singly linked list which can make 5 or 10 nodes and prints the data of each NODE.
This is my assignment and i am very confused and worried about it. Please help me out.
Regards
Assad