Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;堆栈的链表实现 我是C++新手,我尝试通过单链表实现一个栈。我相信我的推送功能正在工作,但我的导师编写的测试功能一直失败_C++_Oop_Linked List_Stack - Fatal编程技术网

C++;堆栈的链表实现 我是C++新手,我尝试通过单链表实现一个栈。我相信我的推送功能正在工作,但我的导师编写的测试功能一直失败

C++;堆栈的链表实现 我是C++新手,我尝试通过单链表实现一个栈。我相信我的推送功能正在工作,但我的导师编写的测试功能一直失败,c++,oop,linked-list,stack,C++,Oop,Linked List,Stack,这是我的推送功能 void push(const int val){ node <T> *newTop = new node <T>; if(m_top == 0){ cout << "m_top == 0 " << val << endl; newTop->m_value = val; newTop->m_next = NULL;

这是我的推送功能

    void push(const int val){
     node <T> *newTop = new node <T>;
     if(m_top == 0){
        cout << "m_top == 0 " << val << endl;
        newTop->m_value = val;
        newTop->m_next = NULL;
        m_top = newTop;
        m_size++;
    }
    else{
        cout << "m_top != 0 " << val << endl;
        newTop->m_value = val;
        newTop->m_next = m_top;
        m_top = newTop;
        m_size++;
    }
} 
void push(常量int val){
node*newTop=新节点;
如果(m_top==0){

寻求调试帮助的cout问题(“此代码为什么不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题说明的问题对其他读者没有用。请参阅:如何创建最小、完整且可验证的示例。(对不起,您的
操作您的
push
功能看起来正常(基于可见的代码)。可能问题发生在转换为字符串的过程中?您可以自己运行测试吗?如果可以,实际生成的字符串是什么?向我们展示operator@nhoughto不,是为堆栈类定义的。
std::ostream&operator
void test_push_empty_size(){
   sll_stack<unsigned int> s;
   stringstream s1;

   if (!s.empty())
      throw stack_tester_exception(__func__,__LINE__);

   for (unsigned int i = 1; i <= 10; i++)
      s.push(i*10);

   if (s.empty())
      throw stack_tester_exception(__func__,__LINE__);

   if (s.size() != 10)
      throw stack_tester_exception(__func__,__LINE__);

   s1<<s;
   cout << "s1: " << s1 << endl;
   if (s1.str() != "{TOP_100,90,80,70,60,50,40,30,20,10}")
       throw stack_tester_exception(__func__,__LINE__); //Where it fails!!

   cout << __func__ << " passed." << endl;
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;


class stack_empty_exception {

public:
stack_empty_exception(const string & msg, unsigned int line) {
    _msg = msg; _line = line;
}
string what() {
    ostringstream s;
    s << "stack empty in function: " << _msg << " at line: " << _line;
    return s.str();
}
private:
stack_empty_exception();
string          _msg;
unsigned int    _line;
};

template <class T> class node{
public:
node() : m_value(0), m_next(0) {}
node( int val, node *nxt ) :
m_value(val), m_next(nxt) {}

int     m_value;
node *  m_next;
};

template <class T> class sll_stack{
public:

sll_stack()
{
    m_size = 0;
    m_top = 0;
}


sll_stack(vector<T> &vec)
{
    int sizeOfInput = vec.size();
    cout << "push: ";
    for(int i = 0; i<sizeOfInput; i++){
            push(vec[i]);
            cout << vec[i];
    }
    cout << endl;
}

~sll_stack(){
    clear();
}

void clear(){

}

unsigned int    size() const{
    return m_size;
}

bool        empty() const
{
    if(m_size == 0) return true;
    else return false;
}

void    push(const int val)
{
     node <T> *newTop = new node <T>;
     if(m_top == 0){
        cout << "m_top == 0 " << val << endl;
        newTop->m_value = val;
        newTop->m_next = NULL;
        m_top = newTop;
        m_size++;
    }
    else{
        cout << "m_top != 0 " << val << endl;
        newTop->m_value = val;
        newTop->m_next = m_top;
        m_top = newTop;
        m_size++;
    }
}


T   pop(){
    T data;
    if(empty() == true) throw stack_empty_exception(__func__,__LINE__);
    else{
        node <T> * old = m_top;
        m_top = m_top -> m_next;
        m_size--;
        data = old->m_value;
        delete(old);
        return data;
    }
    return NULL;
}


T peek(){
    return NULL;
}


friend std::ostream & operator<< (std::ostream &s, const sll_stack &stk){
    return s;
}


private:
 node <T>       *m_top;
 unsigned int   m_size;

 sll_stack (const sll_stack &);
 sll_stack& operator= (sll_stack &);
};
#endif
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <exception>
#include <vector>
#include "sll_stack.h"

using namespace std;

unsigned int input[] = {1,2,3,4,5,6,7,8};
std::vector<unsigned int> values (input, input + sizeof(input) / sizeof(unsigned int) );

class stack_tester_exception{
public:
stack_tester_exception (const string & msg, unsigned int line) : _msg(msg), _line(line) {}
string what() const
{
    ostringstream s;
    s << _line;
    return _msg + " line number: " + s.str();
}
private:
stack_tester_exception();
string _msg;
unsigned int _line;
};

void test_push_empty_size(){
sll_stack<unsigned int> s;
stringstream s1;

if (!s.empty())
    throw stack_tester_exception(__func__,__LINE__);

for (unsigned int i = 1; i <= 10; i++)
    s.push(i*10);

if (s.empty())
    throw stack_tester_exception(__func__,__LINE__);

if (s.size() != 10)
    throw stack_tester_exception(__func__,__LINE__);

s1<<s;
cout << "s1: " << s1 << endl;
if (s1.str() != "{TOP_100,90,80,70,60,50,40,30,20,10}")
    throw stack_tester_exception(__func__,__LINE__);

cout << __func__ << " passed." << endl;
}

void test_pop_all_elements(){
stringstream s1;
sll_stack<unsigned int> s(values);
unsigned int expected = values.size();
unsigned int popped = 0;

s1 << s;
if (s1.str() != "{TOP_8,7,6,5,4,3,2,1}")
    throw stack_tester_exception(__func__,__LINE__);

for (unsigned int i = values.size(); i>0; i--)
{
    if (s.size() != i)
        throw stack_tester_exception(__func__,__LINE__);

    popped = s.pop();

    if (popped != expected)
        throw stack_tester_exception(__func__,__LINE__);

    expected--;
}
cout << __func__ << " passed." << endl;
}

void test_peek(){

sll_stack<unsigned int> s(values);
unsigned int expected = values.size();
unsigned int top = 0;

for (unsigned int i = 0; i < values.size(); i++)
{
    top = s.peek();
    if (top != expected)
        throw stack_tester_exception(__func__,__LINE__);

    if (s.size() != 8)
        throw stack_tester_exception(__func__,__LINE__);

}
cout << __func__ << " passed." << endl;
}

void test_exceptions(){

sll_stack<unsigned int> s;

try
{
    unsigned int x = s.pop();
    // We shouldn't get here
    std::cout << x << std::endl;
    throw stack_tester_exception(__func__,__LINE__);
}
catch (stack_empty_exception & e)
{
    std::cout << "exceptions working for pop"<< std::endl;
    // do nothing, this is the correct case
}

try
{
    unsigned int x = s.peek();
    // We shouldn't get here
    std::cout << x << std::endl;
    throw stack_tester_exception(__func__,__LINE__);
}
catch (stack_empty_exception & e)
{
    std::cout << "exceptions working for peek"<< std::endl;
    // do nothing, this is the correct case
}

}

int main (){
unsigned int tests_passed = 0;

try
{
    test_push_empty_size();
    tests_passed++;

    test_pop_all_elements();
    tests_passed++;

    test_peek();
    tests_passed++;

    test_exceptions();
    tests_passed++;
}
catch (stack_tester_exception const &e)
{
    cout << "Failed test case: " << e.what() << std::endl;
}
catch (...)
{
    cout << "Caught unhandled exception." << std::endl;
}
cout << "Passed: " << tests_passed << endl;
return 0;
}