Linked List Help

This is a discussion on Linked List Help within the C++ Programming forums, part of the General Programming Boards category; Im attempting my first linked list and it is near completion but i have a few error i cant understand. ...

  1. #1
    Kyle
    Join Date
    Jan 2005
    Posts
    5

    Linked List Help

    Im attempting my first linked list and it is near completion but i have a few error i cant understand. i have a attach, insert, remove_current functions, which add and remove elements from the list, i believe i got those working fine, but my copy constructor loops infinently when i try to clone the list. heres what i got

    Code:
    void Sequence::insert(const Sequence::value_type &entry)
    {
        if(head == NULL){ //Insertion for first item
            head = new Node(entry, head);
            tail = head;
            cur = head;
            ++many_nodes;
            return;
            }
        else if(cur == head || cur == NULL){
            head = new Node(entry, head);
            cur = head;
            ++many_nodes;
            return;            
        }
        else{
            pre->next = new Node(entry, cur);
            cur = pre->next;
            ++many_nodes;
        }
        
    }
    Code:
    void Sequence::attach(const value_type &entry)
    {
        
            if( head == NULL){
                    head = new Node(entry, head);
                    cur = head;
                    tail = head;
                    ++many_nodes;
                    return;    
             }
            else if (cur == tail) {
                cur->next = new Node(entry, cur->next);
                pre = cur;
                cur = cur->next;
                tail = cur;
                        ++many_nodes;
                return;        
            }
            else if (cur == NULL){
                tail->next = new Node(entry, tail->next);
                tail = tail->next;
                cur = tail;
                    ++many_nodes;
                return;
            }
            else{
                cur->next = new Node(entry, cur->next);
                pre = cur;
                cur = cur->next;
            }
            ++many_nodes;
    }
    Code:
    void Sequence::remove_current()
      throw(std::out_of_range)
        {
        Node * tmp = NULL;
        if(cur == NULL) return;
        if(cur == head && tail == head){
        tmp = head;
        head = head->next;
        cur = head;
        tail = head;
        --many_nodes;
        return;
        }
        if(cur == head){
        tmp = head;
        head = head->next;
        cur = head;
        delete tmp;
        --many_nodes;
        return;
        }
        else if (cur == tail){
            tmp = cur;
            cur = cur->next;
            tail = pre;
            delete tmp;
            --many_nodes;
            return;
        }
        else{
            tmp = cur;
            cur = cur->next;
            pre->next = cur;
            delete tmp;
        }
        --many_nodes;
    }

    Copy Constructor:
    Code:
    Sequence::Sequence(const Sequence& from)
      :  head(NULL), tail(NULL), cur(NULL), pre(NULL), many_nodes(0)
    {
      do_copy(from);
    }
    void Sequence::do_copy(const Sequence& from) {
        start();
        for(Node * p = from.head; p != NULL; p = p->next){
            attach(p->data);
            }
    }
    Thanks alot!!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Can you show us the context in which you're using this.

    If you're doing something like
    foo = foo;

    Then I can see some deep problems in the code, like forever trying to append to itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Kyle
    Join Date
    Jan 2005
    Posts
    5

    copy constr

    I call the copy constructor in the program by declaring

    Code:
    Sequence s2 = s;    /*s is a linked with several nodes that i want to       
    
    */copy into s2

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You don't show start() or the Node constructor so I can't say what you do in there. However, in general, to copy the data from one list into another in the same sequence I'd first make sure that the old list isn't empty. If it is then no copying needed. If it isn't, since you keep track of the tail node, you don't need to run the list. If tail node is NULL then new list is empty so assign an initialized new node to head node and tail node. If tail node is not NULL, then assign an initialized node to tail->next and then assign tail->next to tail. What you do with cur and prev when you append each new node to tail is up to you. I'd repeat the process advancing from head node of old list to tail node of old list.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  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