主题:用栈和队列来实现判断回文字符串,得不到想要的结果
//我思考了好久,觉得问题出在while(...) {i++;} 这里但是还是找不到为什么会有问题
//无论输入的是不是回文字符串,i 的值都是0 ,不知道是什么原因
//代码如下:
#include<malloc.h>
#include<stdio.h>
#define MAXSIZE 100
//定义栈结构体
typedef struct
{
char str1[MAXSIZE];
int top;
}Stack, *PStack;
//栈的初始化
PStack Init_Stack()
{
PStack S;
S = (PStack)malloc(sizeof(Stack));
if (S)
{
S->top = -1;
}
return S;
}
//判断栈空
int Empty_Stack(PStack S)
{
if (S->top == -1)
{
return 1;
}
else
{
return 0;
}
}
//入栈
int Push_Stack(PStack S, char ch)
{
if (S->top = MAXSIZE-1)
{
return 0;//栈满不能入栈
}
else
{
S->top++;
S->str1[S->top] = ch;
return 1;
}
}
//出栈
char Pop_Stack(PStack S)
{
char ret;
if (Empty_Stack(S))
{
return 0;
}
else
{
ret = S->str1[S->top];
S->top--;
return ret;
}
}
//定义队列结构体
typedef struct
{
char str2[MAXSIZE];
int head, tail;
int length;
}Queue, *PQueue;
//队列初始化
PQueue Init_Queue()
{
PQueue Q;
Q = (PQueue)malloc(sizeof(Queue));
if (Q)
{
Q->head = Q->tail = 0;
Q->length = 0;
}
return Q;
}
//判断队列是否为空
int Empty_Queue(PQueue Q)
{
if (Q->length == 0)
{
return 1;
}
else
{
return 0;
}
}
//入队
int In_Queue(PQueue Q, char ch)
{
if (Q->length == MAXSIZE)
{
return 0;//队列满不能入队
}
else
{
Q->tail++;
Q->str2[Q->tail] = ch;
Q->length++;
return 1;
}
}
//出队
char Out_Queue(PQueue Q)
{
char ret;
if (Empty_Queue(Q))
{
return 0;
}
else
{
Q->head++;
ret = Q->str2[Q->head];
Q->length--;
return ret;
}
}
int main()
{
PStack s;
PQueue q;
int i = 0, count;
char ch;
s = Init_Stack();
q = Init_Queue();
printf("Please input a string(0 to end):");
while ((ch = getchar()) != '@')
{
Push_Stack(s, ch);
In_Queue(q, ch);
}
count = q->length;
printf("%d\n",count);
while (!Empty_Stack(s) && Pop_Stack(s) == Out_Queue(q))
{
i++;//这句执行了没有???
}
printf("%d\n",i);//这里的i为什么一直是0
if (i == count)
{
printf("Is palindrome!\n");
}
else
{
printf("Is not palindrome!\n");
}
return 0;
}
//无论输入的是不是回文字符串,i 的值都是0 ,不知道是什么原因
//代码如下:
#include<malloc.h>
#include<stdio.h>
#define MAXSIZE 100
//定义栈结构体
typedef struct
{
char str1[MAXSIZE];
int top;
}Stack, *PStack;
//栈的初始化
PStack Init_Stack()
{
PStack S;
S = (PStack)malloc(sizeof(Stack));
if (S)
{
S->top = -1;
}
return S;
}
//判断栈空
int Empty_Stack(PStack S)
{
if (S->top == -1)
{
return 1;
}
else
{
return 0;
}
}
//入栈
int Push_Stack(PStack S, char ch)
{
if (S->top = MAXSIZE-1)
{
return 0;//栈满不能入栈
}
else
{
S->top++;
S->str1[S->top] = ch;
return 1;
}
}
//出栈
char Pop_Stack(PStack S)
{
char ret;
if (Empty_Stack(S))
{
return 0;
}
else
{
ret = S->str1[S->top];
S->top--;
return ret;
}
}
//定义队列结构体
typedef struct
{
char str2[MAXSIZE];
int head, tail;
int length;
}Queue, *PQueue;
//队列初始化
PQueue Init_Queue()
{
PQueue Q;
Q = (PQueue)malloc(sizeof(Queue));
if (Q)
{
Q->head = Q->tail = 0;
Q->length = 0;
}
return Q;
}
//判断队列是否为空
int Empty_Queue(PQueue Q)
{
if (Q->length == 0)
{
return 1;
}
else
{
return 0;
}
}
//入队
int In_Queue(PQueue Q, char ch)
{
if (Q->length == MAXSIZE)
{
return 0;//队列满不能入队
}
else
{
Q->tail++;
Q->str2[Q->tail] = ch;
Q->length++;
return 1;
}
}
//出队
char Out_Queue(PQueue Q)
{
char ret;
if (Empty_Queue(Q))
{
return 0;
}
else
{
Q->head++;
ret = Q->str2[Q->head];
Q->length--;
return ret;
}
}
int main()
{
PStack s;
PQueue q;
int i = 0, count;
char ch;
s = Init_Stack();
q = Init_Queue();
printf("Please input a string(0 to end):");
while ((ch = getchar()) != '@')
{
Push_Stack(s, ch);
In_Queue(q, ch);
}
count = q->length;
printf("%d\n",count);
while (!Empty_Stack(s) && Pop_Stack(s) == Out_Queue(q))
{
i++;//这句执行了没有???
}
printf("%d\n",i);//这里的i为什么一直是0
if (i == count)
{
printf("Is palindrome!\n");
}
else
{
printf("Is not palindrome!\n");
}
return 0;
}