-1

Hi I just recently implemented a circular linked list in Java but I am having some troubles doing so in the (C) programming language - No entries will print to the console. Could someone help me with the insertion and output portion to my circular linked list?

Here is the Insertion function as well as the Output function.

/**
 *  FUNCTION        :   addWeapon
 *  DESCRIPTION     :   This function will add a new weapon to the weapon wheel.
 *  PARAMETERS      :   pWeaponHead
 *  RETURNS         :   true, false
 */
const bool addWeapon(struct Weapon* pWeaponHead, struct Weapon* pWeaponTail)
{
    struct Weapon* pNewWeapon = NULL;
    pNewWeapon = (struct Weapon*)malloc(sizeof(struct Weapon));

    // Check the system for sufficient memory.
    if (pNewWeapon == NULL) { return false; }
    else
    {
        // We have memory for a new weapon entry.
        getchar();
        printf(KPROMPTFORWEAPON);
        fgets(pNewWeapon->arsWeapon, K100BYTES, stdin);
        newLineRemover(pNewWeapon->arsWeapon);
        pNewWeapon->pNext = pNewWeapon;

        // Is the weapon wheel empty ?
        if (pWeaponHead == NULL) 
        {
            pWeaponHead = pNewWeapon;
            pWeaponTail = pNewWeapon;
            return true;
        }
        pWeaponTail->pNext = pWeaponHead;
        pNewWeapon->pNext = pNewWeapon;
        pWeaponHead = pNewWeapon;
    }
    return true;
}

/**
 *  FUNCTION        :   showWeaponWheel
 *  DESCRIPTION     :   This function will display the weapon selection
 *                      which is represented by the "Weapon Wheel" this allows
 *                      us to choose which weapon we would like that exists in the list.
 *  PARAMETERS      :   pWeaponHead
 *  RETURNS         :   true, false
 */
const bool showWeaponWheel(struct Weapon* pWeaponHead)
{
    // Check to see if the list is empty.
    if (pWeaponHead == NULL) { return false; }

    struct Weapon* pHead = pWeaponHead;
    printf("** -- Weapon Wheel -- \n");
    do
    {   
        printf("\t%s\n", pHead->arsWeapon);
        pHead = pHead->pNext;
    } while (pHead != pWeaponHead);
    return true;
}   
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • What problems are you facing with this code? Not working as expected or program crash or something else? – kiner_shah Jun 28 '20 at 04:25
  • 1
    @kiner_shah No entries will print to the console. –  Jun 28 '20 at 04:26
  • 2
    Standard setting pointer in function problem. `WeaponHead = pNewWeapon` that sets a **local** variable. Caller's variable is unchanged. The function needs to either return the head pointer or a double pointer needs to be passed in. – kaylum Jun 28 '20 at 04:28
  • @kaylum A question: Can you pass in a double pointer and have void return ? –  Jun 28 '20 at 04:31
  • 2
    Does this answer your question? [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) – kaylum Jun 28 '20 at 04:32
  • @BraidenGole Certainly can. – kaylum Jun 28 '20 at 04:32
  • @kaylum Yes, thanks ! –  Jun 28 '20 at 04:32

1 Answers1

0

It seems to have 2 problems... firstly, as mentioned in comments, you have passed the value of the pointer and attempted to change it, that doesn't change value of the actual pointer. You need to pass the address of that pointer as a double pointer. That'll help you add the first entry. Secondly, I don't understand

        pWeaponTail->pNext = pWeaponHead;
        pNewWeapon->pNext = pNewWeapon;
        pWeaponHead = pNewWeapon;

it just links the tail to the head of list, and links the tail fo new entry to itself.. It won't work

Try this:

const bool addWeapon(struct Weapon** pWeaponHead, struct Weapon** pWeaponTail)
{
    struct Weapon* pNewWeapon = NULL;
    pNewWeapon = (struct Weapon*)malloc(sizeof(struct Weapon));

    // Check the system for sufficient memory.
    if (pNewWeapon == NULL) { return false; }
    else
    {
        // We have memory for a new weapon entry.
        getchar();
        printf(KPROMPTFORWEAPON);
        fgets(pNewWeapon->arsWeapon, K100BYTES, stdin);
        newLineRemover(pNewWeapon->arsWeapon);
        pNewWeapon->pNext = pNewWeapon;

        // Is the weapon wheel empty ?
        if (*pWeaponHead == NULL) 
        {
           *pWeaponHead = pNewWeapon;
           *pWeaponTail = pNewWeapon;
            return true;
        }
        (*pWeaponTail)->pNext = pNewWeapon;
        pNewWeapon->pNext = *pWeaponHead;
        *pWeaponHead = pNewWeapon;
    }
    return true;
}