一个简单的c++回文程序,运行后老提示遇到问题需要关闭

来源:百度知道 编辑:UC知道 时间:2024/06/06 13:00:57
程序如下:
#include<iostream.h>
#include<stdio.h>
struct list /定义节点结构
{
char data;
list *next;
};
class Stack /定义一个栈
{
list *ptr;
public:
Stack(){ptr=NULL;}/栈置空
void push(char); /入栈
char pop(); /出栈
int empty()
{
if(ptr==NULL) return 1;
else return 0;
}
};
void Stack::push(char x)
{
list *newnode=new list;
newnode->data=x;
newnode->next=ptr;
ptr=newnode;

}
char Stack::pop()
{
list *top;
char value;
value=ptr->data;
top=ptr;
ptr=ptr->next;
delete top;
return value;
}
class Queue /定义队列
{
public: list *ptrf,*ptrb;
public:
Queue(){ptrf=ptrb=NULL;}/队列置空
void enqueue(char); /入队
char dequeue(); /出队
};
void Queue::enqueue(char x)
{
list *newnode=new list;

代码有两个错误:
1.队列出列函数有错
char Queue::dequeue()
{
list *tmp;
char value;
value=ptrf->data;
tmp=ptrf;
ptrf=ptrf->next;
delete tmp;
return value;
}
应该为:
char Queue::dequeue()
{
list *tmp;
char value;
//value=ptrf->data;
value=ptrb->data;
//tmp=ptrf;
tmp=ptrb;
//ptrf=ptrf->next;
ptrb=ptrb->next;
delete tmp;
return value;
}

另外:
while ((ch=getchar())!='.')
这一句改成
while ((ch=getchar())!='\n')