![](/img/trans.png)
[英]Code works great but I don't know how to do reverse process in the second step
[英]I don't know how it works?
以下函數用於 start 指向以下鏈表的第一個節點的輸出是什么?
1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
作為一名新的貢獻者,我將讓您休息一下,並提供創建鏈表的代碼,以及 Blaze 建議的修改。 我在 C 方面並不太快,所以可能有更好的實現。 希望這會幫助您和/或其他人。
#include "stdio.h"
#include "malloc.h"
struct node {
int data;
node* nextNode;
};
void fun(struct node* start)
{
printf("%d", start->data); // <=== this needs to be first
if (start->nextNode == NULL) {
return;
}
printf("->");
fun(start->nextNode);
}
node* findLastNode(struct node* previousNode)
{
if (previousNode->nextNode == NULL) {
return previousNode;
}
findLastNode(previousNode->nextNode);
}
void addNode(node* firstNode, int data)
{
node* lastNode = NULL;
node* nodePtr;
nodePtr = (node*)malloc(sizeof(node));
nodePtr->data = data;
nodePtr->nextNode = NULL;
if (firstNode->nextNode == NULL) {
firstNode->nextNode = nodePtr;
}
else {
lastNode = findLastNode(firstNode);
lastNode->nextNode = nodePtr;
}
}
int main()
{
node firstNode;
firstNode.nextNode = NULL;
addNode(&firstNode, 1);
addNode(&firstNode, 2);
addNode(&firstNode, 3);
addNode(&firstNode, 4);
addNode(&firstNode, 5);
addNode(&firstNode, 6);
fun(firstNode.nextNode);
printf("\n");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.