Logo 
Search:

C++ Programming Forum

Ask Question   UnAnswered
Home » Forum » C++ Programming       RSS Feeds

Stack Underflow?!

  Asked By: Aya    Date: Sep 21    Category: C++ Programming    Views: 2570
  

I'm getting a stack underflow output on an endless loop with this code. I have included both the application code as well as the code for stackt class used.
The Application c++ code:


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "Stackt.h"

void Towers (int N, Stackt<char>& Source, Stackt<char>& Target, Stackt<char>& Aux);
void Display (int Top, Stackt<char>& Source, Stackt<char>& Target, Stackt<char>& Aux);
char Temporary;

void main()
{
int N;
Stackt<char> A, B, C;
cout<<"Please Enter the Number of Disks"<<endl;
cin>>N;
for (int i = 0; i < N; i++)
A.push(i+1);
Towers(N, A, B, C);
}

void Towers (int N, Stackt<char>& Source, Stackt<char>& Target, Stackt<char>& Aux)
{
if (N == 1)
{
Source.pop(Temporary);
Target.push(Temporary);
Display(N, Source, Target, Aux);
}
else
{
Towers (N-1, Source, Aux, Target);
Source.pop(Temporary);
Target.push(Temporary);
Towers (N-1, Aux, Target, Source);
}
}

void Display (int Top, Stackt<char>& Source, Stackt<char>& Target, Stackt<char>& Aux)
{
cout<<"Moving Disk "<<Top<<" from tower "<<"A"<<" to tower "<<"B"<<endl;
cout<<"A:";
while (Source.stackIsEmpty() == true)
{
Source.pop(Temporary);
cout<<setw(5)<<Temporary<<endl;
}

cout<<"B:";
while (Aux.stackIsEmpty() == true)
{
Aux.stackTop(Temporary);
cout<<setw(5)<<Temporary<<endl;

}

cout<<"C:";
while (Target.stackIsEmpty() == true)
{
Target.pop(Temporary);
cout<<setw(5)<<Temporary<<endl;
}


}



The Class files
Header
// File: Stackt.h
// Stack template class definition.
// Dynamic array implementation

#ifndef STACKT_H
#define STACKT_H

template <class Type>

class Stackt
{
public:

Stackt(int nelements = 128); // Constructor
Stackt (const Stackt<Type> &); // Copy Constructor
~Stackt(); // Destructor

// Member Functions
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
bool stackIsFull() const; // Test for Full stack

private:
Type *stack; // pointer to dynamic array
int top, MaxSize;

};

#endif // STACKT_H
#include "Stackt.cpp"


The cpp files
// File: Stackt.cpp
// Stack template class implementation

#include <iostream>
using namespace std;


// Constructor with argument, size is nelements, default is 128
template <class Type>
Stackt<Type>::Stackt(int nelements)
{ MaxSize = nelements; stack = new Type[MaxSize]; top = -1; }

// Copy Constructor
template <class Type>
Stackt <Type>::Stackt(const Stackt<Type> &original)
:MaxSize(original.MaxSize), top(original.top)
{
stack = new Type[MaxSize];
for (int i = 0; i <= top; i++) stack[i] = original.stack[i];
}


// Destructor
template <class Type>
Stackt<Type>::~Stackt()
{ delete [] stack;}

// Push
template <class Type>
void Stackt<Type>::push(Type v)
{
if(stackIsFull()) cout << "Stack Overflow" << endl;
else stack[++top] = v;
}

// Pop
template <class Type>
void Stackt<Type>::pop(Type &v)
{
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else v = stack[top--];
}

// Retrieve stack top without removing it
template <class Type>
void Stackt<Type>::stackTop(Type &v) const
{
if(stackIsEmpty()) cout << "Stack Underflow";
else v = stack[top];
}

// Test for Empty stack
template <class Type>
bool Stackt<Type>::stackIsEmpty() const
{ return (top < 0); }

// Test for Full stack
template <class Type>
bool Stackt<Type>::stackIsFull() const
{ return (top >= (MaxSize-1)); }

Share: 



Tagged: