4 Replies - 267 Views - Last Post: 16 March 2010 - 01:24 AM Rate Topic: -----

#1 gilzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 10

Linked list problem

Posted 15 March 2010 - 07:18 PM

I'm learning linked list right now, I'm trying to swap the first node and the second node with this function. The outcome I'm getting deletes the 2nd node so basically in the end I get a list with one less node. What am I doing wrong?

NODE *swapList(NODE *pList){
	NODE *pPre;
	NODE *pCur;
	
	pPre=pList;
	pCur=pPre->link;
	
	pPre->link=pCur->link;
	pCur->link=pPre;
	
	pList=pCur;
	
	return pList;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Linked list problem

#2 Guest_gregoryH@work*


Reputation:

Re: Linked list problem

Posted 15 March 2010 - 07:51 PM

Hi

Your logic needs a little rewire, you need to find out the preceeding and next (if any) to enable a swap.

If this is a single linked list you might not be able to to this as easily as a double linked list.

That is to swap Node1 and Node2 you need the node before Node1, lets call that Node0.

NODE *t = Node0->Link;
Node0>link = Node2 ;
Node2->Link = t ;


See how you go.

(GregoryH@work)
Was This Post Helpful? 0

#3 gilzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 10

Re: Linked list problem

Posted 15 March 2010 - 08:52 PM

isn't pList essentially node0 in my case?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked list problem

Posted 15 March 2010 - 09:29 PM

The only thing that I can see that could cause the problem that you're complaining about is that you're not using your own function correctly.
In other words, there's nothing wrong with the function, but you are calling it like this
// node *list is pointer to an already-existing list
swapList( list );



when you should be calling it like this
// node *list is pointer to an already-existing list
list = swapList( list );



Note that list is a pointer to a node, NOT a pointer to a pointer to a node. When you pass list as an argument to the function, processing inside the function can change things that list points to, but it can't change the value of list itself. When you assign a new value to pList inside the swap function, you are only changing the value of the local copy of pList, not pList itself. To do that you have to assign the new value in the calling function, using the return value.
Was This Post Helpful? 0
  • +
  • -

#5 gilzero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 10

Re: Linked list problem

Posted 16 March 2010 - 01:24 AM

View Postr.stiltskin, on 15 March 2010 - 08:29 PM, said:

The only thing that I can see that could cause the problem that you're complaining about is that you're not using your own function correctly.
In other words, there's nothing wrong with the function, but you are calling it like this
// node *list is pointer to an already-existing list
swapList( list );



when you should be calling it like this
// node *list is pointer to an already-existing list
list = swapList( list );



Note that list is a pointer to a node, NOT a pointer to a pointer to a node. When you pass list as an argument to the function, processing inside the function can change things that list points to, but it can't change the value of list itself. When you assign a new value to pList inside the swap function, you are only changing the value of the local copy of pList, not pList itself. To do that you have to assign the new value in the calling function, using the return value.


That was it! I don't know how I overlooked that... I was calling it correctly for all my other functions. :oops: Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1