You are on page 1of 4

DATA STRUCTURES AND ALGORITHM

ASSIGNMENT # 1

ADT STACK:
#include <iostream>
#include<stack> using
namespace std; template
<class ItemType> class
Stack { private: int
top; int maxStack;
ItemType* items;
public:
Stack();
Stack(int); ~Stack();
bool isEmpty(); bool
isFull(); void push(ItemType
newItem); void
pop(ItemType& item);
};
template<class ItemType>
Stack<ItemType>::Stack()
{ maxStack = 100;
top = -1;
items = new ItemType[500];
}
template<class ItemType>
Stack<ItemType>::Stack(int maxStack)
{ this->maxStack = maxStack;
top = -1;
items = new ItemType[maxStack];
}
template<class ItemType> Stack<ItemType>::~Stack()
{
delete[] items;
}
template<class ItemType> bool
Stack<ItemType>::isEmpty()
{ return (top == -1);
}
template<class ItemType> bool
Stack<ItemType>::isFull()
{ return (top == maxStack -
1);
}
template<class ItemType>
void Stack<ItemType>::push(ItemType newItem)
{ if (isFull())
{
cout << "StacK Overflow" << endl;
exit(1); } top++;
items[top] = newItem;
}
template<class ItemType>
void Stack<ItemType>::pop(ItemType& item)
{
if (isEmpty())
{
cout << "Stack Underflow" << endl;
exit(1); }
item = items[top];
top--; }

DRIVER CODE:
#include <iostream>
#include<string>
#include<fstream>
#include"stack.h"

using namespace std;


int
main()
{
Stack <char> s;
string word,push=" ", pop=" ",greatestPal=" ";//greatestPalindrome
char choice, ch;
int totalPal=0,totalWords=0,temp=0;//totalPalindromes
ofstream myFile("input.txt");
ifstream myReadFile("input.txt");
ofstream inFile("output.txt");
do {
cout << "Enter word: ";
cin >> word;
myFile << word << endl;
myReadFile >> word; for (int i = 0; i <
word.length(); i++) {
ch = word[i];
inFile << ch << endl;

s.push(ch);
cout << ch << endl;
push += ch;
}
cout << "Word pop from stack: " << endl;
for (int i = 0; i < word.length(); i++)
{

s.pop(ch);
pop += ch;
cout << ch << endl;
}
if (push == pop)
{
cout << "This word is a palendrom" << endl;
totalPal += 1;
//checking greatest palindrome
if (temp < word.length())
{
temp = word.length();
greatestPal = word;
}
push.clear();
pop.clear();
}
else
{
cout << "This word is not a palendrom" << endl;
push.clear();
pop.clear();
}
cout << "Do you want to continue (y/n)?";
cin >> choice;
totalWords += 1;
} while ((choice == 'Y') || (choice == 'y'));

cout << "Total words are: " << totalWords << endl;
cout << "Total palendroms are: " << totalPal << endl;
cout << "Greatest palindrome is: " << greatestPal << endl;
system("pause"); return 0; }

OUTPUT:

You might also like