____________________________________
First: Header file.h (functions prototype).
____________________________________
// File: StackL.h
// Linked List Stack class definition
#ifndef STACKL_H
#define STACKL_H
template <class Type>
class StackL
{
public:
StackL(); // Constructor
~StackL(); // Destructor
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
private:
// Node Class
class node
{
public:
Type e; // stack element
node *next; // pointer to next node
}; // end of class node declaration
typedef node * NodePointer;
NodePointer top; // pointer to top
};
#endif // STACKL_H
#include "StackL.cpp"
// Linked List Stack class definition
#ifndef STACKL_H
#define STACKL_H
template <class Type>
class StackL
{
public:
StackL(); // Constructor
~StackL(); // Destructor
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
private:
// Node Class
class node
{
public:
Type e; // stack element
node *next; // pointer to next node
}; // end of class node declaration
typedef node * NodePointer;
NodePointer top; // pointer to top
};
#endif // STACKL_H
#include "StackL.cpp"
________________________________________________
second: Implementation file.cpp
_____________________________________
// File:StackL.cpp
// Linked List Stack Class implementation
#include <iostream>
using namespace std;
// Class Constructor
template <class Type>
StackL<Type>::StackL()
{ top = NULL; }
// Class Destructor
template <class Type>
StackL<Type>::~StackL()
{ NodePointer cursor;
while(top != NULL)
{ cursor = top; top = top->next;
delete cursor; }
}
// return True if stack is empty
template <class Type>
bool StackL<Type>::stackIsEmpty() const
{ return (top == NULL);}
// push a node with data (v) at the top of the stack;
// the new node becomes the top node.
template <class Type>
void StackL<Type>::push(Type v)
{ NodePointer pnew = new node ;
pnew->e = v; pnew->next = top; top = pnew; }
// Pop top node. Next node becomes top node.
template <class Type>
void StackL<Type>::pop(Type &v)
{ NodePointer cursor;
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else
{ v = top->e; cursor = top;
top = top->next; delete cursor; }
}
// Retrieve top node without removing it
template <class Type>
void StackL<Type>::stackTop(Type &v) const
{ NodePointer cursor;
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else
{ v = top->e; }
}
________________________________________________
// Linked List Stack Class implementation
#include <iostream>
using namespace std;
// Class Constructor
template <class Type>
StackL<Type>::StackL()
{ top = NULL; }
// Class Destructor
template <class Type>
StackL<Type>::~StackL()
{ NodePointer cursor;
while(top != NULL)
{ cursor = top; top = top->next;
delete cursor; }
}
// return True if stack is empty
template <class Type>
bool StackL<Type>::stackIsEmpty() const
{ return (top == NULL);}
// push a node with data (v) at the top of the stack;
// the new node becomes the top node.
template <class Type>
void StackL<Type>::push(Type v)
{ NodePointer pnew = new node ;
pnew->e = v; pnew->next = top; top = pnew; }
// Pop top node. Next node becomes top node.
template <class Type>
void StackL<Type>::pop(Type &v)
{ NodePointer cursor;
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else
{ v = top->e; cursor = top;
top = top->next; delete cursor; }
}
// Retrieve top node without removing it
template <class Type>
void StackL<Type>::stackTop(Type &v) const
{ NodePointer cursor;
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else
{ v = top->e; }
}
________________________________________________
finally class test
_____________________________________
// File: StackLAppl.cpp
// Applies Linked List Stack Class to reverse a string
#include <iostream>
#include <string>
using namespace std;
#include "StackL.h"
int main()
{
StackL<char> s;
string instring, outstring;
char c;
int i;
// Read a string
cout << "Enter a string:" << endl;
getline(cin,instring);
cout << instring << endl;
outstring = "";
for (i = 0; i < instring.length(); i++)
{
c = instring.at(i);
s.push(c);
}
while(!s.stackIsEmpty())
{
s.pop(c);
outstring = outstring + c;
}
cout << outstring << endl;
s.~StackL();
return 0;
}
// Applies Linked List Stack Class to reverse a string
#include <iostream>
#include <string>
using namespace std;
#include "StackL.h"
int main()
{
StackL<char> s;
string instring, outstring;
char c;
int i;
// Read a string
cout << "Enter a string:" << endl;
getline(cin,instring);
cout << instring << endl;
outstring = "";
for (i = 0; i < instring.length(); i++)
{
c = instring.at(i);
s.push(c);
}
while(!s.stackIsEmpty())
{
s.pop(c);
outstring = outstring + c;
}
cout << outstring << endl;
s.~StackL();
return 0;
}
No comments:
Post a Comment