|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一个单链表demo.c
- #include <stdio.h>
- #include <stdlib.h>
- struct LinkList
- {
- int num;
- struct LinkList *next;
- };
- struct LinkList * initLinkList(struct LinkList **pHeader, struct LinkList **pTail)
- {
- struct LinkList *newNode;
-
- newNode = malloc(sizeof(struct LinkList));
- if(newNode == NULL)
- {
- perror("Memory failure\n");
- exit(EXIT_FAILURE);
- }
- newNode->num = -1;
- newNode->next = NULL;
-
- *pHeader = newNode;
- *pTail = newNode;
-
- return newNode;
- }
- void inserNode(struct LinkList* pTail)
- {
- int newNodeNum;
- struct LinkList *newNode;
-
- while (1)
- {
- printf("input a number(-1 to quit): ");
- scanf("%d", &newNodeNum);
-
- if (newNodeNum == -1)
- {
- return;
- }
-
- newNode = malloc(sizeof(struct LinkList));
- newNode->num = newNodeNum;
- newNode->next = NULL;
-
- pTail->next = newNode;
- pTail = newNode;
- }
- }
- void forEachNode(struct LinkList *pHeader)
- {
- if ( !pHeader->next )
- {
- return;
- }
-
- struct LinkList *pCurrent = pHeader->next;
- do
- {
- printf("%d\n", pCurrent->num);
- pCurrent = pCurrent->next;
- }while (pCurrent);
- }
- void test01()
- {
- struct LinkList *pHeader = NULL;
- struct LinkList *pTail = NULL;
-
- initLinkList(&pHeader, &pTail);
-
- //第一次插入
- inserNode(pTail);
- forEachNode(pHeader);
-
- //第二次插入
- inserNode(pTail);
- forEachNode(pHeader);
- }
- int main(void)
- {
- test01();
-
- return 0;
- }
复制代码
现在问题是第二次插入后再遍历链表时只会打印第二次插入的数据, 不会把两次的数据都打印出来, 问题出在哪?
问题出在`inserNode()`函数中。在第一次插入节点后,虽然我们将尾节点`pTail`更新为新插入的节点,但是头节点`pHeader`仍然指向原来的头节点。所以在第二次插入节点时,是从原来的头节点开始遍历的,而没有遍历到新插入的节点。
解决这个问题可以修改`inserNode()`函数,让它返回新插入的节点,并在每次插入节点后更新尾节点`pTail`为新插入的节点。同时,在`test01()`函数中,第一次插入后,将头节点`pHeader`更新为第一个插入的节点。这样在第二次插入时,就会从第一个插入的节点开始遍历。
修改后的代码如下:
#include
#include
struct LinkList
{
int num;
struct LinkList *next;
};
struct LinkList * initLinkList(struct LinkList **pHeader, struct LinkList **pTail)
{
struct LinkList *newNode;
newNode = malloc(sizeof(struct LinkList));
if(newNode == NULL)
{
perror("Memory failure\n");
exit(EXIT_FAILURE);
}
newNode->num = -1;
newNode->next = NULL;
*pHeader = newNode;
*pTail = newNode;
return newNode;
}
struct LinkList* inserNode(struct LinkList* pTail)
{
int newNodeNum;
struct LinkList *newNode;
while (1)
{
printf("input a number(-1 to quit): ");
scanf("%d", &newNodeNum);
if (newNodeNum == -1)
{
return pTail; // 返回最新的尾节点
}
newNode = malloc(sizeof(struct LinkList));
newNode->num = newNodeNum;
newNode->next = NULL;
pTail->next = newNode;
pTail = newNode;
}
return pTail; // 没有插入时,返回原来的尾节点
}
void forEachNode(struct LinkList *pHeader)
{
if ( !pHeader->next )
{
return;
}
struct LinkList *pCurrent = pHeader->next;
do
{
printf("%d\n", pCurrent->num);
pCurrent = pCurrent->next;
}while (pCurrent);
}
void test01()
{
struct LinkList *pHeader = NULL;
struct LinkList *pTail = NULL;
pTail = initLinkList(&pHeader, &pTail); // 初始化链表
//第一次插入,并更新头节点
pTail = inserNode(pTail);
pHeader = pHeader->next;
forEachNode(pHeader);
//第二次插入
pTail = inserNode(pTail);
forEachNode(pHeader);
}
int main(void)
{
test01();
return 0;
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|