Thread: Linked Lists

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Question Linked Lists

    Below is a small figure that shows a linked list with 4 nodes. Please show the necessary steps to insert a node after the 2nd element in the list.

    head tail end

    5 8 2 7
    next --->next ---->next ---->next--------end
    end--prev<--- prev<---- prev <---- prev

    You may use auxiliary variables of type nodePtr (a pointer to a node structure), if you need.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Also, if a linked list was circular (i.e., the [I]prev[I] pointer in the first element was pointing to the last element in the list - and the [I]next[I] pointer of the last element points to the first element in the list), how would you be able to find out if you've reached the end of the list if you are traversing it (in order to avoid going around the list forever)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    allocate memory for a new node,adjust the next pointer to point to the third element,and adjust the prev pointer to point to the second element
    adjust the next pointer of the second element to point to this new element and adjust the previous pointer of the original third element to point to this new added element.

    thats one way of doing it.
    Last edited by qqqqxxxx; 03-09-2006 at 11:13 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    and if a list was circular,there is no first element then ,is there?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    qqqqxxxx, so how would the code look to adjust the next pointer to point to the second element,and adjust the prev pointer to point to the first element
    adjust the next pointer of the first element to point to this new element and adjust the previous pointer of the original 2nd element to point to this new added element.

    On if a cicular linked list, well I would think that it would last forever too so thanks for the check!

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you'll get better responses if you post the code you're having trouble with.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    assuming q is the address of the first node then,

    q->next->next
    is the address of the second node.
    Code:
    struct node *n = (struct node *)malloc(sizeof(struct node));
    struct node *temp =q->next->next;
    n->prev= temp;                 //adjusting the new element (third)to 
    n->next= temp->next;      // point to the right places
    temp->next =n;                //adjusting the 2nd element
    and so on,


    can u complete the rest of the code?

    Alternatively u could have an addnode function.
    Last edited by qqqqxxxx; 03-09-2006 at 11:51 PM.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Alright i got it now, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM