堆栈和队列容量过满(c ++)

时间:2015-05-07 17:54:44

标签: c++ stack queue

经过数小时的研究,阅读,查看示例代码......我被困(或炒)。我有一个内存泄漏,它没有任何内容泄漏到我的Stack和Queue'列表中。前提是我试图通过使用Stacks& amp;来创建一个单词或短语是回文的查找。队列。当使用一个单词来测试我的程序时,堆栈&排队'列表'填上。我可以用一个词来解决它...但它需要为一个短语工作。

#include <iostream>
#include <ostream>
#include <stdlib.h>
using namespace std;

#include "Stack.h"
#include "stack.cpp"
#include "Queue.h"
#include "queue.cpp"

void blankspace(char *s, char *t);

int main(void)
{
    Stack palinS;
    Queue palinQ;
    char mess[80];
    char bmess[80];
    int i;
    int n;
    int j;
    i = 0;
    n = 0;
    j = 0;

    cout << "Please enter a word or phrase: ";
    cin >> mess;

    blankspace(mess, bmess);

    while (bmess != NULL){
        palinS.push(bmess[i]);
        palinQ.enqueue(bmess[i]);
        i++;
    }

    n = sizeof(bmess);
    while (!palinS.empty()){
        palinS.top();
        palinQ.front();
        if (palinS.top() == palinQ.front())
            j++;
        palinS.pop();
        palinQ.dequeue();
    }

    if (j++ == n){
        cout << "  You have a palindrome!!!";
    }
    else {
        cout << "  SORRY... The word/phrase is NOT a palindrome.";
    }

    return (0);
}

void blankspace(char *s, char *t)
{
    while (*s != '\0'){
        if (*s != ' ') *t = *s;
        t++;
        s++;
    }

1 个答案:

答案 0 :(得分:1)

@RyanP真的值得赞扬这个答案。
看了他的建议之后,我把代码修改为......

while (bmess [i] != '/O'){
    palinS.push(bmess [i]);
    palinQ.front(bmess [i]);
    i++;
    }

这解决了我永无止境的Stack&amp; amp;排队&#39;列表&#39;创建。感谢所有评论!

相关问题