AKI EL PUNTO CPP
Código:
//Programa para probar varias opciones de una Pila
#include "iostream"
#include "myStackLinked.h"
#include "stdio.h"
using namespace std;
void main()
{
int valor;
stackType<int> intStack;
stackType<int> tempStack;
intStack.initializeStack();
cout<<"Usted ha creado una pila de 5 posiciones";
cout<< endl;
intStack.push(23);
intStack.push(45);
intStack.push(38);
tempStack = intStack; //copia intStack en copyStack
cout<<"Elementos en la pila tempStack: ";
cout<<endl;
while(!tempStack.isEmptyStack()) //print copyStack
{
cout<<tempStack.top()<<" ";
tempStack.pop();
}
cout<<endl;
cout<< "Ingrese un nuevo elemento para la pila: ";
cin>>valor;
intStack.push(valor);
cout<< "Ingrese un nuevo elemento para la pila: ";
cin>>valor;
intStack.push(valor);
// Mostrando la pila
while(!intStack.isEmptyStack()) //print intStack
{
cout<<intStack.top()<<" ";
intStack.pop();
}
cout<<endl;
cout<< "Ingrese un nuevo elemento para la pila: ";
cin>>valor;
intStack.push(valor);
cout<<endl;
cout<< "Ingrese un nuevo elemento para la pila: ";
cin>>valor;
intStack.push(valor);
cout<<endl;
cout<<"El elemnto en el tope de intStack es: "<<intStack.top()<<endl;
//cout<<"Ahora el elemnto en el tope de intStack es: "<<intStack.top()<<endl;
//cout<<tempStack.top()<<" ";
system("pause");
}
AKI EL .H
Código:
// myStackLinked.h; Header file
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *stackTop;
};
template<class Type>
class stackType
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: stackTop = 0
void destroyStack();
//Function to remove all the elements from the stack.
//Postcondition: stackTop = 0
bool isEmptyStack();
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
// otherwise, returns false.
void push(const Type& newItem);
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of stack.
Type top();
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element
// of the stack is returned.
void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
stackType();
//constructor
//Creates an array of the size stackSize to hold the
//stack elements. The default stack size is 100.
//Postcondition: The variable list contains the base
// address of the array, stackTop = 0, and
// maxStackSize = stackSize.
stackType(const stackType<Type>& otherStack);
//copy constructor
~stackType();
//destructor
//Removes all the elements from the stack.
//Postcondition: The array (list) holding the stack
// elements is deleted.
private:
nodeType<Type> *link;
void copyStack(const stackType<Type>& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is created and
// assigned to this stack.
};
template<class Type>
void stackType<Type>::initializeStack()
{
destroyStack();
}
template<class Type>
void stackType<Type>::destroyStack()
{
nodeType<Type> *temp;
while(stackTop != NULL)
{
temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
stackTop = 0;
}
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return (stackTop==0);
}
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->stackTop = stackTop;
stackTop = newNode;
}
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop!= 0);
return (stackTop->info);
}
template<class Type>
void stackType<Type>::pop()
{
nodeType<Type> *temp;
if(!isEmptyStack()){
temp=stackTop;
stackTop=stackTop->link;
delete temp;
}
else
cerr<<"Cannot remove from an empty stack."<<endl;
}
template<class Type>
stackType<Type>::stackType()
{
stackTop = 0;
}
template<class Type>
stackType<Type>::~stackType() //destructor
{
destroyStack();
}
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
nodeType<Type> *newNode;
nodeType<Type> *current;
if(!isEmptyStack())
destroyList();
if(otherList.isEmptyStack())
stackTop = NULL;
else
{
current = otherList.stackTop;
stackTop = new nodeType<Type>;
assert(stackTop != NULL);
stackTop->info = current->info;
stackTop->link = NULL;
current = current->link;
while(current != NULL)
{
newNode = new nodeType<Type>;
assert(newNode!= NULL);
newNode->info = current->info;
newNode->link = NULL;
current = current->link;
}
}
}
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
stackTop = NULL;
copyStack(otherStack);
}
template<class Type>
const stackType<Type>& stackType<Type>::operator=
(const stackType<Type>& otherStack)
{
if(this != &otherStack)
copyStack(otherStack);
return *this;
}
#endif
weno se agradece ...
salu...