Hi,
I make a try again on Link List in Visual C++ but got an error. The code is looking right but when I debugged the source, then I realize that when the LINKLIST constructor is called then the two Node type pointers *start and *last creates trouble. Please do help me.

1. Data Class

#include <string>

using namespace std;

 class Data{

		private:

			string name;

		public:

			void setData(string);
			 string getData();
};

 void Data::setData(string name){

		this->name=name;
 }

 string Data::getData(){

	 return (name);
 }

2. Node Class

#include "Data.h"

class Node {

	private:

		Data data;
		Node *next;

	public:

		Node();
		Node(Data,Node*);

		void setNodeData(Data);
		void setNext(Node*);
		Data getNodeData();
		Node* getNext();
};

Node::Node(){

	next=NULL;
}

Node::Node(Data data, Node *next){

	this->data=data;
	this->next=next;

}

void Node::setNodeData(Data data){
		
		this->data=data;
}

void Node::setNext(Node *next){

		this->next=next;

}

Data Node::getNodeData(){

	return (data);
}

Node* Node::getNext(){

	return (next);
}

3. Link List Class

#include <iomanip>
#include <iostream>
#include "Node.h"

using namespace std;

class LinkList{

		private:

			Node *start,*last;

		public:

			LinkList();

			void setFirst(Node*);
			void setLast(Node*);
			Node* getFirst();
			Node* getLast();
			Node* createNode(Data);
			void createList(Node*);
			void showList();
};


int main(){

	LinkList myList;
	LinkList();
	Data myData;
	
	myData.setData("Shahab");

	myList.createList(myList.createNode(myData));

	myList.showList();


	return 0;
}

LinkList::LinkList(){
		
	start=NULL;
	last=NULL;
}

void LinkList::setFirst(Node *newNode){
		
			start=newNode;
}

void LinkList::setLast(Node *newNode){
	
			last=newNode;
}

Node* LinkList::getFirst()
{
	return (start);
}

Node* LinkList::getLast(){

	return (last);
}

Node* LinkList::createNode(Data data){

	Node *newNode=new Node(data,NULL);

	return newNode;
}

void LinkList::createList(Node *newNode){

	if(this->getFirst==NULL){
		
		this->setFirst(newNode);
		this->setLast(newNode);
	}

	else if (this->getFirst()==this->getLast()){

		this->getFirst()->setNext(newNode);
		this->setLast(newNode);
	}
	else{


		Node *temp=this->getFirst();

		while(temp->getNext()!=NULL){

			temp=temp->getNext();
		}

		temp->setNext(newNode);
		this->setLast(newNode);

	}
}

void LinkList::showList(){

	if(this->getFirst()==NULL){

		cout<<"List is Empty"<<endl;
	}
	else
	{
		Node *temp=this->getFirst();

		while(temp!=NULL)
		{
			cout<<"This is the Node:  "<<temp->getNodeData().getData()<<endl;

			temp=temp->getNext();
		}
	}
}

Recommended Answers

All 2 Replies

That's a lot of code with very little explanation of the problem. Also, too many tabs and blank lines, IMHO.

What error message did you get?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.