Deleting the oldest element in a linked list

This is a discussion on Deleting the oldest element in a linked list within the C Programming forums, part of the General Programming Boards category; Hi all, I often visit your forum to get some hint about my school project, it's very nice, but now ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Deleting the oldest element in a linked list

    Hi all,
    I often visit your forum to get some hint about my school project, it's very nice, but now I absolutely need help about a pretty easy thing (I'm surprised that I can't find a solution myself).
    I'm working on a quite big project but I'm stuck because I need to produce a function that "dequeues" the oldest element in a list

    Code:
    int dequeue(NODEPTR pnt, NODEPTR h)
    {int c;
         pnt=h;
    	 while(pnt->next!=NULL)
    		   pnt=pnt->next;
         c=pnt->nodecode;                   //this is the variable that I need and it prints it correctly at least for what I can run now
    	 printf("%d\n",c);
             h=pnt->next;
    
         return c;
    }
    The problem is that it doesn't delete anything...

    h is the head of the list, pnt is a pointer of the same type
    it has to return a int variable but that's not the problem, I've tried a lot of variations and still I can't delete the oldest element...I hope somebody will help me
    Last edited by NoobCoder; 07-02-2009 at 03:12 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How does your program know or discover, which element is oldest? Is this a stack (FILO), and that's why you're walking down to the last element?

    After the while loop in your code, where is your pointer, pnt? Two big guesses!

    Can you move back up toward the head, from the tail of a singly linked list?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    It should be a FIFO, I need to dequeue the oldest enqueued element...I need to use that because I'm trying to implement a bfs so I need that dequeue deletes the oldest enqueued node...
    I've worked around a little bit and this is what I've done

    Code:
    int dequeue(NODEPTR pnt, NODEPTR h)  //this is  wrong
    {int c;
        pnt=h;
         if((pnt->next)==NULL)
    	   { c=head->nodecode;
             h=NULL;
    	     printf("%d\n",c);
    	   }
    else{ 
    	  c=head->nodecode;
          printf("%d\n",c);                 //now prints the variable that deletes but it's the most recent -.-'
          head=pnt->next;
    	  free(pnt);
        }
         
    return c;
    }
    The function now is working , but in the opposite way, it's deleting the most recent enqueued node and it doesn't delete the element when the list has 1 element...any idea ?
    Last edited by NoobCoder; 07-02-2009 at 04:45 AM.

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    if it is FIFO, the oldest is the tail of the list isn'it?

    Code:
         if((pnt->next)==NULL)
    	   { c=head->nodecode;
             h=NULL;
    	     printf("%d\n",c);
    	   }
    Here you're telling: if the list has only one node, take its number. you are not deleting anything.
    Code:
    else{ 
    	  c=head->nodecode;
          printf("%d\n",c);                 //now prints the variable that deletes but it's the most recent -.-'
          head=pnt->next;
    	  free(pnt);
        }
    in this case you are taking the node number of the first element, moving the head to the next node, and deleting the first node! of course it deletes the latest entry.

    This should work:
    Code:
    int dequeue(NODEPTR pnt, NODEPTR h)
    {int c;
         pnt=h;
        NODEPTR prev=pnt;
    	 while(pnt->next!=NULL) {
                       prev=pnt;
    		   pnt=pnt->next;
            }
         c=pnt->nodecode;                
    	 printf("%d\n",c);
             free(pnt);
             prev->next=NULL;
             h=pnt->next;
    
         return c;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21