回文堆栈队列不工作

时间:2014-03-27 15:58:54

标签: 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 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;
}

请帮助我,我的大脑会爆炸!

0 个答案:

没有答案