C++ 堆栈程序打印功能工作不正常
我刚完成这个程序,但从一开始我就注意到它并没有打印出它应该打印的所有内容。 需要先从顶部开始打印堆栈中的整数,然后从底部开始再次打印。 它正确地从上到下打印,但出于某种原因,它只从下到上打印最下面的数字。 例如,如果堆栈包含整数1、2、3、4、5、6, 其中1位于堆栈底部,6为顶部数字。 程序应打印以下内容: 顶部{654321}底部{123456}顶部 但它打印了以下内容: 顶部{654321}底部{1}顶部 以下是打印功能:C++ 堆栈程序打印功能工作不正常,c++,pointers,stack,nodes,C++,Pointers,Stack,Nodes,我刚完成这个程序,但从一开始我就注意到它并没有打印出它应该打印的所有内容。 需要先从顶部开始打印堆栈中的整数,然后从底部开始再次打印。 它正确地从上到下打印,但出于某种原因,它只从下到上打印最下面的数字。 例如,如果堆栈包含整数1、2、3、4、5、6, 其中1位于堆栈底部,6为顶部数字。 程序应打印以下内容: 顶部{654321}底部{123456}顶部 但它打印了以下内容: 顶部{654321}底部{1}顶部 以下是打印功能: void Print() const // Prints
void Print() const
// Prints stack contents to stdout in both top-to-bottom and bottom-to-top order
{
Node* temp = topPtr;
cout << "Top { ";
// Forward print
while (temp != NULL)
{
cout << temp->data << " ";
if (temp->next == NULL)
break;
temp = temp->next;
}
cout << "} Bottom Bottom { ";
// Reverse print
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->previous;
}
cout << "} Top" << endl;
} // End Print()
void Print()常量
//以从上到下和从下到上的顺序将堆栈内容打印到标准输出
{
节点*temp=topttr;
下一步;
}
不能简单
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->previous;
}
您从未设置“上一个”
,因此它保留为一些未指定的值(零)。此外,previous
未在cpp文件中的任何位置设置或选中。它应该设置在这里,尽管实际上不需要在其他任何地方
最后,
throw IsFull()
是一个异常。您可能不是有意抛出函数调用的结果。这是堆栈还是链表?与std::stack或std::vector不同,您编写自己的stack类的动机是什么?@Pubby8使用链表实现的堆栈?@Pubby8这是学校的一个基本程序,您可以使用什么,不可以使用什么,除了打印外,它在其他任何地方都能正常工作。我想这可能是一个CS作业问题,我喜欢版权2011\uuuu MyCompanyName\uuuuu。保留所有权利。
是的,应该是throw StackFull();
//
// stack.h
//
// Specification file for Stack class, a stack of integers implemented
// using doubly-linked nodes.
//
// ***** DO NOT MODIFY THIS FILE *****
//
#include <iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H
class StackEmpty { /* No Code */ };
// StackEmpty exception class - throw an object of this type when stack is empty
// Hint: there is no code for exception classes
class StackFull { /* No Code */ };
// StackFull exception class - throw an object of this type when stack is full
class StackInvalidPeek { /* No Code */ };
// StackInvalidPeek exception class - throw an object of this type when invalid peek position is used
struct Node // Node data type for storing a single stack entry along with pointers to
{ // neighboring entries (previous and next) in the stack
Node* previous; // Member variable that holds the address of the predessor node in the stack sequence
Node* next; // Member variable that holds the address of the successor node in the stack sequence
int data; // Member variable that holds the data value
};
class Stack // Implements stack of integers ADT using doubly-linked sequence of nodes
{
private:
Node* topPtr; // Points to the top node on the stack array
public:
Stack(); // Default constructor initializes empty stack
~Stack(); // Destructor deallocates all nodes from stack
// Must not create a memory leak
void Push(int n); // Pushes integer n onto top of stack.
// If unable to push, throws StackFull exception.
void Pop(); // Removes top integer from stack
// If stack is already empty, throws StackEmpty exception
bool IsEmpty() const; // Returns true if stack is empty; false otherwise
bool IsFull() const; // Returns true if stack is full; false otherwise
void MakeEmpty(); // Removes all nodes from stack leaving an empty, but usable stack
// Must not create a memory leak
int Top() const; // Returns value of top integer on stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty exception
int Size() const; // Returns number of items on stack WITHOUT modifying the stack
int Max() const; // Returns value of largest integer within stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty
int Min() const; // Returns value of smallest integer within stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty
int Peek( int n) const; // Returns stack value n levels down from top of stack. Peek(0) = Top()
// If position n does not exist, throws StackInvalidPeek
. ./******* DO NOT MODIFY ANY OF THE CODE FOR PRINT() *******/
/****** DO NOT PLACE A COPY OF PRINT() CODE IN STACK.CPP!!! *******/
void Print() const
// Prints stack contents to stdout in both top-to-bottom and bottom-to-top order
{
Node* temp = topPtr;
cout << "Top { ";
// Forward print
while (temp != NULL)
{
cout << temp->data << " ";
if (temp->next == NULL)
break;
temp = temp->next;
}
cout << "} Bottom Bottom { ";
// Reverse print
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->previous;
}
cout << "} Top" << endl;
} // End Print()
}; // End Class Stack
#endif
//
// stack.cpp
//
//
// Created by Otapia on 9/19/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <new>
#include "stack.h"
Stack::Stack() // Default constructor initializes empty stack
{
topPtr = NULL;
}
Stack::~Stack() // Destructor deallocates all nodes from stack
// Must not create a memory leak
{
Node* tempPtr;
while ( topPtr != NULL )
{
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
void Stack::Push(int n) // Pushes integer n onto top of stack.
// If unable to push, throws StackFull exception.
{
if(!IsFull())
{
Node* tempPtr = new Node;
tempPtr->data = n;
tempPtr->next = topPtr;
topPtr = tempPtr;
}
else
throw IsFull();
}
void Stack::Pop() // Removes top integer from stack
// If stack is already empty, throws StackEmpty exception
{
if (!IsEmpty())
{
Node* tempPtr;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
else
throw StackEmpty();
}
bool Stack::IsEmpty() const // Returns true if stack is empty; false otherwise
{
return(topPtr == NULL);
}
bool Stack::IsFull() const // Returns true if stack is full; false otherwise
{
Node* location;
try
{
location = new Node;
delete location;
return false;
}
catch(std::bad_alloc)
{return true; }
}
void Stack::MakeEmpty() // Removes all nodes from stack leaving an empty, but usable stack
// Must not create memory leak
{
Node* tempPtr;
while ( topPtr != NULL ) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
topPtr = NULL;
}
int Stack::Top() const // Returns value of top integer on stack WITHOUT modifying the stack
{
if(!IsEmpty())
return topPtr->data;
throw StackEmpty();
}
int Stack::Size() const // Returns number of items on stack WITHOUT modifying the stack
{
Node* temp = topPtr;
int count = 0;
while (temp != NULL)
{
temp = temp->next;
count ++;
}
return count;
}
int Stack::Max() const // Returns value of largest integer within stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty
{
int max = 0;
int n;
Node* temp = topPtr;
if(!IsEmpty())
{
while(temp != NULL)
{
n = temp->data;
if(n > max)
{
max = n;
}
temp = temp->next;
}
return max;}
else
throw StackEmpty();
}
int Stack::Min() const // Returns value of smallest integer within stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty
{int min = 100;
int n;
Node* temp = topPtr;
if(!IsEmpty())
{
while(temp != NULL)
{
n = temp->data;
if(n < min)
{
min = n;
}
temp = temp->next;
}
return min;}
else
throw StackEmpty();
}
int Stack::Peek(int n) const // Returns stack value n levels down from top of stack. Peek(0) = Top()
// If position n does not exist, throws StackInvalidPeek
{
int num = 0;
int x = 0;
Node* temp = topPtr;
if(!IsEmpty())
{
while(temp != NULL)
{
if (x >= n || temp->next == NULL)
break;
temp = temp->next;
x++;
}
if (n <= x)
{
num = temp->data;
}
else throw StackInvalidPeek();
}
else
throw StackInvalidPeek();
return num;
}
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->previous;
}
void Stack::Push(int n) // Pushes integer n onto top of stack.
// If unable to push, throws StackFull exception.
{
if(!IsFull())
{
Node* tempPtr = new Node;
tempPtr->data = n;
tempPtr->next = topPtr;
topPtr = tempPtr;
} else
throw IsFull();
}