#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;
}
为什么无法对Q .base进行复制
出现无效字符串的原因是什么