Stack Data Structure
Posted by mark123
Last Updated: May 04, 2013
#include "Node.h"
#include<iostream>
//using namespace std;
Node::Node()
{
setName(' ');
this->next=NULL;
}
Node::Node(char n,Node * nptr)
{
 setName(n);
 setNext(nptr);
}
Node::~Node()
{

}
void Node::setName(char n)
{
 name=n;
}
char Node::getName()const
{
 return this->name;
}
void Node::setNext(Node * nptr)
{
this->next=nptr;
}
Node * Node::getNext()const
{
return this->next;
}

#include "Stack.h"

Stack::Stack()
{
this->top=NULL;
count=0;
}
Stack::Stack(Node * t)
{
this->top=t;
}
Stack::~Stack()
{
 while(top!=NULL)
this->pop();
}
bool Stack::isEmpty()const
{
if(this->top==NULL)
{
return true;
}
else
{
return false;
}

}
char Stack::stackTop()const
{
 return top->getName();
}
bool Stack::pop()
{
  Node * tmp=this->top;
  if(isEmpty())
  {
 cout<<"Cannot pop from Empty stack!!";
 return false;
  }
  else
  {
this->top=top->getNext();
delete tmp;
 count=count-1;
return true;
  }
}

bool Stack::push(char n)
{
   Node * newNode=new Node(n,NULL);
   if(isEmpty())
   {
 top=newNode;
 count=count+1;
 return true;
 
   }
   else
   {
     newNode->setNext(top);
top=newNode;
 count=count+1;
return true;
   }

}
int Stack::stackCount()
{
return count;
}

#include "Stack.h"

using namespace std;
void header();
void getData();

int main()
{
header();
getData();
//menu();
system("pause");
return 0;
}
void getData(){
char choice;
char data[50];
char n=' ';
char a[50]=" ";
char b[50]=" ";
int size;
int i=0;
static int score=0;
int test=0;
Stack * s1=new Stack();
Stack * s2=new Stack();
Stack * s3=new Stack();


do
{
cout<<"Please enter a string to test if it is a palindrome:";
cin.getline(data,50);
size=strlen(data);
if(size<1)
cout<<"You must enter a character.Please try again."<<endl;
for(int i=0;i<size;i++)
{
 if(isspace(data[i]))
 {
cout<<"changing value."<<endl;
system("cls");
test=0;
 }
 else if(!isspace(data[i]))
 {
data[i]=tolower(data[i]);
    s1->push(data[i]);
s2->push(data[i]);
test=1;
 }

}
}while((size<1)||(test==0));
n=s1->stackTop();
while(!s1->isEmpty())
 {
n=s1->stackTop();
s1->pop();
   s3->push(n); 
 }
while((s2->stackCount()!=0)&&(s3->stackCount()!=0))
{
 a[i]=s2->stackTop();
  s2->pop();
  b[i]=s3->stackTop();
  s3->pop();
  i++;
}


 
  
cout<<"Your word spelt forward"<<" "<<a<<endl;
cout<<"Your word spelt in reverse:"<<" "<<b<<endl;
if(strcmp(a,b)==NULL)
{
cout<<"congratulations you have a palindrome"<<endl;
score=score+10;
}
else
{
cout<<"This is not a palindrome"<<endl;
score=score-20;
}
cout<<"Your score so far:"<<score<<endl;
cout<<"Would you like to play again?(Y/N):";
cin>>choice;
fflush(stdin);
choice=tolower(choice);
if(choice=='y')
{
getData();
}
else if(choice=='n')
{
cout<<"Thank you for playing the game. Please feel free to come back quickly.Exiting...";
exit(0);
}
else
{
cout<<"Error directing to main by default."<<endl;
system("pause");
system("cls");
getData();
}


}
void header()
{  
cout<<"=============================================================="<<endl;
cout<<"|  A           GAMING        PRODUCTION                       |"<<endl;
cout<<"|   STACK   ATTACK  The palindrome game you cant get enough of |"<<endl;
cout<<"==============================================================="<<endl;
cout<<" Instructions: 10 points for palindromes, 20 points lost for non-palindrome"<<endl;
system("pause");
system("cls");
}