# Linked list problem

Page 1 of 1

## 4 Replies - 438 Views - Last Post: 16 March 2010 - 01:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=162170&amp;s=875a6a05091a7e610f22812be375117d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gilzero

• New D.I.C Head

Reputation: 0
• 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;

pList=pCur;

return pList;
}

```

Is This A Good Question/Topic? 0

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)

### #3 gilzero

• New D.I.C Head

Reputation: 0
• 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?

### #4 r.stiltskin

• D.I.C Lover

Reputation: 1833
• 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.

### #5 gilzero

• New D.I.C Head

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

## Re: Linked list problem

Posted 16 March 2010 - 01:24 AM

r.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. Thanks for the help!