Question

I'm a csci student at Colorado Mesa University. The department head teaches a grounded method for linked lists:

struct nodeType
{
    int id;
    nodeType *link;
};

void createList(nodeType *&head, nodetype *&tail)
{
    head = new nodetype;
    tail = new nodetype;
    head->id=-1; //some initialize value
    head->link=tail;
    tail->link=NULL;
}

void insertList(nodeType *&head, nodeType *&tail)
{
    nodetype *knew,*prior, *next;
    knew = new nodetype;

    knew ->name = name

    prior = head;
    next = head->link;
    while(next != tail && knew->id > next->id)
    {
        prior = next;
        next = next->link;
    }

    prior->link = knew;
    knew->link = next;
}

She teaches this for obvious reasons. With the grounded head and tail, It's easier to insert from, as you call the above function, and then write a function to append all the data inside those two nodes, and it's slightly easier when writing your delete function, as you never delete head or tail, and thus it makes it harder to lose the list and create garbage.

My algorithms professor says that everywhere else that I encounter lists "in the real world," a non-grounded list would be better. Other languages, using the STL and on the internet, I wouldn't find list functions that implement a head and tail.

I just want to be prepared for programming in the actual real world, not what my professors think is the real world, so my question is this: Is it better to use one or the other, to use whichever I find easier, or to approach each problem with both in mind?

Thank you in advance for your time helping me resolve this feud.

Était-ce utile?

La solution

In "the real world", you will use a list that other programmers have designed, implemented, optimized, and tested, and you won't ever know whether it is "grounded", since that is just an implementation detail.

What's important to take away from your algorithms courses is:

  • Performance characteristics. Does a linked-list or vector have faster random-access? Faster append? Faster removal of the first element? Using the right container is NOT premature optimization.

  • Seeing enough different implementation styles so that if you ever have to step through your code with a debugger, you won't be royally confused. If you saw the at-end-of-list test return true, but the next-node pointer isn't NULL, you'd probably be really confused if you'd never seen a "grounded" list implementation before.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top