简体   繁体   中英

How to access a linked list pointed to by a void pointer

[enter image description here][1]I just started leaning C++ and I have been given a course project.

The initial data is located into a data structure consisting of arrays of pointers, headers and items

typedef struct headerA
{
    void* pItems;  // Pointer to the linked list of items. 
                   // Items may be of types ITEM1...ITEM10.
    char cBegin;   // The linked list contains objects in which
                   // the first letter of the second word
                   // of ID cBegin.
    struct headerA* pNext;
} HEADER_A;

typedef struct item1
{
    char* pID;
    unsigned long int Code;
    char* pTime; // formatted as hh::mm::ss
    struct item1* pNext;
} ITEM1;
 
HEADER_A * *p = GetStruct3(3, 100);
ITEM1* pNewItem = (ITEM1*)GetItem(1); // generates a stand-alone item of type iItem and returns the 
//pointer to it.
int i = 0;
while (p[i] != NULL) // p is a an array of pointers to linked list of Headers
{
    pNew = p[i]->pNext; // accessing the first Header. Each header points to a 
    //linked list of Items
    while (pNew)   // looping through the linked list of each header
    {
        pNew->pItems = pNewItem; // This is where I have a problem. how to access 
      //the void * pItems

        while (pNewItem) // looping through the linked list of Items
        {
            cout << pNewItem->Code << pNewItem->pID << pNewItem->pTime << endl;
            pNewItem = pNewItem->pNext;
        }
        pNew = pNew->pNext;
    }
    i++;
}

My problem is, I am unable to properly access the linked list of Items in each Header.

void* pItems;  // Pointer to the linked list of items.

When I used a typecast pNewItem = (ITEM1*)pNew->pItems; , it prints only the first item in the first header, and the while loops don't work anymore.

How do I access the linked list of ITEM in each header pointed to by a void* pointer, using C++ style?

 HEADER_A **p = GetStruct3(3, 100);
ITEM1* pNewItem = (ITEM1*)GetItem(1);
ITEM3* pNewItem2 = (ITEM3*)GetItem(3);
HEADER_A* pNew;
 int i = 0;
while (p[i] != NULL)
{  
    pNew = p[i]->pNext;
    cout << i << endl;
    while (pNew)
    {
        cout << i << endl;
        cout << pNew->cBegin << endl;
        pNew->pItems = pNewItem;
        pNewItem12 = pNewItem;
        
        while (pNewItem12)
        {
            cout << pNewItem12->Code << pNewItem12->pID << pNewItem12->pTime 
   << endl;
            cout << " we are now here12" << endl;
            pNewItem12 = pNewItem12->pNext;   
        }
        cout << " we are now her1234e" << endl;
        pNew = pNew->pNext;
    }
    i++;

This is sample output from the code snippet above

0
0
T
1686042807Lime Green10:34:47
we are now here12
we are now her1234e
0
W
1686042807Lime Green10:34:47
we are now here12
we are now her1234e
1
1
G
1686042807Lime Green10:34:47
we are now here12
we are now her1234e
1

This is not the complete output but it shows that it is looping through the while loops but it is printing the same item that i generated in the beginning not the ones in the structure, This is because I am having difficulties going through the list of linked list of Item pointed to by void* pItem in each Header [1]: https://i.stack.imgur.com/Wh3av.jpg

I noticed my error. The datatype of the pointer I was typecasting to was not the same as the datatype of the Items in the linked list pointed to by the void pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM