C ++对象传递错误

时间:2017-03-31 00:58:49

标签: c++

所以我尝试使用upRotate和downRotate函数上下旋转堆栈。代码编译,但是当我运行它时,我做的第一次旋转很好,但是我的所有其他旋转都缺少堆栈的一部分。当我检查我传入函数的原始堆栈时,即使我按值传递了Stack对象,该堆栈也发生了变化。这是我的代码。

#include "Stack.h"

Stack upRotate(Stack s)
{
 Stack t,r;
 t.push(s.pop());
 while(!s.empty())
  r.push(s.pop());
 while(!r.empty())
  t.push(r.pop());
 return t;
}

Stack downRotate(Stack s)
{
 Stack t,r;
 while(!s.empty())
  r.push(s.pop());
 s.push(r.pop());
 while(!r.empty())
  t.push(r.pop());
 t.push(s.pop());
 return t;
}

int main() {
 Stack s;
  s.push(1);
  s.push(1);
  s.push(2);
  s.push(3);
  s.print();
  downRotate(s).print();
  upRotate(s).print();

}

这是Stack类的代码。

#ifndef STACK_H
#define STACK_H

#include <cstdlib>
#include <iostream>


/**
 * StackNode class: contains key and pointer to next node. Constructor creates
 * new node and loads key into it.
 */
class StackNode {
  public:
    int key;
    StackNode *next;
    StackNode(int);
};

StackNode::StackNode(int key) {
  this->key  = key;
  this->next = NULL;
}


/**
 * Stack class: contains pointer to top node. Constructor sets top node to NULL.
 * Supports push, pop, peek operations.  Pop adds a node to the top of the stack;
 * push removes node from the top of the stack and returns its key.  Peek views
 * the key of the node on the top of the stack.
 */
class Stack {
  private:
    StackNode *top;
  public:
    Stack();
    void push(int);
    int  peek();
    int  pop();
    bool empty();
    void print();
};


Stack::Stack() {
  top = NULL;
}


/**
 * Delete top node and return key.
 * @return  the key of the top node
 */
int Stack::pop() {
  int popkey = -1;
  StackNode *popme = top;
  if (top)
    popkey = top->key;
  top = popme->next;
  delete popme; 
  return popkey;
}


/**
 * Add node to top of the stack.
 * @param key  key to insert into new top node
 */
void Stack::push(int key) {
  StackNode *newtop = new StackNode(key); 
  newtop->next = top;
  top = newtop;
}


/**
 * Delete top node and return key.
 * @return  the key of the top node
 */
int Stack::peek() {
  int key = -1;
  if (top)
    key = top->key;
  return key;
}


/**
 * Tell if stack is empty.
 * @return  true if stack is empty
 */
bool Stack::empty() {
  if (top)
    return false;
  return true;
}


/**
 * Print the stack from top to bottom.
 */
void Stack::print() {
  StackNode *node = top;
  while (node) {
    std::cout << '\t' << node->key;
    node = node->next;
  }
  std::cout << std::endl;
}

#endif 

1 个答案:

答案 0 :(得分:1)

您应该编写Stack的复制构造函数。

Stack::Stack(const Stack &s)
{
    Stack temp;
    this->top = NULL;
    if(s.top != NULL)
    {
        StackNode *node = s.top;
        while (node) {
            temp.push(node->key);
            node = node->next;
        }   
    }
    if(temp.top != NULL)
    {
        StackNode *node1 = temp.top;
        while (node1) {
            this->push(node1->key);
            node1 = node1->next; 
        }
    }
    //free temp's heap space
    StackNode *node2 = temp.top; 
    StackNode *node3 = node2;
    while(node2)
    {
        node2 = node2->next;
        delete node3;
        node3 = node2;
    }
}