堆栈的C ++ Linked List实现

时间:2014-11-13 22:56:12

标签: c++ oop linked-list stack

我不熟悉使用C ++,并试图通过单链表实现堆栈。我相信我的推送功能正在运行,但是我的导师所写的测试功能仍然失败了。

这是我的推送功能

    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 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;

}

编辑: 这是我们需要编辑的整个.h文件

#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

这是我的讲师写的tester.cpp文件:

#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;
}

0 个答案:

没有答案