Moncel 2022-04-05 08:49 采纳率: 86.7%
浏览 31
已结题

字符队列入队时“无效字符串”问题

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

#define ERROR 0
#define FALSE 0
#define TURE 1
#define OK 1
#define MAXSIZE 20

typedef char ElemType;
typedef int Status;

typedef struct StackNode {
    ElemType data;             //指针域
    struct StackNode* next;   //数据域
}*LinkStack;

typedef struct {
    char base[MAXSIZE];
    int front;
    int rear;
}LoopQueue;

void IniStack(LinkStack& S);// 初始化
Status Push(LinkStack& S, char e);//入栈
Status Pop(LinkStack& S, ElemType& e);//出栈
Status StackTraver(LinkStack S);//遍历

Status InitQueue(LoopQueue& Q);//初始化
Status QueueEmpty(LoopQueue Q);//判空
Status EnQueue(LoopQueue& Q, ElemType e);//插入
Status DeQueue(LoopQueue& Q, ElemType& e);//删除
Status QueueTraver(LoopQueue Q);//遍历

int main() {
    LinkStack S;
    LoopQueue Q;
    IniStack(S);
    InitQueue(Q);
    int i = 0,j = 0;
    char str1[20] = "hello";
    char *str;
    str = str1;
    printf("请输入一段文字:");
    gets_s(str,20);
    while (str[i] != '\0') {
        Push(S, str[i]);
        EnQueue(Q, str[i]);
        i++;
        if (i == 20) {
            printf("超出范围");
            return FALSE;
        }
    }
    /*while (j != i) {

        j++
    }*/

    return 0;
}

void IniStack(LinkStack& S) {// 初始化
    S = NULL;
}
Status Push(LinkStack& S, char e) {//入栈
    LinkStack p;
    p = (LinkStack)malloc(sizeof(StackNode));
    if (!p) return FALSE;
    p->data = e;
    p->next = S;
    S = p;
    return 0;
}
Status Pop(LinkStack& S, ElemType& e) {//出栈
    LinkStack p;
    if (!S) return ERROR;
    p = S;
    S = S->next;
    e = p->data;
    free(p);
    return OK;
}
Status StackTraver(LinkStack S) {//遍历
    while (S != NULL) {
        printf("%d\t", S->data);
        S = S->next;
    }
    printf("\n");
    return OK;
}


Status InitQueue(LoopQueue& Q) {//初始化
    Q.front = Q.rear = 0;
    return OK;
}
Status QueueEmpty(LoopQueue Q) {//判空
    if (Q.front == Q.rear)
        return TURE;
    else
        return FALSE;
}
Status EnQueue(LoopQueue& Q, ElemType e) {//入队
    if ((Q.rear + 1) % MAXSIZE == Q.front)
        return FALSE;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE;
    return OK;
}
Status DeQueue(LoopQueue& Q, ElemType& e) {//出队
    if (QueueEmpty(Q) == 1)
        return FALSE;
    e = Q.base[Q.front];
    Q.front = (Q.front + 1) % MAXSIZE;
    return OK;
}
Status QueueTraver(LoopQueue Q) {//遍历
    if (QueueEmpty(Q) == 1)
        return FALSE;
    int p = Q.front;
    while (p != Q.rear) {
        printf("%d\t", Q.base[p]);
        p = (p + 1) % MAXSIZE;
    }
    printf("\n");
    return 0;
}

img


为什么无法对Q .base进行复制
出现无效字符串的原因是什么

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-04-05 15:18
    关注

    试运行了上面的代码,没发现题主出现的问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月11日
  • 已采纳回答 5月3日
  • 创建了问题 4月5日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度