can someone help me, i have a c++ code about palindrome, but i don\'t want to us
ID: 3621668 • Letter: C
Question
can someone help me, i have a c++ code about palindrome, but i don't want to use template, just normal class and stack using linked list. here is my code . gonna rate livesaver thx :).//the Stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
using std::cout;
#include <cassert>
#include "StackNode.h"
template < class T >
class Stack {
public:
Stack(); // default constructor
~Stack(); // destructor
void push( T & ); // insert item in stack
T pop(); // remove item from stack
bool isEmpty() const; // is the stack empty?
void print() const; // output the stack
StackNode< T > *getTopPtr() const { return topPtr; }
private:
StackNode< T > *topPtr; // pointer to fist StackNode
};
// Member function definitions for class Stack
template < class T >
Stack< T >::Stack() { topPtr = 0; }
template < class T >
Stack< T >::~Stack()
{
StackNode< T > *tempPtr, *currentPtr = topPtr;
while ( currentPtr != 0 ) {
tempPtr = currentPtr;
currentPtr = currentPtr -> getNextPtr();
delete tempPtr;
}
}
template < class T >
void Stack< T >::push( T &d )
{
StackNode< T > *newPtr = new StackNode< T >( d, topPtr );
assert( newPtr != 0 ); // was memory allocated?
topPtr = newPtr;
}
template < class T >
T Stack< T >::pop()
{
assert( !isEmpty() );
StackNode< T > *tempPtr = topPtr;
topPtr = topPtr -> nextPtr;
T poppedValue = tempPtr -> data;
delete tempPtr;
return poppedValue;
}
template < class T >
bool Stack< T >::isEmpty() const { return topPtr == 0; }
template < class T >
void Stack< T >::print() const
{
StackNode< T > *currentPtr = topPtr;
if ( isEmpty() ) // Stack is empty
cout << "Stack is empty ";
else { // Stack is not empty
cout << "The stack is: ";
while ( currentPtr != 0 ) {
cout << currentPtr -> data << ' ';
currentPtr = currentPtr -> nextPtr;
}
cout << ' ';
}
}
#endif
//the StackNode.h
#ifndef STACKND_H
#define STACKND_H
template< class T > class Stack; // forward declaration
template < class T >
class StackNode {
friend class Stack< T >;
public:
StackNode( const T & = 0, StackNode * = 0 );
T getData() const;
void setNextPtr( StackNode *nPtr ) { nextPtr = nPtr; }
StackNode *getNextPtr() const { return nextPtr; }
private:
T data;
StackNode *nextPtr;
};
// Member function definitions for class StackNode
template < class T >
StackNode< T >::StackNode( const T &d, StackNode< T > *ptr )
{
data = d;
nextPtr = ptr;
}
template < class T >
T StackNode< T >::getData() const { return data; }
#endif
//the main.cpp
#include <iostream>
using std::cout;
using std::cin;
#include <cctype>
#include <cstring>
#include "stack.h"
int main()
{
Stack< char > charStack;
char c, string1[ 80 ], string2[ 80 ];
int i = 0;
cout << "Enter a sentence: ";
while ( ( c = static_cast< char >( cin.get() ) ) != ' ' )
if ( isalpha( c ) ) {
string1[ i++ ] = c;
charStack.push( c );
}
string1[ i ] = '';
i = 0;
while ( !charStack.isEmpty() )
string2[ i++ ] = charStack.pop();
string2[ i ] = '';
if ( strcmp( string1, string2 ) == 0 )
cout << " The sentence is a palindrome ";
else
cout << " The sentence is not a palindrome ";
return 0;
}
Explanation / Answer
Dear,
Here is the code
#include <iostream>
using namespace std;
class DynStack
{
private:
// Structure for stack nodes
struct StackNode
{
char value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public:
// Constructor
DynStack()
{ top = NULL; }
// Destructor
~DynStack();
// Stack operations
void push(char);
void pop(char &);
bool isEmpty();
};
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
DynStack::~DynStack()
{
StackNode *nodePtr, *nextNode;
// Position nodePtr at the top of the stack.
nodePtr = top;
// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************
void DynStack::push(char num)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
void DynStack::pop(char &num)
{
StackNode *temp; // Temporary pointer
// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty. ";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool DynStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
int main()
{
// Create a DynIntStack object.
DynStack stack;
char catchVar,string2[10];
char string1[]="madam"; // Push 5, 10, and 15 onto the stack.
cout <<"Pushing";
int j=0;
while(string1[j]!='')
{ stack.push(string1[j]);
j++;
}
int i=0;
// Pop the values off the stack and dispaly them.
cout << "Popping... ";
while(!stack.isEmpty())
{
stack.pop(catchVar);
string2[i]=catchVar;
i++;
}
string2[j]='';
if ( strcmp( string1, string2 ) == 0 )
cout << " The sentence is a palindrome ";
else
cout << " The sentence is not a palindrome ";
system("pause");
return 0;
}
Hope this will help you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.