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

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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..

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

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