【发布时间】:2020-06-28 04:21:15
【问题描述】:
您好,我最近刚刚在 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;
}
【问题讨论】:
-
这段代码你遇到了什么问题?无法按预期工作或程序崩溃或其他原因?
-
@kiner_shah 没有条目将打印到控制台。
-
函数问题中的标准设置指针。
WeaponHead = pNewWeapon设置 local 变量。调用者的变量没有改变。该函数需要返回头指针或需要传入双指针。 -
@kaylum 一个问题:你能传入一个双指针并返回 void 吗?
标签: c circular-list