| | |
Singly-Linked Lists problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
Add: 45
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
Add: 45
List: 1,45,2,3,4
How do I fix this? This is my code below:
I'm trying to add an element before a certain element.
Example:
List: 1,2,3,4,5
Find: 2
Add: 45
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
Add: 45
List: 1,45,2,3,4
How do I fix this? This is my code below:
C++ Syntax (Toggle Plain Text)
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;
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
But you don't need anything else because nothing is related to anything in the program.
The code is not in english so I would need to translate everything but you don't really need it.
C++ Syntax (Toggle Plain Text)
add_before_element(5, p_beginning);
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
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?
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?
•
•
•
•
Originally Posted by Rashakil Fol
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?
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.
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
•
•
•
•
Originally Posted by Daishi
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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
I'm printing it out this way:
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Link List Sorting Problem
- Next Thread: stl vector - can you delete by position?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






