How do I remove the last node from a list, without having a prev pointer ?

This is a discussion on How do I remove the last node from a list, without having a prev pointer ? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by manasij7479 If I make the remove function to remove the one next to the passed node.. One ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    Quote Originally Posted by manasij7479
    If I make the remove function to remove the one next to the passed node..
    One thing to note is that erase on SGI's slist still exists, except that it removes the element that the iterator points to in linear time.

    Quote Originally Posted by manasij7479
    Should I change the begin() to return an iterator to the front sentinel instead of the first node containing a value?
    Then what happens if the iterator returned by begin() is dereferenced?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by laserlight View Post
    One thing to note is that erase on SGI's slist still exists, except that it removes the element that the iterator points to in linear time.
    I'd keep both then..
    Then what happens if the iterator returned by begin() is dereferenced?
    Same as what happens for end() ..
    ...but the loops will look more complicated.... for(auto x = ++(l.begin());x!=l.end();x++)
    and that looks like a bad idea anyway..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Remember that to the user, the sentinels "should not exist." That is, they should not dereference those sentinels. So what happens if they do? It's best to simply make sure they can't, if possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    I'm not sure keeping both is a bad thing. Even if remove just unhooks a node from the list, you could always invent erase(). Then the code is like the remove-erase idiom.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Linked List remove node issue
    By prihod in forum C Programming
    Replies: 1
    Last Post: 04-19-2008, 09:54 AM
  4. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

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