【发布时间】:2021-06-15 18:32:33
【问题描述】:
谁能告诉我为什么我的递归搞砸了?谢谢你UUUU
这是 C 中的堆栈
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
struct node
{
char stdNum[12], stdName[24], CnY[10];
float gwa;
struct node *link;
};
// Initialized Functions
void createStck();
void traverseStck();
void recursion();
void push();
// Declare Globally to be used in other functions
struct node *TOP = NULL;
struct node *tempTop = NULL, *tempNode = NULL;
bool tempTopExist = false, travesalDone = false, pushTemptoTop = false, firstIteration = false;
struct node *ptr = NULL;
菜单
int main()
{
system("CLS");
int input;
do
{
printf("1 - Creation of Nodes in Stack\n");
printf("2 - Traversal of Nodes in Stack\n");
printf("3 - Addition of Nodes in Stack\n");
printf("4 - Deletion of Nodes in Stack\n\n");
printf("Choice: ");
scanf("%d", &input);
} while (input > 4 || input < 1);
switch (input)
{
case 1:
createStck();
break;
case 2:
traverseStck(TOP);
break;
}
return 0;
}
堆栈的创建
void createStck()
{
struct node *PushNode = NULL;
PushNode = malloc(sizeof(struct node));
PushNode->link = NULL;
TOP = PushNode;
char resp, temp;
do
{
printf("Student Number : ");
scanf("%s", &PushNode->stdNum);
printf("Student Name : ");
scanf("%c", &temp);
scanf("%[^\n]", PushNode->stdName);
printf("Course & Year : ");
scanf("%c", &temp);
scanf("%[^\n]", PushNode->CnY);
printf("GWA : ");
scanf("%f", &PushNode->gwa);
printf("\n(ALERT) Add another node [Y/N]? ");
scanf(" %c", &resp);
if (resp == 'Y' || resp == 'y')
{
printf("\n");
PushNode = malloc(sizeof(struct node));
PushNode->link = TOP;
TOP = PushNode;
}
} while (resp == 'Y' || resp == 'y');
PushNode = NULL;
main();
}
遍历堆栈,同时将每个值推送到临时存储中,最后从该临时存储中取回
如果输入 = 1 2 3 堆栈将为 3 2 1 遍历将是 3 2 1 推动每个将结束 1 2 3(后进后出) 问题是当我使用递归将其推回(后进后出)时
void traverseStck(struct node *TOP)
{
struct node *popNode = NULL;
popNode = TOP;
if (popNode == NULL)
{
printf("The Stack is empty!");
}
else
{
do
{
printf("%s\n", TOP->stdNum);
printf("%s\n", TOP->stdName);
printf("%s\n", TOP->CnY);
printf("%f\n\n", TOP->gwa);
TOP = TOP->link;
popNode->link = NULL;
push(popNode);
tempTopExist = true;
popNode = TOP;
} while (TOP != NULL);
travesalDone = true;
push(tempTop);
getch();
main();
}
}
void recursion(struct node *poppedNode)
{
if (poppedNode == NULL)
{
tempTopExist = false;
tempTop = NULL;
tempNode = NULL;
travesalDone = false;
}
else
{
if (firstIteration == false)
{
firstIteration = true;
TOP = poppedNode;
TOP->link = NULL;
recursion(poppedNode->link);
}
else
{
tempNode = poppedNode;
tempNode->link = TOP;
TOP = tempNode;
recursion(poppedNode->link);
}
}
}
void push(struct node *poppedNode)
{
if (travesalDone == false)
{
if (tempTopExist == false)
tempTop = poppedNode;
if (tempTopExist == true)
{
tempNode = poppedNode;
tempNode->link = tempTop;
tempTop = tempNode;
}
}
if (travesalDone == true)
{
tempNode = NULL;
recursion(poppedNode);
firstIteration = false;
}
}
【问题讨论】:
-
是否因为您也在递归调用
main()而搞砸了?函数createStck()不会返回命中main()中的break语句,而是调用main()。与函数traverseStck()类似,但这是有条件的。 -
虽然在 C 语言中技术上允许这样做,但永远不要在任何地方给自己打电话
main。如果您需要在main函数中使用循环,请使用实际循环。 -
@Ace 不要使用全局变量。
-
@WeatherVane
createStck()没有中断,因为创建后我需要再次运行菜单以使用户能够选择另一个选项。 -
好吧,如前所述,您不应该通过调用
main()来做到这一点,而是适当地构造流控制。