hello guys, if u are kind please explain me why the following function is not correct : void insertAfter(char *); Here is the implementation.
Header File :

#ifndef _LinkedList_
#define _LinkedList_

class node
{
private :
	char *name;
	int age;
public:
	node * next;
	node();
	~node();
	char * getName() const;
	int getAge() const;
	void setName(char *);
	void setAge(int);
	static const int size = 10;
	void displayList();
	void insertAtTheEnd();
	void insertInFrontOfList();
	void insertAfter(char *);
	void insertBefore(char *);
};
#endif

Here is the function :

void node::insertAfter(char *nume)
{
	cout << "Insert after." << endl;
	node * temp;
	node * temp2;
	char tempName[20];
	int tempAge;
	temp = new node;
	cout << "Give the name : ";
	cin >> tempName;
	cout << "Give the age : " ;
	cin >> tempAge;
	temp->setAge(tempAge);
	temp->setName(tempName);
	temp2 = start_ptr;
	if (temp2 == NULL)
	{	cout << "Empty list." << endl;
		start_ptr = temp;
		return;
	}
	else
	{
[B]		while (temp2->getName() != nume)	
		{
			temp2 = temp2->next;
		};[/B]
		if (temp2 == NULL )
		{
			temp2->next = temp;
			temp->next = NULL;
		}
		else
		{
			temp->next = temp2->next;
			temp2->next = temp;
		};
	};
};

The bolded part is were i thing is the problem. For example i have a list with just one element having name = paul. in the main i call the function insertAfter("paul"); The condition is (temp2->getName() != nume) is considerated true. Is there any problem when i compare the two strings ?

Recommended Answers

All 5 Replies

>Is there any problem when i compare the two strings ?
You're not comparing strings, you're comparing pointers to char. What you need to do (assuming you can't switch to the std::string class) is compare using strcmp.

Narue addresses your direct question. But to me it appears that you are confusing nodes with lists. Lists are built with nodes, but they aren't nodes. I think it would be better to separate the two concepts.

For example where does start_ptr come from in the above code? Why should a node have to know where to insert itself or how to display an entire group of nodes? Etc. From my point of view everything from line 17-line 22, inclusive, should be members of a list class, not members of a node class.

Also the logic of and in insertAfter() appears a bit lacking. Suppose your list contains the names Tom in the first node, Harry in the second node and you want to add the name Mary using the insertAfter() function. What are you going to insert Mary after? After Tom? After Harry? After both?

Also the way insertAfter() is set up the while loop will never stop because temp2->name will never equal nume in the scenario I gave above. That means you will eventually overrun the end of the list, hopefully crashing the program in the process. I think you need another way to stop the loop in addition to the one you have set up.

thx guys, strcmp solves the problem.
for Lerner : insertAfter(char *nume) - there is a parameter of function that inidcates where to insert. And yes , I modified a little bit the function to verify when I am at the end of list

hello , guys, i m coming back with another question about linked list implementation :
i m trying to create the next function :

void deleteFirst();

start_ptr is a global variable : node *start_ptr = NULL;
this is the code of the function :

void node::deleteFirst()
{
	node * temp ;
	temp = start_ptr;
	start_ptr = temp->next;
	delete temp;
};

when i test the deleteFirst function i encounter the following error message : HEAP CORRUPTION DETECTED : aftert normal block(#120) at 0x00345C10.

I tried to solve this by implementing the copy constructor (deep copy) and the overload the = operator :

node::node(node & refNode)
{
	name = new char[1+ strlen(refNode.name)];
	strcpy(name,refNode.name);
};

node & node::operator =(const node & refNode)
{
	if (this != &refNode)
	{
		delete [] name;
		name = new char[1 + strlen(refNode.name)];
		strcpy(name,refNode.name);
	};
	return *this;
};

any ideas ? thx in advance

i put here alos the constructor and destructor, i think here is the problem , but do not know where exactly.

node::node()
{
	next = NULL;
	name = new char;
};

node::~node()
{
	delete next;
	delete []name;
};

If you are going to delete (or might otherwise change) the address stored in the first node then you need to send the node/list by reference rather than by value, otherwise you won't be able to reaccess the list later.

void list::deleteFirst(node &* head)
{
   node * temp = head;
   head = head->next;
   delet temp;
}
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.