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.