自定义Deque类C ++

时间:2018-10-13 22:35:35

标签: c++ arrays class operator-overloading deque

我正在用cpp编写Deque的类,并且几乎完全完成了它。我遇到了一个奇怪的错误,在第一个insert_tail()函数调用之后,我得到了垃圾值。

这是我要参考的以下代码。 头文件:

//File: Deque.h

#ifndef DEQUE_H
#define DEQUE_H

#include <iostream>

const int CAPACITY = 5;
const int DEFAULT = -1;

class Deque
{
public:
    Deque()
    {
        front = 1;
        end = 0;
        size = 0;
    }

    int get_size() const;

    int operator[](int) const;

    void insert_head(int);
    int remove_head();

    void insert_tail(int);
    int remove_tail();

    bool is_empty() const;
    bool is_full() const;

private:
    int size;
    int x_[CAPACITY];
    int front;
    int end;
};
std::ostream & operator<<(std::ostream &, const Deque &);

#endif

实施文件:

//File: Deque.cpp

#include <iostream>
#include "Deque.h"

std::ostream & operator<<(std::ostream & cout, const Deque & dq)
{
  cout << dq.get_size() << " [ ";
  for (int i = 0; i < dq.get_size(); ++i)
  {
    cout << dq[i] << " ";
  }
  cout << "]";
  return cout;
}

int Deque::operator[](int x) const
{
    return x_[x];    
}

int Deque::get_size() const
{
    return size;
}

void Deque::insert_head(int element)
{
    if (size != CAPACITY)
    {
        x_[front] = element;
        front = (front+1) % CAPACITY;
        size++;
    }
}

int Deque::remove_head()
{
    if(is_empty())
    {
        return DEFAULT;
    }
    else
    {
        front = (front-1) % CAPACITY;
        size--;
        return x_[front];        
    } 
}

void Deque::insert_tail(int element)
{
    if (!is_full())
    {
        x_[end] = element;
        end = (end-1) % CAPACITY;
        size++;
    }
}


int Deque::remove_tail()
{
    if(is_empty())
    {
        return DEFAULT;
    }
    else
    {
        end = (end+1) % CAPACITY;
        size--;
        return x_[end];
    } 
}

bool Deque::is_empty() const
{   
    return (size == 0);
}

bool Deque::is_full() const
{
    return (size == CAPACITY);
}

测试文件:

//File: main.cpp

#include <iostream>
#include "Deque.h"

void print(const Deque & deque)
{
  static int i = 1;
  std::cout << i << ". " << deque  << ", empty: " << deque.is_empty() 
            << ", full: " << deque.is_full();
  i++;
}

void test_remove_head(Deque & deque)
{
  int x = deque.remove_head();
  print(deque); std::cout << ", x: " << x << "\n";
}

void test_remove_tail(Deque & deque)
{
  int x = deque.remove_tail();
  print(deque); std::cout << ", x: " << x << "\n";
}

void test_insert_tail(Deque & deque, int x)
{
  deque.insert_tail(x);
  print(deque); std::cout << "\n";
}

void test_insert_head(Deque & deque, int x)
{
  deque.insert_head(x);
  print(deque); std::cout << "\n";
}

void test1()
{
  Deque deque; print(deque); std::cout << "\n";
  test_remove_tail(deque);
  test_remove_head(deque);
  test_insert_tail(deque, 2);
  test_insert_tail(deque, 1);
  test_insert_tail(deque, 0);
  test_insert_head(deque, 5);
  test_insert_head(deque, 1);
  test_insert_tail(deque, 0);
  test_insert_head(deque, 2);
  test_remove_head(deque);
  test_remove_tail(deque);
  test_remove_tail(deque);
  test_remove_tail(deque);
  test_remove_head(deque);
  test_remove_head(deque); 
  test_remove_head(deque);
}

void test2()
{
  Deque deque;
  for (int i=0; i<15; i++) test_insert_head(deque, i);
  for (int i=0; i<15; i++) test_remove_tail(deque);
}

int main()
{
  test1(); test2();
  return 0;  
}

输出:

1. 0 [ ], empty: 1, full: 0
2. 0 [ ], empty: 1, full: 0, x: -1
3. 0 [ ], empty: 1, full: 0, x: -1
4. 1 [ 2 ], empty: 0, full: 0
5. 2 [ 2 0 ], empty: 0, full: 0
6. 3 [ 2 0 0 ], empty: 0, full: 0
7. 4 [ 2 5 0 4198128 ], empty: 0, full: 0
8. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
9. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
10. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
11. 4 [ 2 5 1 4198128 ], empty: 0, full: 0, x: 1
12. 3 [ 2 5 1 ], empty: 0, full: 0, x: 0
13. 2 [ 2 5 ], empty: 0, full: 0, x: 2
14. 1 [ 2 ], empty: 0, full: 0, x: 2
15. 0 [ ], empty: 1, full: 0, x: 5
16. 0 [ ], empty: 1, full: 0, x: -1
17. 0 [ ], empty: 1, full: 0, x: -1
18. 1 [ 32764 ], empty: 0, full: 0
19. 2 [ 32764 0 ], empty: 0, full: 0
20. 3 [ 32764 0 1 ], empty: 0, full: 0
21. 4 [ 32764 0 1 2 ], empty: 0, full: 0
22. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
23. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
24. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
25. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
26. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
27. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
28. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
29. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
30. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
31. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
32. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
33. 4 [ 4 0 1 2 ], empty: 0, full: 0, x: 0
34. 3 [ 4 0 1 ], empty: 0, full: 0, x: 1
35. 2 [ 4 0 ], empty: 0, full: 0, x: 2
36. 1 [ 4 ], empty: 0, full: 0, x: 3
37. 0 [ ], empty: 1, full: 0, x: 4
38. 0 [ ], empty: 1, full: 0, x: -1
39. 0 [ ], empty: 1, full: 0, x: -1
40. 0 [ ], empty: 1, full: 0, x: -1
41. 0 [ ], empty: 1, full: 0, x: -1
42. 0 [ ], empty: 1, full: 0, x: -1
43. 0 [ ], empty: 1, full: 0, x: -1
44. 0 [ ], empty: 1, full: 0, x: -1
45. 0 [ ], empty: 1, full: 0, x: -1
46. 0 [ ], empty: 1, full: 0, x: -1
47. 0 [ ], empty: 1, full: 0, x: -1

如您所见,在第4行之后,正确的插入语句未完成,并且函数“ remove_head()”和“ remove_tail()”中的x:也未收到正确的返回值。

0 个答案:

没有答案