Insert into position of Linked List

This is a discussion on Insert into position of Linked List within the C++ Programming forums, part of the General Programming Boards category; I am having trouble insearting a node into a linked list into a specified position: Code: cout << "Enter letter" ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Insert into position of Linked List

    I am having trouble insearting a node into a linked list into a specified position:

    Code:
         cout << "Enter letter" << endl;          
         // new node for input
         node *current_in = new node; 
         cin >> current_in -> letter; // input into that new node
         
         cout << "Enter position" << endl;
         int in_pos;
         cin >> in_pos;    
          
           node *x = last;  // last node
           node *prev =  NULL;
            
           int current_node = 0;  
           while (x != NULL) {  
                current_node ++;               
                if (current_node == in_pos)  {          
                    current_in -> next = x;
                    prev -> next = current_in;
                }
                current_in -> next = last;
               last = current_in;
             
           }
    How can I make this work ?

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    the way you advance in the list should be something like:

    Code:
    cin >> index;
    node* ptr = head;
    
    for(int i = 0; i != index; ++i)
    ptr = ptr->next;
    once you hit the index you want, then that's when you set the pointer values instead of keeping it all in a while loop.
    Last edited by dra; 02-17-2008 at 08:04 PM.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    This may sound trivial but insertions like this into a linked-list are typically not good practice. Which you have here we normally call an indexed-list or map. Linked-lists should use iterators (if you've made an iterator class) or should insert at a certain value in the list not at an index. If you'd like to see how the STL handles list insertions, check this out:

    http://cplusplus.com/reference/stl/list/insert.html
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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