2

I'm trying to get my dequeue method working on my implementation of a LinkedList ADT. However, it is removing from the beginning of the queue instead of the end. Any help with this? I'm new to C, and am trying to port a java exercise over to C.. It's supposed to remove the last node of the list.

Here's my dequeue method:

static void linkedQueueDequeue(Queue* q) {
    LinkedQueue *lq = ((LinkedQueue*)q->privateData);
    Node* temp = lq->head->next;
    lq->head->data = lq->head->next->data;
    lq->head->next = temp->next;
    free(temp);
    lq->size--;


}

Here's the output when trying to dequeue last node:

=====================
|Testing LinkedQueue|
=====================
adding 1 to first node
adding 2 to second node
adding 3 to third node
adding 4 to fourth node
[1,2,3,4]
dequeue last node
should print [1,2,3]
[2,3,4]
return first node
peek: 2
what's the size?
size: 3
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
ChrisDevWard
  • 885
  • 2
  • 9
  • 20
  • Well, your code clearly removes the head of the list. What have you tried to reach the tail of the list instead? – Mat Feb 09 '12 at 06:53
  • Hint: If you want to remove the last node, You need to traverse the linked list till the second last element and then remove the last element. – Alok Save Feb 09 '12 at 06:54
  • looks like you'll need a while loop to get the last node. – Robert Peters Feb 09 '12 at 07:02
  • You overwrite the data of the head of the list, you always remove the second node, you don't do any error checking (what if `q` or `lq->head` or `lq->head->next` is `NULL`?) – Some programmer dude Feb 09 '12 at 07:27
  • You need to store a pointer to the tail. Add a pointer to the nodes to store the previous node. This will save on traversing the linked list to remove the last item – Ed Heal Feb 09 '12 at 08:19

2 Answers2

1

As you saw already, the code in linkedQueueDequeue pops the first entry as if you wanted a stack (LIFO), you can iterate your temp to the end of the list, then remove it's temp->next:

static void linkedQueueDequeue(Queue* q) {
    LinkedQueue *lq = ((LinkedQueue*)q->privateData);
    Node* temp = lq->head->next;
    while(temp->next) temp = temp->next;
    free(temp->next);
    temp->next = 0;
    lq->size--;
}

Also note, that there ist something slightly odd about your Queue/LinkedQueue implementation considering the conversion (LinkedQueue*)q in line 2. Are you sure you need that cast? I cannot really tell because you did not give us the definitions of Queue and LinkedQueue. Is there maybe also a ->tail in LinkedQueue? If so, then you dont need the iteration and can instead use ->tail to position temp (and of course: you have to update ->tail to the new end).

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • That worked, thanks! I wasn't sure how to iterate to the next node, but it seems too simple now that I think about it. Linked lists have always been my worst enemy. – ChrisDevWard Feb 09 '12 at 09:37
  • 1
    `Linked lists have always been my worst enemy`: They are until they are your best friend ;-) – Bernd Elkemann Feb 09 '12 at 15:11
0

Your output appears to be the correct behavior for a queue/FIFO in that the first item to be removed from the list is the first item that was added to the list. Are you trying instead to create a stack?

aqua
  • 617
  • 4
  • 12