【问题标题】:Why does the recursion mess up, and end up getting into an infinite loop?为什么递归会搞砸,最终陷入无限循环?
【发布时间】: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() 来做到这一点,而是适当地构造流控制。

标签: c stack


【解决方案1】:

我没有使用递归,只是制作了一个要遍历的副本,使原始堆栈保持不变。

解决了。谢谢各位

#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 to avoid errors
void createStck();
void traverseStck();
void addStck();
void delStck();
void endProg();

// Declared Globally to be used in other functions
struct node *TOP = NULL, *ptr = NULL;

// Check if Stack is already created
bool stackCreated = false;

int main()
{
  system("CLS");
  int input;
  do
  {
    system("CLS");
    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");
    printf("5 - End Stack Program\n\n");
    printf("Choice: ");
    scanf("%d", &input);
  } while (input > 5 || input < 1);

  switch (input)
  {
  case 1:
    createStck();
    break;
  case 2:
    traverseStck();
    break;
  case 3:
    addStck();
    break;
  case 4:
    delStck();
    break;
  case 5:
    endProg();
    break;
  }
  return 0;
}

void createStck()
{
  system("CLS");
  if (!stackCreated)
  {
    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;
    stackCreated = true;
    main();
  }
  else
  {
    char choice;
    printf("(ALERT) The stack already created!\n");
    printf("Do you want to create a stack? [Y/N]: ");
    scanf(" %c", &choice);
    (choice == 'Y' || choice == 'y') ? (stackCreated = false, createStck()) : (printf("\n(ALERT) You will now be redirected to the main menu!"), sleep(2), main());
  }
}

void traverseStck()
{
  system("CLS");
  ptr = TOP;
  if (ptr == NULL)
  {
    printf("(ALERT) The Stack is empty!, please create a stack before traversing");
    sleep(2);
    main();
  }
  else
  {
    do
    {
      printf("Student Number  : %s\n", ptr->stdNum);
      printf("Student Number  : %s\n", ptr->stdName);
      printf("Course and Year : %s\n", ptr->CnY);
      printf("GWA             : %f\n\n", ptr->gwa);

      ptr = ptr->link;
    } while (ptr != NULL);

    printf("(ALERT) Press any key to continue...");
    getch();
    main();
  }
}

void addStck()
{
  system("CLS");
  if (TOP == NULL)
  {
    char choice;
    printf("(ALERT) The stack is empty\n");
    printf("Do you want to create a stack? [Y/N]: ");
    scanf(" %c", &choice);
    (choice == 'Y' || choice == 'y') ? createStck() : (printf("\n(ALERT) You will now be redirected to the main menu!"), sleep(2), main());
  }
  else
  {
    struct node *addNode = NULL;
    addNode = malloc(sizeof(struct node));
    char temp;

    printf("Student Number : ");
    scanf("%s", &addNode->stdNum);

    printf("Student Name   : ");
    scanf("%c", &temp);
    scanf("%[^\n]", addNode->stdName);

    printf("Course & Year  : ");
    scanf("%c", &temp);
    scanf("%[^\n]", addNode->CnY);

    printf("GWA            : ");
    scanf("%f", &addNode->gwa);

    addNode->link = TOP;
    TOP = addNode;
    addNode = NULL;
    printf("\n(SUCCESS)  New Node added on the top of the stack!");
    sleep(2);
    main();
  }
}

void delStck()
{
  system("CLS");
  if (TOP == NULL)
  {
    printf("(ALERT) The Stack is empty!, cannot delete if stack does not exist!\n");
  }
  else if (TOP->link == NULL)
  {
    TOP = NULL;
    printf("(SUCCESS) The stack has been deleted!");
    stackCreated = false;
  }
  else
  {
    ptr = TOP;
    ptr = ptr->link;
    TOP = ptr;
    ptr = NULL;
    printf("(SUCCESS) The top stack has been deleted!");
  }
  sleep(2);
  main();
}

void endProg()
{
  system("CLS");
  printf("Thank you for using my program - Ace :)");
  sleep(2);
  exit(1);
}

【讨论】:

    猜你喜欢
    • 2010-11-01
    • 2020-01-22
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2023-03-22
    相关资源
    最近更新 更多