鱼C论坛

 找回密码
 立即注册
查看: 5491|回复: 6

[已解决]c队列c语言实现救救孩子

[复制链接]
发表于 2021-4-18 16:54:42 | 显示全部楼层 |阅读模式
55鱼币
求求大佬帮我看看错在哪里


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE  1
#define OVERFLOW -2
#define FALSE 0
#define MAXSIZE 10
typedef struct Queue{

        int *base;        // 队列首地址
        int front;      
        int rear;
      

}Queue,*LQueue;//这是我定义的队列结构体

typedef struct Node
{
        int data;  
        struct Node* next;
}Node,*LNode;//节点的结构体

int InitQueue(LQueue Q){
          Q->base=(int*)malloc(sizeof(int)*MAXSIZE);
          Q->front = Q->rear = NULL;
      
         return TRUE;
}
int Fullor(LQueue Q)
{
                 Q->front =Q->rear +1;
                 return(FALSE);
}
int InQueue(LQueue &Q,int &elem)
{
        if ((Q->rear + 1) % MAXSIZE == (Q->front))
                return FALSE;
        Q->base[Q->rear] = elem;
        Q->rear = (Q->rear + 1) % MAXSIZE;
        return TRUE;
}

int OutQueue(LQueue &Q,int &elem)
{
        if (Q->front == Q->rear)
                return FALSE;
        elem= Q->base[Q->front];
        Q->front = (Q->front + 1) % MAXSIZE;
        return TRUE;
}

int printQueue(LQueue Q)
{
        printf("the queue is:\n");
        for(int i =Q->front;i<=Q->rear;++i)
        {
                printf("%d",Q->base[i]);
        }
        return TRUE;
}
int main(void)
{
         LQueue L;
         InitQueue(L);
         int i;
         int elem;
         int h;
         printf("input is~:");
         for(i=0;i<10;i++)
         {
                elem=i+25;
                InQueue(L,elem);
                return TRUE;
         }
         printQueue(L);
         printf("请问你要舍弃多少个数字\n");
         scanf("%d", &h);
         while (h != 0)
         {
                OutQueue(L, elem);
                h--;
         }
         printQueue(L);
         return TRUE;
}
最佳答案
2021-4-18 16:54:43
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define TRUE  1
  5. #define OVERFLOW -2
  6. #define FALSE 0
  7. #define MAXSIZE 10
  8. typedef struct Queue{

  9.         int *base;        // 队列首地址
  10.         int front;      
  11.         int rear;
  12.       

  13. }Queue,*LQueue;//这是我定义的队列结构体

  14. typedef struct Node
  15. {
  16.         int data;  
  17.         struct Node* next;
  18. }Node,*LNode;//节点的结构体

  19. int InitQueue(LQueue &Q){ // 因为要初始化Q的内存空间,所以传址
  20.         // 先初始化Q
  21.         Q = (LQueue)malloc(sizeof(Queue));
  22.           Q->base=(int*)malloc(sizeof(int)*MAXSIZE);
  23.           Q->front = Q->rear = NULL;
  24.       
  25.          return TRUE;
  26. }
  27. int Fullor(LQueue Q)
  28. {
  29.                  Q->front =Q->rear +1;
  30.                  return(FALSE);
  31. }
  32. int InQueue(LQueue &Q,int &elem)
  33. {
  34.         if ((Q->rear + 1) % MAXSIZE == (Q->front))
  35.                 return FALSE;
  36.         Q->base[Q->rear] = elem;
  37.         Q->rear = (Q->rear + 1) % MAXSIZE;
  38.         return TRUE;
  39. }

  40. int OutQueue(LQueue &Q,int &elem)
  41. {
  42.         if (Q->front == Q->rear)
  43.                 return FALSE;
  44.         elem= Q->base[Q->front];
  45.         Q->front = (Q->front + 1) % MAXSIZE;
  46.         return TRUE;
  47. }

  48. int printQueue(LQueue Q)
  49. {
  50.         printf("the queue is:\n");
  51.         for(int i =Q->front;i<=Q->rear;++i)
  52.         {
  53.                 printf("%d",Q->base[i]);
  54.         }
  55.         return TRUE;
  56. }
  57. int main(void)
  58. {
  59.          LQueue L; // 指针变量,要初始化内存空间
  60.          InitQueue(L);
  61.          int i;
  62.          int elem;
  63.          int h;
  64.          printf("input is~:");
  65.          for(i=0;i<10;i++)
  66.          {
  67.                 elem=i+25;
  68.                 InQueue(L,elem);
  69.                 return TRUE;
  70.          }
  71.          printQueue(L);
  72.          printf("请问你要舍弃多少个数字\n");
  73.          scanf("%d", &h);
  74.          while (h != 0)
  75.          {
  76.                 OutQueue(L, elem);
  77.                 h--;
  78.          }
  79.          printQueue(L);
  80.          return TRUE;
  81. }
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-18 16:54:43 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define TRUE  1
  5. #define OVERFLOW -2
  6. #define FALSE 0
  7. #define MAXSIZE 10
  8. typedef struct Queue{

  9.         int *base;        // 队列首地址
  10.         int front;      
  11.         int rear;
  12.       

  13. }Queue,*LQueue;//这是我定义的队列结构体

  14. typedef struct Node
  15. {
  16.         int data;  
  17.         struct Node* next;
  18. }Node,*LNode;//节点的结构体

  19. int InitQueue(LQueue &Q){ // 因为要初始化Q的内存空间,所以传址
  20.         // 先初始化Q
  21.         Q = (LQueue)malloc(sizeof(Queue));
  22.           Q->base=(int*)malloc(sizeof(int)*MAXSIZE);
  23.           Q->front = Q->rear = NULL;
  24.       
  25.          return TRUE;
  26. }
  27. int Fullor(LQueue Q)
  28. {
  29.                  Q->front =Q->rear +1;
  30.                  return(FALSE);
  31. }
  32. int InQueue(LQueue &Q,int &elem)
  33. {
  34.         if ((Q->rear + 1) % MAXSIZE == (Q->front))
  35.                 return FALSE;
  36.         Q->base[Q->rear] = elem;
  37.         Q->rear = (Q->rear + 1) % MAXSIZE;
  38.         return TRUE;
  39. }

  40. int OutQueue(LQueue &Q,int &elem)
  41. {
  42.         if (Q->front == Q->rear)
  43.                 return FALSE;
  44.         elem= Q->base[Q->front];
  45.         Q->front = (Q->front + 1) % MAXSIZE;
  46.         return TRUE;
  47. }

  48. int printQueue(LQueue Q)
  49. {
  50.         printf("the queue is:\n");
  51.         for(int i =Q->front;i<=Q->rear;++i)
  52.         {
  53.                 printf("%d",Q->base[i]);
  54.         }
  55.         return TRUE;
  56. }
  57. int main(void)
  58. {
  59.          LQueue L; // 指针变量,要初始化内存空间
  60.          InitQueue(L);
  61.          int i;
  62.          int elem;
  63.          int h;
  64.          printf("input is~:");
  65.          for(i=0;i<10;i++)
  66.          {
  67.                 elem=i+25;
  68.                 InQueue(L,elem);
  69.                 return TRUE;
  70.          }
  71.          printQueue(L);
  72.          printf("请问你要舍弃多少个数字\n");
  73.          scanf("%d", &h);
  74.          while (h != 0)
  75.          {
  76.                 OutQueue(L, elem);
  77.                 h--;
  78.          }
  79.          printQueue(L);
  80.          return TRUE;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-18 19:43:25 | 显示全部楼层

谢谢呀可是运行结果只到input is~就没了
可以再帮我解答一下咩
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-18 20:11:27 | 显示全部楼层
YinLee 发表于 2021-4-18 19:43
谢谢呀可是运行结果只到input is~就没了
可以再帮我解答一下咩

我总不可能帮你把代码全敲完吧。自己不动手?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-18 20:34:23 | 显示全部楼层
ba21 发表于 2021-4-18 20:11
我总不可能帮你把代码全敲完吧。自己不动手?

这就是我敲完的代码自己完全看不出来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-18 20:35:20 | 显示全部楼层
ba21 发表于 2021-4-18 20:11
我总不可能帮你把代码全敲完吧。自己不动手?

还是谢谢你呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-18 21:25:50 | 显示全部楼层

这注释了几处,自己看看吧。这代码没发继续看下去。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define TRUE  1
  5. #define OVERFLOW -2
  6. #define FALSE 0
  7. #define MAXSIZE 10
  8. typedef struct Queue{

  9.         int *base;        // 队列首地址
  10.         int front;      
  11.         int rear;
  12.       

  13. }Queue,*LQueue;//这是我定义的队列结构体

  14. typedef struct Node
  15. {
  16.         int data;  
  17.         struct Node* next;
  18. }Node,*LNode;//节点的结构体

  19. int InitQueue(LQueue &Q){ // 因为要初始化Q的内存空间,所以传址
  20.         // 先初始化Q
  21.         Q = (LQueue)malloc(sizeof(Queue));
  22.           Q->base=(int*)malloc(sizeof(int)*MAXSIZE);
  23.           Q->front =Q->rear=NULL;
  24.                           

  25.       
  26.          return TRUE;
  27. }
  28. int Fullor(LQueue Q)
  29. {
  30.                  Q->front =Q->rear +1;
  31.                  return(FALSE);
  32. }
  33. int InQueue(LQueue Q,int elem) // 这里何必要加&
  34. {
  35.         if ((Q->rear + 1) % MAXSIZE == (Q->front)) // Q->rear InitQueue不是=NULL吗?
  36.                 return FALSE;
  37.         Q->base[Q->rear] = elem;
  38.         Q->rear = (Q->rear + 1) % MAXSIZE;
  39.         return TRUE;
  40. }

  41. int OutQueue(LQueue &Q,int &elem)
  42. {
  43.         if (Q->front == Q->rear)
  44.                 return FALSE;
  45.         elem= Q->base[Q->front];
  46.         Q->front = (Q->front + 1) % MAXSIZE;
  47.         return TRUE;
  48. }

  49. int printQueue(LQueue Q)
  50. {
  51.         printf("the queue is:\n");
  52.         for(int i =Q->front;i<=Q->rear;++i)
  53.         {
  54.                 printf("%d",Q->base[i]);
  55.         }
  56.         return TRUE;
  57. }
  58. int main(void)
  59. {
  60.          LQueue L; // 指针变量,要初始化内存空间
  61.          InitQueue(L);
  62.          int i;
  63.          int elem;
  64.          int h;
  65.          printf("input is~:");
  66.          for(i=0;i<10;i++)
  67.          {
  68.                 elem=i+25;
  69.                 InQueue(L,elem);
  70.                 // return TRUE; // 退出??????
  71.          }
  72.          printQueue(L);
  73.        //  printf("请问你要舍弃多少个数字\n");
  74.         // scanf("%d", &h);
  75.         // while (h != 0)
  76.         // {
  77.           //      OutQueue(L, elem);
  78.          //       h--;
  79.        //  }
  80.        //  printQueue(L);
  81.          return TRUE;
  82. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 22:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表