Look over C++ template
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Look over C++ template

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Posts
    3

    Smile Look over C++ template

    HEy i was just wondering if someone could look over my program and make any suggestions on what i could do better and if I have everything that was asked...here is what was assigned:

    Write a C++ class template (named GenLookup) to implement a generic look-up list class. The elements of the lists may be instantiated to any simple data type or to string type. The lists must be implemented by a linked-list for which you keep a pointer to the start (Head) of the list. New items will always be added at the start of the list. {For efficiency and ease of maintenance, minimize the code by using a singly-linked list and only a Head pointer in the GenLookup class (or alternatively, in a List class it is based on).} The operations you must provide will be used as:
    MyListl.Add(ElementVal); // add item to this list

    MyList2.Remove( ElementVal); // get an item off this list

    Element* Ptr = MyList2.Find( ElementVal); // get a pointer to an item on this list (or null if not found)

    Write a driver program that will create multiple lists (including numeric and string types) and allow the user to add, remove, and find data on each, with output to show the operations performed and values added, removed, or found. Raise an exception when Remove is attempted on an item not on the list. Handle this exception in the driver program.

    Hand in source listings for all classes and for the driver program, the executable file (.exe), and sample output that shows you have instantiated multiple lists with different kinds of elements (including string) and that you have verified the behaviors for each list.

    AND HERE IS MY CODE:

    Code:
    template <class T>
    class GenLookup
    {
    private
    struct ListNode
    {
    T value;
    struct ListNode *next;
    };
    ListNode *head;
    public
    NumberList()
    {
    head = NULL;
    }
    ~NumberList*();
    void Add(T);
    void Remove(T);
    };
    template <class T>
    void GenLookup<T>::Add(T (ElementVal)
    {
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode = NULL;
    newNode = new ListNode;
    newNode->value = num;
    if(!head)
    {
    head = newNode;
    newNode->next =NULL;
    }
    else
    {
    nodePtr = head;
    previusNode = NULL;
    while(nodePtr != NULL && nodePtr -> value < num)
    {
    previousNode = nodePtr;
    nodePtr = nodePtr -> next;
    }
    if(previousNode == NULL)
    {
    head = newNode;
    newNode -> next = nodePtr;
    }
    else
    {
    previousNode -> next = newNode;
    newNode -> next = nodePtr;
    }
    }
    }
    template <class T>
    void GenLookup<T>::Remove(T (ElementVal)
    {
    ListNode *nodePtr;
    ListNode *previousNode;
    if(!head)
    return;
    if(head -> value == num)
    {
    nodePtr = head -> next;
    delete head;
    head = nodePtr;
    }
    else
    {
    nodePtr = head;
    while(nodePtr != NULL && nodePtr -> value != num)
    {
    previousNode = nodePtr;
    nodePtr = nodePtr -> next;
    }
    if(nodePtr)
    {
    previousNode -> next = nodePtr -> next;
    delete odePtr;
    }
    }
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: Look over C++ template

    Thanks for using code tags even though you somehow lost the formatting.

    You could use initialization lists http://www.parashift.com/c++-faq-lit....html#faq-10.6 New nodes should be added at the start of the list and as I understand the text that's the head. If so, you can simplify the code a lot. Also, the current code is more a sorted insert than an add.

    You still haven't written the find method and the ever so important testing code but before that, try to compile it as it is since you have some minor issues to solve. If you fail to solve them please ask again.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,879

    Re: Look over C++ template

    1) Your class won't compile as listed here.
    I'll assume you copied a list class called NumberList from somewhere and atempted to "templatize" it.

    2) Your Add() function has an invalid prototype. There's a second opening '(' character.
    guessing it's a transcription error and it's intended to be a reference mark.

    3) Your Add() function doesn't work as prescribed in the task. It seems to be written to be adding a node to the end of the list rather than to the start.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center