Data Structures and Algorithms Assignment: Stack Implementation and Palindrome Detection, Assignments of Software Development Methodologies

These documents will improve your skills.

Typology: Assignments

2020/2021

Uploaded on 01/30/2023

MinahilNasser
MinahilNasser 🇵🇰

5

(1)

6 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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())
{
pf3
pf4

Partial preview of the text

Download Data Structures and Algorithms Assignment: Stack Implementation and Palindrome Detection and more Assignments Software Development Methodologies in PDF only on Docsity!

DATA STRUCTURES AND ALGORITHM

ASSIGNMENT # 1

ADT STACK:

#include #include using namespace std; template 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 Stack::Stack() { maxStack = 100; top = - 1; items = new ItemType[500]; } template Stack::Stack(int maxStack) { this->maxStack = maxStack; top = - 1; items = new ItemType[maxStack]; } template Stack::~Stack() { delete[] items; } template bool Stack::isEmpty() { return (top == - 1); } template bool Stack::isFull() { return (top == maxStack - 1); } template void Stack::push(ItemType newItem) { if (isFull()) {

cout << "StacK Overflow" << endl; exit(1); } top++; items[top] = newItem; } template void Stack::pop(ItemType& item) { if (isEmpty()) { cout << "Stack Underflow" << endl; exit(1); } item = items[top]; top--; }

DRIVER CODE:

#include #include #include #include"stack.h" using namespace std; int main() { Stack 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)