Hi,
I am using Visula C++ and building LinkedList. But I am studk.
Please look at this if goes something wrong. Please mention it. Their are are two classes I am using for it. The first it Data Class and other one is Node class. Note that both classes are in separate files. Here is the Code.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Data
{
private:
string name;
public:
void setName(string);
string getName();
};
void Data::setName(string str)
{
name=str;
}
string Data::getName()
{
return (name);
}
.........................................................................................................
#include <iostream>
#include <iomanip>
#include <string>
#include "Data.h"
using namespace std;
class Node
{
private:
Data obj;
Node *link,*start,*last;
public:
Node()
{
link=NULL;
start=NULL;
last=NULL;
};
void setLink(Node*);
Node* getLink();
void setStart(Node*);
Node* getStart();
void setLast(Node*);
Node* getLast();
Node* createNode(string);
void insertNode(Node*);
void displayNodes();
};
int main()
{
Node myObj;
myObj.insertNode(myObj.createNode("Shahab"));
myObj.insertNode(myObj.createNode("Guldan"));
myObj.displayNodes();
return 0;
}
Node* Node::createNode(string str)
{
Node *newNode=new Node();
newNode->obj.setName(str);
newNode->setLink(NULL);
return newNode;
}
void Node::insertNode(Node *newNode)
{
if(getStart() && getLast()==NULL)
{
setStart(newNode);
setLast(newNode);
}
else
{
Node *temp;
temp=getStart();
while(temp->getLink()!=NULL)
{
temp=temp->getLink();
}
temp->setLink(newNode);
setLast(newNode);
}
}
void Node::displayNodes()
{
Node *temp;
temp=getStart();
while(temp->getLink()!=NULL)
{
cout<<"This is the List: "<<temp->obj.getName();
temp=temp->getLink();
}
}
void Node::setLink(Node *lnk)
{
link=lnk;
}
Node* Node::getLink()
{
return(link);
}
void Node::setStart(Node *newNode)
{
start=newNode;
}
Node* Node::getStart()
{
return (start);
}
void Node::setLast(Node *newNode)
{
last=newNode;
}
Node* Node::getLast()
{
return (last);
}
Thanks