Singly-Linked Lists problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Singly-Linked Lists problem

 
0
  #1
Sep 2nd, 2005
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:

  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;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Singly-Linked Lists problem

 
0
  #2
Sep 3rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists problem

 
0
  #3
Sep 3rd, 2005
But you don't need anything else because nothing is related to anything in the program.

  1. add_before_element(5, p_beginning);

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Singly-Linked Lists problem

 
0
  #4
Sep 3rd, 2005
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Singly-Linked Lists problem

 
0
  #5
Sep 3rd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists problem

 
0
  #6
Sep 4th, 2005
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.

  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

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists problem

 
0
  #7
Sep 4th, 2005
Originally Posted by Daishi
  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:

  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.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Singly-Linked Lists problem

 
0
  #8
Sep 4th, 2005
How are you printing out the list?

-Fredric
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists problem

 
0
  #9
Sep 4th, 2005
I'm printing it out this way:

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC