Linked List Run Time Error

This is a discussion on Linked List Run Time Error within the C++ Programming forums, part of the General Programming Boards category; My console window just come up and said an error has occured and must close, can you figure what's wrong ...

  1. #1
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Unhappy Linked List Run Time Error

    My console window just come up and said an error has occured and must close, can you figure what's wrong with my code? I've searched the forums and read some of the other people's linked list questions in the forum but no answer. Here's the code and the main function.

    Code:
    template<typename T>
    class List {
    public:
      List() {
        m_head = 0;
        m_tail = m_head;
        m_size = 0;
      }
    
      ~List() {
        ListNode* save = m_head;
        while(save) {
          save = save->m_next;
          delete m_head;
          m_head = save;
        }
        m_tail = 0;
        m_size = 0;
      }
    
      void add(const T& elem) {
        if(m_head) {
          ListNode *link = m_tail;
          m_tail->m_next = new ListNode;
          m_tail = m_tail->m_next;
          m_tail->m_prev = link;
          m_tail->m_next = 0;
          m_tail->m_data = elem;
        } else {
          m_tail = new ListNode;
          m_tail->m_prev = 0;
          m_tail->m_next = 0;
          m_tail->m_data = elem;
        }
        ++m_size;
        return;
      }
    
      void insert(const T& elem, unsigned pos) {
        ListNode *targ = locate(pos);
        if(targ) {
          if(targ == m_head) {
            ListNode *link = m_head;
            m_head->m_prev = new ListNode;
            m_head = m_head->m_prev;
            m_head->m_prev = 0;
            m_head->m_next = link;
            m_head->m_data = elem;
          } else {
            ListNode *node = new ListNode;
            ListNode *link = targ->m_prev;
            link->m_next = node;
            targ->m_prev = node;
            node->m_prev = link;
            node->m_next = targ;
            node->m_data = elem;
          }
          ++m_size;
        }
        return;
      }
      // not done!
      void remove(unsigned pos) {
        ListNode *targ = locate(pos);
        return;
      }
      
      void print() const {
        ListNode *print = m_head;
        while(print) {
          std::cout << '[' << print->m_data << ']';
          print = print->m_next;
        }
        return;
      }
      
    private:
      struct ListNode {
        ListNode *m_prev;
        ListNode *m_next;
        T         m_data;
      } *m_head, *m_tail;
      
      unsigned    m_size;
      
      ListNode *locate(unsigned n) {
        if(n > 0 && n <= m_size) {
          ListNode *find = 0;
          if(n > m_size/2) {
            find = m_tail;
            while(n < m_size) {
              find = find->m_prev;
              ++n;
            }
          } else {
            find = m_head;
            unsigned one(1);
            while(n > one) {
              find = find->m_next;
              --n;
            }
          }
          return find;
        } else {
          return 0;
        }
      }
    };
    This is main.
    Code:
    int main() {
      List<int> list;
      list.add(1);
      list.add(3);
      list.add(4);
      list.add(5);
      list.insert(2, 2);
      list.print();
      std::cin.get();
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    Your add() function never updates head pointer.
    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
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    That was embarrassing...

    Thanks, thanks, I had rewritten a list(this one) that I had a year ago and I was wondering why it didn't work this time. Haha! I thought that making m_tail point to the same address as m_head in the constructor was fine, forgot they both point to 0 and the address they point to changes when I used new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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