Moncel 2022-04-05 00: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 07:18
    关注

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

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

报告相同问题?

问题事件

  • 系统已结题 5月10日
  • 已采纳回答 5月3日
  • 创建了问题 4月5日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部