944,009 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2564
  • C++ RSS
Sep 2nd, 2005
0

Singly-Linked Lists problem

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. node* p_before = find_element(number, k_beginning); //return a pointer to an element we're trying to find
  2. node* p_after = p_before->k_next;
  3.  
  4. node* k_new= new node;
  5. k_new->number= number;
  6. k_new->k_next= k_after;
  7. p_before->k_next= k_new;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsdpowa is offline Offline
21 posts
since Aug 2005
Sep 3rd, 2005
0

Re: Singly-Linked Lists problem

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 3rd, 2005
0

Re: Singly-Linked Lists problem

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

C++ Syntax (Toggle Plain Text)
  1. add_before_element(5, p_beginning);

C++ Syntax (Toggle Plain Text)
  1. void add_before_element(int number, node*& p_beginning)
  2. {
  3. node* p_before = find_element(number, k_beginning); //return a pointer to an element we're trying to find
  4. node* p_after = p_before->p_next;
  5.  
  6. node* p_new= new node;
  7. p_new->number= number;
  8. p_new->p_next= p_after;
  9. p_before->p_next= p_new;
  10. }

The code is not in english so I would need to translate everything but you don't really need it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsdpowa is offline Offline
21 posts
since Aug 2005
Sep 3rd, 2005
0

Re: Singly-Linked Lists problem

C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Sep 3rd, 2005
0

Re: Singly-Linked Lists problem

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?
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Sep 4th, 2005
0

Re: Singly-Linked Lists problem

Quote 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?
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.

C++ Syntax (Toggle Plain Text)
  1. 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)
  1. void add_before_element(int number, node*& p_beginning) //This is a function that is being called
  2. {
  3. 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
  4. 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
  5.  
  6. node* p_new= new node; //We make a new node called p_new
  7. p_new->number= number; //Now we insert a number (5) in the newly created node
  8. p_new->p_next= p_after; //And now we link the newly created node
  9. p_before->p_next= p_new; //Same goes here
  10. }

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)
  1. Adding a number before a certain number
  2. Our list contains: 5, 3, 4, 8, 1, 2
  3. Before what element would you like to insert a number? 1
  4. What element would you like to insert? 45
  5. Our list after the operation: 3, 4, 8, 1, 45, 2

I also did the add_after_element() and it works fine.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsdpowa is offline Offline
21 posts
since Aug 2005
Sep 4th, 2005
0

Re: Singly-Linked Lists problem

Quote originally posted by Daishi ...
C++ Syntax (Toggle Plain Text)
  1. 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:

C++ Syntax (Toggle Plain Text)
  1. Our list: 3, 2, 1 (I first input 1 then 2 then 3)
  2. Element you'd like to add before? 1
  3. Number: 45
  4. Our list: 2, 45, 1
  5.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsdpowa is offline Offline
21 posts
since Aug 2005
Sep 4th, 2005
0

Re: Singly-Linked Lists problem

How are you printing out the list?

-Fredric
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Sep 4th, 2005
0

Re: Singly-Linked Lists problem

I'm printing it out this way:

C++ Syntax (Toggle Plain Text)
  1. void print_out(node*& p_beginning)
  2. {
  3. node* p_print = p_beginning;
  4.  
  5. while (p_print != 0)
  6. {
  7. cout << p_print->number << " ";
  8. p_print = p_print->p_next;
  9. }
  10. cout << endl;
  11. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bsdpowa is offline Offline
21 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Link List Sorting Problem
Next Thread in C++ Forum Timeline: stl vector - can you delete by position?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC