If current channel is the first in the channel list channels_getprev() will not return the last channel in the list. Instead the next to last channel will be returned.
To fix this move channels_cache = p; statement out from the while loop.
Correct code:
int channels_getprev(int id)
{
struct channel_t* p = find_channel(id);
if (p==NULL) {
return channels_getfirst();
} else {
channels_cache = p->prev;
if (p->prev == NULL) {
p = channels;
while (p->next != NULL) {
//channels_cache = p; // remove this line
p = p->next;
}
channels_cache = p; // new line
}
If current channel is the first in the channel list channels_getprev() will not return the last channel in the list. Instead the next to last channel will be returned.
To fix this move channels_cache = p; statement out from the while loop.
Correct code:
int channels_getprev(int id) { struct channel_t* p = find_channel(id);
if (p==NULL) { return channels_getfirst(); } else { channels_cache = p->prev; if (p->prev == NULL) { p = channels; while (p->next != NULL) { //channels_cache = p; // remove this line p = p->next; } channels_cache = p; // new line }
} }