C++ 回文堆栈队列不工作
我正在尝试制作一个程序来检查文本文件中的文本是否是回文。但我遇到了一个问题,找不出问题所在 请帮帮我C++ 回文堆栈队列不工作,c++,stack,queue,palindrome,C++,Stack,Queue,Palindrome,我正在尝试制作一个程序来检查文本文件中的文本是否是回文。但我遇到了一个问题,找不出问题所在 请帮帮我 #include <iostream> #include <string> #include <fstream> using namespace std; typedef char ItemType; struct Node { ItemType item; Node* next; }; class FullQueue {}; class
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
typedef char ItemType;
struct Node {
ItemType item;
Node* next;
};
class FullQueue {};
class EmptyQueue {};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue();
~Queue();
Queue(const Queue& original);
const Queue& operator=(const Queue& rhs);
void copyQueue(const Queue& original);
void makeEmpty();
bool isEmpty() const;
bool isFull() const;
void enqueue(const ItemType& item);
bool dequeue(ItemType&);
void print(ostream& out) const;
};
Queue::Queue() {
front = NULL;
rear = NULL;
}
Queue::Queue() {
makeEmpty();
}
void Queue::makeEmpty() {
Node* tmp;
while (front != NULL) {
tmp = front;
front = front->next;
delete tmp;
}
rear = NULL;
}
Queue::Queue(const Queue& original) {
copyQueue(original);
}
const Queue& Queue::operator = (const Queue& right) {
if (this != &right) {
makeEmpty();
copyQueue(right);
}
return *this;
}
void Queue::copyQueue(const Queue& original) {
Node* tmp = original.front;
front = NULL;
rear = NULL;
while (tmp != NULL) {
enqueue(tmp->item);
tmp = tmp->next;
}
}
bool Queue::isFull() const {
Node* tmp;
try {
tmp = new Node;
delete tmp;
return false;
}
catch (bad_alloc e) {
return true;
}
}
void Queue::enqueue(const ItemType& data) {
if (isFull())
throw FullQueue();
else {
Node* tmp = new Node;
tmp->item = data;
tmp->next = NULL;
if (front = NULL) {
front = tmp;
rear = tmp;
}
else{
rear->next = tmp;
rear = tmp;
}
}
}
bool Queue::dequeue(ItemType& data) {
if (isEmpty()) {
throw EmptyQueue();
return false;
}
else {
Node* tmp = front;
data = front->item;
front = front->next;
if (front == NULL)
rear = NULL;
delete tmp;
return true;
}
}
void Queue::print(ostream& out) const {
Queue q;
q = *this;
out << "Data in a queue : ";
while (!q.isEmpty()) {
ItemType data;
q.dequeue(data);
out << data << " ";
}
out << endl;
}
class FullStack {};
class EmptyStack {};
class Stack {
private:
Node* top;
public:
Stack();
~Stack();
Stack(const Stack& original);
const Stack& operator = (const Stack& rhs);
void copyStack(const Stack& original);
void makeEmpty();
bool isEmpty() const;
bool isFull() const;
void push(const ItemType& item);
void pop();
ItemType getTop() const;
void print(ostream& out) const;
};
Stack::Stack() {
top = NULL;
}
Stack::Stack() {
makeEmpty();
}
void Stack::makeEmpty() {
Node* tmp;
while (top != NULL) {
tmp = top;
top = top->next;
delete tmp;
}
}
Stack::Stack(const Stack& original) {
copyStack(original);
}
const Stack& Stack::operator = (const Stack& right) {
if (this != &right) {
makeEmpty();
copyStack(right);
}
return *this;
}
void Stack::copyStack(const Stack& original) {
if (original.top == NULL)
top = NULL;
else {
top = new Node;
top->item = original.top->item;
Node* scanPtr = original.top->next;
Node* tmp = top;
while (scanPtr != NULL) {
tmp->next = new Node;
tmp = tmp->next;
tmp->item = scanPtr->item;
scanPtr = scanPtr->next;
}
tmp->next = NULL;
}
}
bool Stack::isEmpty() const {
return (top == NULL);
}
bool Stack::isFull() const {
Node* tmp;
try {
tmp = new Node;
delete tmp;
return false;
}
catch (bad_alloc e) {
return true;
}
}
void Stack::push(const ItemType& data) {
if (isFull())
throw FullStack();
else {
Node* tmp = new Node;
tmp->item = data;
tmp->next = top;
top = tmp;
}
}
void Stack::pop() {
if (isEmpty())
throw EmptyStack();
else {
Node* tmp = top;
top = top->next;
delete tmp;
}
}
ItemType Stack::getTop() const {
if (isEmpty())
throw EmptyStack();
else
return top->item;
}
void Stack::print(ostream& out) const {
Stack s;
s = *this;
out << "Data in a stack : ";
while (!s.isEmpty()) {
out << s.getTop() << " ";
s.pop();
}
out << endl;
}
bool isPalindrome(Queue &q, Stack &s) {
while (!q.isEmpty()) {
ItemType data1;
ItemType data2;
q.dequeue(data1);
data2 = s.getTop();
s.pop();
data1 = tolower(data1);
data2 = tolower(data2);
if (data1 != data2)
return false;
}
return true;
}
int main() {
char str[256];
cout << "Enter file name : ";
cin >> str;
ifstream fs(str);
while (1) {
Queue q;
Stack s;
if (fs.getline(str,256) && NULL)
break;
for (int i = 0; str[i] != NULL; i++) {
q.enqueue(str[i]);
s.push(str[i]);
}
s.print(cout);
q.print(cout);
if (isPalindrome(q, s)) {
cout << "\"" << str << "\" is a palindrome." << endl;
cout << endl;
}
else {
cout << "\"" << str << "\" is a not palindrome." << endl;
cout << endl;
}
}
fs.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
typedef char ItemType;
结构节点{
项目类型项目;
节点*下一步;
};
类FullQueue{};
类EmptyQueue{};
类队列{
私人:
节点*前端;
节点*后部;
公众:
队列();
~Queue();
队列(常量队列和原始队列);
常量队列和运算符=(常量队列和rhs);
无效复制队列(常量队列和原始队列);
void makeEmpty();
bool isEmpty()常量;
bool isFull()常量;
无效排队(常量项目类型和项目);
bool出列(ItemType&);
无效打印(ostream&out)常数;
};
队列::队列(){
front=NULL;
后=空;
}
队列::队列(){
makeEmpty();
}
无效队列::makeEmpty(){
节点*tmp;
while(前面!=NULL){
tmp=前部;
前=前->下一步;
删除tmp;
}
后=空;
}
队列::队列(常量队列和原始队列){
复制队列(原件);
}
常量队列和队列::运算符=(常量队列和右侧){
如果(此!=&右){
makeEmpty();
复制队列(右);
}
归还*这个;
}
无效队列::复制队列(常量队列和原始队列){
节点*tmp=original.front;
front=NULL;
后=空;
while(tmp!=NULL){
排队(tmp->项目);
tmp=tmp->next;
}
}
bool队列::isFull()常量{
节点*tmp;
试一试{
tmp=新节点;
删除tmp;
返回false;
}
捕获(错误分配){
返回true;
}
}
无效队列::排队(const ItemType和data){
如果(isFull())
抛出FullQueue();
否则{
Node*tmp=新节点;
tmp->item=数据;
tmp->next=NULL;
if(front=NULL){
前端=tmp;
后部=tmp;
}
否则{
后->下一步=tmp;
后部=tmp;
}
}
}
bool Queue::出列(ItemType和data){
if(isEmpty()){
抛出EmptyQueue();
返回false;
}
否则{
节点*tmp=前端;
数据=前端->项目;
前=前->下一步;
if(front==NULL)
后=空;
删除tmp;
返回true;
}
}
无效队列::打印(ostream&out)常量{
队列q;
q=*这个;
下一步;
节点*tmp=top;
while(scanPtr!=NULL){
tmp->next=新节点;
tmp=tmp->next;
tmp->item=scanPtr->item;
scanPtr=scanPtr->next;
}
tmp->next=NULL;
}
}
布尔堆栈::isEmpty()常量{
返回(top==NULL);
}
bool Stack::isFull()常量{
节点*tmp;
试一试{
tmp=新节点;
删除tmp;
返回false;
}
捕获(错误分配){
返回true;
}
}
void Stack::push(const ItemType和data){
如果(isFull())
抛出FullStack();
否则{
Node*tmp=新节点;
tmp->item=数据;
tmp->next=顶部;
top=tmp;
}
}
void Stack::pop(){
if(isEmpty())
抛出空包();
否则{
节点*tmp=top;
顶部=顶部->下一步;
删除tmp;
}
}
ItemType堆栈::getTop()常量{
if(isEmpty())
抛出空包();
其他的
返回顶部->项目;
}
空堆栈::打印(ostream&out)常量{
堆栈s;
s=*这;
用一个新的键盘替换你的键盘,这似乎有Ctrl+V锁定的问题。顺便说一句,你认为你的代码有什么问题?在涉及所有这些自定义代码之前,你不想用std::string
测试你的算法吗?