YinLee 发表于 2021-4-18 16:54:42

c队列c语言实现救救孩子

求求大佬帮我看看错在哪里


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

#define TRUE1
#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 = 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 + 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);
      }
      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;
}

ba21 发表于 2021-4-18 16:54:43

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

#define TRUE1
#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的内存空间,所以传址
        // 先初始化Q
        Q = (LQueue)malloc(sizeof(Queue));
          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 = 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 + 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);
      }
      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;
}

YinLee 发表于 2021-4-18 19:43:25

ba21 发表于 2021-4-18 18:24


谢谢呀可是运行结果只到input is~就没了
可以再帮我解答一下咩

ba21 发表于 2021-4-18 20:11:27

YinLee 发表于 2021-4-18 19:43
谢谢呀可是运行结果只到input is~就没了
可以再帮我解答一下咩

我总不可能帮你把代码全敲完吧。自己不动手?

YinLee 发表于 2021-4-18 20:34:23

ba21 发表于 2021-4-18 20:11
我总不可能帮你把代码全敲完吧。自己不动手?

这就是我敲完的代码{:5_104:}自己完全看不出来

YinLee 发表于 2021-4-18 20:35:20

ba21 发表于 2021-4-18 20:11
我总不可能帮你把代码全敲完吧。自己不动手?

还是谢谢你呀

ba21 发表于 2021-4-18 21:25:50

YinLee 发表于 2021-4-18 20:35
还是谢谢你呀

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

#define TRUE1
#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的内存空间,所以传址
      // 先初始化Q
      Q = (LQueue)malloc(sizeof(Queue));
          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)) // Q->rear InitQueue不是=NULL吗?
                return FALSE;
      Q->base = 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 + 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);
      }
      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;
}
页: [1]
查看完整版本: c队列c语言实现救救孩子