Thread: Generic linked list function

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

    Generic linked list function

    I have three types of linked list in a program:
    Code:
    GFX_ENTRY* gfx_ls;
    GFX_ENTRY* gfx_le;
    TILEDEF*   tile_ls;
    TILEDEF*   tile_le;
    SPRITEDEF* sprite_ls;
    SPRITEDEF* sprite_le;
    Obviously, GFX_ENTRY, TILEDEF and SPRITEDEF are linked list structs. Each of them has a different set of data, but common to each are the next and prev pointers, and a char* name.

    Is it possible to create a single function to allocate and link a new entry into any of the three lists? I suspect void pointers may come into play here, but I'm not sure in what way.

    Cheers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But are the common elements in the same place in each structure?

    That is, could you create a union containing all the structures, with an additional union member overlaying the common (ie, the link pointers) members you want access to?
    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.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    I'd completely forgotten about unions. Unless I'm missing something, which is possible, it wouldn't be a problem...

    Here's how the structs presently look:
    Code:
    typedef struct sGFX_ENTRY
    {
      int n;
      char* name;
      char* filename;
      struct sGFX_ENTRY* next;
      struct sGFX_ENTRY* prev;
    } GFX_ENTRY;
    
    enum {sU,sD,sL,sR};
    
    typedef struct sTILEDEF
    {
      char *gfxname;
      char *name;
      char *message;           // message to display when moving at tile
      int prob;                 // probability of tile appearing after conclusive scan
      short int pain;          // degree of damage caused: 0-5
      short int spawntile;     // tile number to spawn
      short int spawndir[4];   // size of tilespawn area: up, down, left, right
      short int block;         // 0 - allow passage, non-0 - block passage
      short int cost;          // base price to remove this tile
      short int wear;          // base wear on digging instruments (spade, pickaxe)
      struct sTILEDEF* next;
      struct sTILEDEF* prev;
    } TILEDEF;
    
    typedef struct sSPRITEDEF
    {
      int nframes;
      char **gfxname;
      char *name;
      struct sSPRITEDEF* next;
      struct sSPRITEDEF* prev;
    } SPRITEDEF;
    I assume I can can drop next, prev and name from each of the structs and do something inelegant like this:
    Code:
    typedef struct sLIST
    {
      union
      {
        GFX_ENTRY* gfx_entry;
        TILEDEF* tiledef;
        SPRITEDEF* spritedef;
      }
      char *name;
      struct sLIST* next;
      struct sLIST* prev;
    } LIST;
    I'm not sure if that's what you were suggesting. It appears to make things even more complicated, unless there's a simpler way of managing that union than I'm aware.

    As a backup plan, I can just merge all the data into one generic struct and set the new entry function to take a parameter determining whether it's a GFX, SPRITE or TILE and leave unnecessary variables blank.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you're able to delete members from the structs you have, I guess you can also rearrange them as well (to put name, next, prev at the start)

    So
    Code:
    typedef union uNODE {
        GFX_ENTRY    gfx_entry;
        TILEDEF      tiledef;
        SPRITEDEF    spritedef;
        struct {
            union uNODE *next;
            union uNODE *prev;
            char *name;
        } any;
    } NODE;
    It's probably not so useful unless you want to do lots of casting.



    Another way would be to do
    Code:
    struct node {
        void *prev;
        void *next;
        char *name;
    };
    
    typedef struct sGFX_ENTRY
    {
      struct node link;
      int n;
      char* filename;
    } GFX_ENTRY;
    To use your linked list functions, you would just use &gfx_var.link as the parameter to your list functions.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM