My overloaded operator isn't initializing the value passed through it. Please help!!! and yes I am aware that my insert function isn't complete...

#include <iostream>
using namespace std;

struct Node{
    int value;
    Node * next;
};

class linkedList {
 
public: 
             linkedList () {first=0; length=0;}
             bool operator > (int a) ;
             void displayList ();
             bool  Insert (int i, int a);
private: 
			 Node* first;
             int length;  
};

int main()
{
	linkedList L;
	L>6;
	//L>9;
//	L>3;
//	L>9;
	L.displayList();
	return 0;
}

bool linkedList::operator > (int a)
{
	if (length==0)
	{
		first->value=a;
		first->next=0;
		length++;
		return true;
	}
	else
	{
		Node* newn;
		Node* temp;
		newn=new Node;
		temp=new Node;
		for (int i=0; i<=length; i++)
		{
			if (i=0)
				temp=first->next;
			if (i<length)
				temp=temp->next;
			if (i=length)
			{
				temp->next=newn;
				newn->value=a;
				newn->next=0;
				length++;
			}
		}
		delete temp;
		return true;
	}
	return false;
}

void linkedList::displayList()
{
	Node* temp;
	temp = new Node;
	cout << endl << "The elements of the linked list are: ";
	for (int i=0; i<=length; i++)
	{
		if (i=0)
		{
			cout << first->value << " ";
			temp=first->next;
		}
		else 
		{
			cout << temp->value << " ";
			temp=temp->next;
		}
	}
	cout << endl;
}

bool linkedList::Insert(int i, int a)
{
	return true;
}

Recommended Answers

All 10 Replies

Ok I now have the overloaded operator implementing the virst integer however if I try to add more Nodes I recieve a runtime error.

I changed up some of the code in the overloaded operator and display function.

#include <iostream>
using namespace std;

struct Node{
    int value;
    Node * next;
};

class linkedList {
 
public: 
             linkedList () {first=0; length=0;}
             bool operator > (int a) ;
             void displayList ();
             bool  Insert (int i, int a);
private: 
			 Node* first;
             int length;  
};

int main()
{
	linkedList L;
	L>6;
//	L>9;
//	L>3;
//	L>9;
	L.displayList();
	return 0;
}

bool linkedList::operator > (int a)
{
		Node* newn;
		newn=new Node;
	if (length==0)
	{
		first=newn;
		first->value=a;
		first->next=0;
		length++;
		return true;
	}
	else
	{
		Node* temp;
		temp=new Node;
		for (int i=1; i<=length; i++)
		{
			if (i=1)
				temp=first;
			if (i<length)
				temp=temp->next;
			if (i=length)
			{
				temp->next=newn;
				newn->value=a;
				newn->next=0;
				length++;
			}
		}
		delete temp;
		return true;
	}
	return false;
}

void linkedList::displayList()
{
	Node* temp;
	temp = new Node;
	cout << endl << "The elements of the linked list are: ";
	for (int i=1; i<=length; i++)
	{
		if (i=1)
		{
			cout << first->value << " ";
			temp=first->next;
		}
		else 
		{
			cout << temp->value << " ";
			temp=temp->next;
		}
	}
	cout << endl;
}

bool linkedList::Insert(int i, int a)
{
	return true;
}

In Line 49, 53 you seem to be using = in the place of == the same case again in 73. Apart from those.

The code in line 51 is always true and will be executed. I think you should think on what you are trying to do .

In Line 49, 53 you seem to be using = in the place of == the same case again in 73. Apart from those.

The code in line 51 is always true and will be executed. I think you should think on what you are trying to do .

Thank you for bringing those stupid mistakes to my attention. I always forget ==... but What did you mean about line 51 always being true?

Oops i meant 52, the if statement would always return true over there because of the for loop that it is bound in.

I see that you are trying to add the element into the linked list at the end.

So, a simpler implementation would be to use the following algorithm.,

#1 : Start from first;
##1: If first ==0 create a new node,
##2:set first->next = null and enter value into first->value.
#2 Else if first is not equal to 0; take a temporary pointer.
##2: start a while loop.
## 3: while temp->next!=0;
## 4: let temp=temp->next;

and when the condition is true. then you can go about inserting your node there. That approach is rather simpler and doesn't use the length variable. However the length variable can be incremented just in-case.

ok I changed it as you said and now the first value does not implement any longer...

#include <iostream>
using namespace std;

struct Node{
    int value;
    Node * next;
};

class linkedList {
 
public: 
             linkedList () {first=NULL; length=0;}
             bool operator > (int a) ;
             void displayList ();
             bool  Insert (int i, int a);
private: 
			 Node* first;
             int length;  
};

int main()
{
	linkedList L;
	L>6;
//	L>9;
//	L>3;
//	L>9;
//	L.displayList();
	return 0;
}

bool linkedList::operator > (int a)
{
		Node* newn;
		newn=new Node;
	if (length==0)
	{
		first=newn;
		first->value=a;
		first->next=NULL;
		length++;
		return true;
	}
	else
	{
		Node* temp;
		temp=first;
		while (temp->next!=0)
		{
				temp=temp->next;
		}
		temp->value=a;
		temp->next=NULL;
		length++;
		delete temp;
		return true;
	}
	return false;
}

void linkedList::displayList()
{
	Node* temp;
	temp = first;
	cout << endl << "The elements of the linked list are: ";
	while (temp->next!=0)
	{
		cout << temp->value << " ";
		temp=temp->next;
	}
	cout << endl;
}

bool linkedList::Insert(int i, int a)
{
	return true;
}

The Problem is with your display function.
the while (temp->next!=NULL) is causing it
instead while (temp!=NULL) is the correct way of displaying elements..

EDIT:
Apart from that there are major problems in the else part, Examine carefully on what you are trying to do :)

That was probably one of many problems... I fixed that and still nothing... It's probably something stupid that the 2 of us are over looking but the first value now implements and any extra does not display. Could it be my display function?

#include <iostream>
using namespace std;

struct Node{
    int value;
    Node * next;
};

class linkedList {
 
public: 
             linkedList () {first=NULL; length=0;}
             bool operator > (int a) ;
             void displayList ();
             bool  Insert (int i, int a);
private: 
			 Node* first;
             int length;  
};

int main()
{
	linkedList L;
	L>6;
	L>9;
//	L>3;
//	L>9;
	L.displayList();
	return 0;
}

bool linkedList::operator > (int a)
{
		Node* newn;
		newn=new Node;
	if (length==0)
	{
		first=newn;
		first->value=a;
		first->next=NULL;
		length++;
		return true;
	}
	else
	{
		Node* temp;
		temp=first;
		while (temp!=NULL)
		{
				temp=temp->next;
		}
		temp=newn;
		temp->value=a;
		temp->next=NULL;
		length++;
		delete temp;
		return true;
	}
	return false;
}

void linkedList::displayList()
{
	Node* temp;
	temp = first;
	cout << endl << "The elements of the linked list are: ";
	while (temp!=NULL)
	{
		cout << temp->value << " ";
		temp=temp->next;
	}
	cout << endl;
}

bool linkedList::Insert(int i, int a)
{
	return true;
}

The problem was that we need to point to the last element of the list . and then insert a node. Your code would reach beyond the End of the last element and end up pointing nowhere :)

This is rather a better implementation.

else
	{
		Node* temp;
		temp=first;
		while (temp->next!=NULL)
		{
				temp=temp->next;//Traversing till end.
		}
		temp->next=newn;//Setting the pointer to point newn
		newn->value=a;//Setting value for newn
		newn->next=NULL;//Setting null
		length++;
		//delete temp;
		return true;
	}

Thank you!!! thank you!!! Such silly little mistakes xD

Hey, dont forget to write a destructor to the linkedlist class to free the space taken up. Else it will cause major memory leaks :)

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.