Thread: C++ Help.. Double linked list

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    C++ Help.. Double linked list

    bool LinkedList::next()
    {
    Node *prev, *temp;

    temp->ptr = static_cast<int>(prev->ptr) ^ static_cast<int>(current->ptr);
    prev->ptr = current->ptr;
    current->ptr = temp->ptr;
    }

    I keep getting the error:

    error C2296: '^' : illegal, left operand has type 'Node *'

    If this is totally wrong, can you just tell me how to do it, but it needs to be return type bool. The node class is defined as :

    Code:
    class Node
    {
      public:
    
        Node(){}
        ~Node(){}
    
        // stores the value for the node
        int value;
       
        // stores both the next and previous pointers, combined with the xor operator
        Node *ptr;
    };

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use code tags for all code.
    And tell me, what's this supposed to do?
    Code:
    temp->ptr = static_cast<int>(prev->ptr) ^ static_cast<int>(current->ptr);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    it is supposed to xor those two pointers on the right and set the left one equal to that

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You are trying to assign an int to a pointer. Why are you trying to do that?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    ok i dont know how to do it, but I know I need to xor the two pointers on the right and set that to the pointer on the left

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    XORing pointers might not work on all platforms. Why do you want to XOR the pointers?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    It's how you move from each element in the list to the next..

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675


    Wow. That code is all kinds of wrong. Even if it compiled, it's dead when you access a member of an uninitialized pointer (temp -- and prev for that matter)).
    Last edited by rags_to_riches; 02-10-2008 at 01:16 PM. Reason: Needed more explanation so as to not be TOO snarky.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to static_cast it back to a Node *, after you static_cast it to an int earlier. I would be surprised to find that the xor of two pointers is also a valid ptr -- and for sure it's not a valid pointer to anything you have.

    We can't help you make it Bool, because what is that supposed to mean? Bool only has true and false, you can't return a pointer out of it.

    I'm going to be generous and assume that this is an assignment and ye know not what ye do. Xor'ing addresses is an old, antiquated, dirty-even-for-the-time way to squeeze an extra 24 bits (or whatever a pointer was worth on your architecture) out of a node structure. (The penalty was that you always always always needed to have an extra pointer to previous (if you were traveling forward) or to next (if you were traveling backward) to do the xor with and that NULL had to be all bits 0.) The idea that you wouldn't use std::list, or if the assignment is to roll your own, a "normal" doubly linked list, is hard to fathom these days.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    yeah it is an assignment.. i need to make a double linked list that only uses one pointer .. im trying to make the next function by xor'ing two pointers together, that is how i was told to do it.. you can start from scratch if you want.. i just have no idea.. and in response to the last thread, I cant static cast it back, because it says i cant static cast it to int in the first place.. any ideas will be welcome.. Thanks..

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by garrettw09 View Post
    yeah it is an assignment.. i need to make a double linked list that only uses one pointer ..
    Why would you want to do that? No sane programmer would. If you use only one pointer, use a single linked list.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    it saves space.. like 4 bytes per pointer that you don't use i guess.. it's dumb.. but i just need to know how to do it..

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    4 bytes is nothing. So explain again why you need to do it that way?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    it is an assignment.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it's an assignment, Elysia, don't bother the poor guy. His instructor is going to bother him enough.

    Anyway, you're going to need to be more specific on what you're trying to do. What is your Bool function supposed to test? (And remember, it's a Bool, so it absopositively has to be a yes/no question.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM