Me again, this time I really have a problem.
I'm trying to add an element before a certain element.
Example:
List: 1,2,3,4,5
Find: 2
List: 1,45,2,3,4,5

Now the thing is that when I find the number and try to add a new number it adds OK but then it deletes the last element of the list.
Example:
List: 1,2,3,4,5
Find: 2
List: 1,45,2,3,4

How do I fix this? This is my code below:

``````node* p_before = find_element(number, k_beginning);	//return a pointer to an element we're trying to find
node* p_after = p_before->k_next;

node* k_new= new node;
k_new->number= number;
k_new->k_next= k_after;
p_before->k_next= k_new;``````

## All 8 Replies

I, for one, generally do much better at answering questions if the code posted is complete, yet small and fully compileable, and demonstrates the problem.

But you don't need anything else because nothing is related to anything in the program.

``add_before_element(5, p_beginning);``
``````void add_before_element(int number, node*& p_beginning)
{
node* p_before = find_element(number, k_beginning);	//return a pointer to an element we're trying to find
node* p_after = p_before->p_next;

node* p_new= new node;
p_new->number= number;
p_new->p_next= p_after;
p_before->p_next= p_new;
}``````

The code is not in english so I would need to translate everything but you don't really need it.

``node* p_before = find_element(number, k_beginning);``

What is k_beginning? Is it a node? Is it a number? Is it suppose to be p_beginning?

In any case, that code shouldn't truncate the end of the list. But it still looks like bad code, what happens when you try to add before the first element?

-Fredric

First of all, you passed p_beginning to the function and then tried to use k_beginning. Your code compiles?

Second, you expect us to have a crystal ball that can read your mind. What does "add_before_element" mean? Do you want to add before the element containing 'number'? Or do you want to add before the numberth element? Without explaining at all what find_element tries to do, this is still unknown.

And how in the world do you expect to insert the number 42 before the element either containing 2 or lying at the second position, using a function that gets passed only one integer?

First of all, you passed p_beginning to the function and then tried to use k_beginning. Your code compiles?

Second, you expect us to have a crystal ball that can read your mind. What does "add_before_element" mean? Do you want to add before the element containing 'number'? Or do you want to add before the numberth element? Without explaining at all what find_element tries to do, this is still unknown.

And how in the world do you expect to insert the number 42 before the element either containing 2 or lying at the second position, using a function that gets passed only one integer?

Like I said, the code is not in english and I ruffly translated those pointers, don't mind the minor errors.
I really don't know what's so hard to understand? :)

I'd like to add an element BEFORE the specific element in the list.
add_before_element() is exactly what I'm trying to do, I explained that before.The first CODE quote was a simple call to the CODE quote below so you could picture things better passing a NUMBER I'd like to add and a pointer to the beginning.I'll explain once again for all of you who don't understand.

``add_before_element(5, p_beginning);   //This is a line somewhere in the program which calls a function for adding an element before a certain element passing a number and pointer to the beginning``
``````void add_before_element(int number, node*& p_beginning)   //This is a function that is being called
{
node* p_before = find_element(number, k_beginning);	//There is another function called find_element() for finding an element and instead of the element it returns a pointer to that element and that pointer is called p_before
node* p_after = p_before->p_next;   //Now, since the p_before is a pointer to the already found element, we make p_after a poitner to the next element of the found one -> p_before->p_next

node* p_new= new node;   //We make a new node called p_new
p_new->number= number;  //Now we insert a number (5) in the newly created node
p_new->p_next= p_after;  //And now we link the newly created node
p_before->p_next= p_new;  //Same goes here
}``````

The thing is that it adds a number fine but then it deletes the last number in the node.Here is the sample output:

``````Adding a number before a certain number
Our list contains: 5, 3, 4, 8, 1, 2
Before what element would you like to insert a number? 1
What element would you like to insert? 45
Our list after the operation: 3, 4, 8, 1, 45, 2``````

I also did the add_after_element() and it works fine.

``node* p_before = find_element(number, k_beginning);``

What is k_beginning? Is it a node? Is it a number? Is it suppose to be p_beginning?

In any case, that code shouldn't truncate the end of the list. But it still looks like bad code, what happens when you try to add before the first element?

-Fredric

Hmm, it looks like it adds after the element.Here is the sample output:

``````Our list: 3, 2, 1 (I first input 1 then 2 then 3)
Element you'd like to add before? 1
Number: 45
Our list: 2, 45, 1``````

How are you printing out the list?

-Fredric

I'm printing it out this way:

``````void print_out(node*& p_beginning)
{
node* p_print = p_beginning;

while (p_print != 0)
{
cout << p_print->number << " ";
p_print = p_print->p_next;
}
cout << endl;
}``````
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.