C 循环链表
C Circular Linked list
您好,我最近刚刚在 Java 中实现了一个循环链表,但我在使用 (C) 编程语言时遇到了一些麻烦 - 没有任何条目会打印到控制台。
有人可以帮我处理循环链表的插入和输出部分吗?
这是插入函数以及输出函数。
/**
* 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;
}
好像有2个问题...
首先,如评论中所述,您已经传递了指针的值并试图更改它,但不会更改实际指针的值。您需要将该指针的地址作为双指针传递。
这将帮助您添加第一个条目。
其次,不懂
pWeaponTail->pNext = pWeaponHead;
pNewWeapon->pNext = pNewWeapon;
pWeaponHead = pNewWeapon;
它只是将列表的尾部链接到列表的头部,并将新条目的尾部链接到它自己。这是行不通的
试试这个:
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;
}
您好,我最近刚刚在 Java 中实现了一个循环链表,但我在使用 (C) 编程语言时遇到了一些麻烦 - 没有任何条目会打印到控制台。 有人可以帮我处理循环链表的插入和输出部分吗?
这是插入函数以及输出函数。
/**
* 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;
}
好像有2个问题... 首先,如评论中所述,您已经传递了指针的值并试图更改它,但不会更改实际指针的值。您需要将该指针的地址作为双指针传递。 这将帮助您添加第一个条目。 其次,不懂
pWeaponTail->pNext = pWeaponHead;
pNewWeapon->pNext = pNewWeapon;
pWeaponHead = pNewWeapon;
它只是将列表的尾部链接到列表的头部,并将新条目的尾部链接到它自己。这是行不通的
试试这个:
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;
}