Thread: Abstract data types in a queue

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    16

    Abstract data types in a queue

    Hello - I've written a queue and I'm trying to keep it generic as far as the data it could hold. For that purpose, my node structure is defined as such:

    Code:
    struct node {
    
    void* data; struct node* next;
    }node;
    The void* is because I don't want to restrict the data type in the node (int, char, struct). My problem is adding the data to the queue. In my enqueue method, I create a new node to add the data to:
    Code:
    void enqueue(queue* q, void* item)
    {
    
    //.... // Create a new node and assign data node* new_node = (node*)malloc(sizeof(node)); new_node->data = malloc(sizeof(item)); memcpy(new_node->data, item,strlen((char *)item)); //...
    }
    My problem is using the memcpy function. I don't know how to copy exactly the size of the data type I intend to pass because of the generality of the code. In the above example I had to use strlen when I wanted to test it with a char*, but I would like to make it work for every data type without having to do a specific cast every time. I'd really appreciate help on this - thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could add another parameter to specify the size of the data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Do you need memcpy? Couldn't you just set new_node->data = item?

    I am actually working on the same thing as you right now. And that is the approach I am taking.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    16
    I tried that but it referenced the same item in memory for all nodes. So, in my tester.c file I inserted the string "node %d" for i = 0; i < 5, and got:
    node 4
    node 4
    node 4
    node 4
    node 4

    Which drove me crazy. So this is how I'm working around it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sa125 View Post
    I tried that but it referenced the same item in memory for all nodes. So, in my tester.c file I inserted the string "node %d" for i = 0; i < 5, and got:
    node 4
    node 4
    node 4
    node 4
    node 4

    Which drove me crazy. So this is how I'm working around it.
    Did you have something like this?
    Code:
    int i, temp;
    for (i = 0; i < 5; i++) {
        printf("Enter number for queue: ");
        scanf("%d", &temp);
        enqueue(queue_ptr, &temp);
    }
    If you're reusing the same variable when making the queue, then you'll need to malloc and memcpy inside enqueue; if all the data is already distinct in your main program, then you can just assign pointers. (And "passing sizeof data" is already an established idiom in C; that's how the library functions that use void *, like bsearch and qsort, work.)

  6. #6
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I did have the same problem as you probably for the reason tabstop mentioned. I was reusing the same variable to add data. Alloc new memory for each piece of data that you add to the queue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading binary file with different data types
    By larne in forum C Programming
    Replies: 8
    Last Post: 07-29-2008, 10:12 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Need help with ADT (abstract data types)
    By ortegac in forum C Programming
    Replies: 1
    Last Post: 03-30-2006, 02:23 AM
  4. Possible to pack smaller data types into larger ones?
    By Heraclitus in forum C Programming
    Replies: 3
    Last Post: 02-09-2003, 03:22 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

Tags for this Thread