i am creating a word ladder program. i have already written code to find all words in the "words.txt" file that are one letter different from the user inputted word. i am supposed to "find all words in words.txt that are one letter different. for each word that is one letter different create a stack object, push on the start word and the word that is one letter different. Enqueue each of these stacks onto a queue (creating a queue of stacks)."
here is my code so far. i have each word that is one letter different pushed onto a stack but how do i enqueue each of these onto a queue?
words.txt is attached.
main code:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include "stack.h"
using namespace std;
void wordTest(string);
int main()
{
set<string> myset;
set<string>::iterator iter;
string word, search;
ifstream infile;
infile.open("words.txt");
while (infile >> word)
{
myset.insert(word);
}
//for (iter = myset.begin(); iter != myset.end(); iter++)
// cout << *iter << endl;
cout << "Enter word to search for: ";
cin >> search;
iter = myset.find(search);
if (iter == myset.end())
cout << "That word is not in the set" << endl;
else
cout << *iter << " is in the set" << endl;
wordTest(search);
infile.close();
return 0;
}
void wordTest(string search)
{
string word;
set<string> myset;
set<string>::iterator iter;
string temp;
Stack<string> object;
ifstream infile;
infile.open("words.txt");
while (infile >> word)
{
myset.insert(word);
}
temp = search;
for (int i = 97; i < 123; i++)
{
search[0] = i;
if (search[0] == temp[0])
{
continue;
}
else
{
//cout << search << endl;
iter = myset.find(search);
if (iter == myset.end())
{
continue;
}
else
{
//Stack<string> object;
object.push(temp);
object.push(search);
//object.printStack(cout);
}
}
}
search[0] = temp[0];
for (int i = 97; i < 123; i++)
{
search[1] = i;
if (search[1] == temp[1])
{
continue;
}
else
{
//cout << search << endl;
iter = myset.find(search);
if (iter == myset.end())
{
continue;
}
else
{
//Stack<string> object;
object.push(temp);
object.push(search);
//object.printStack(cout);
}
}
}
search[1] = temp[1];
for (int i = 97; i < 123; i++)
{
search[2] = i;
if (search[2] == temp[2])
{
continue;
}
else
{
//cout << search << endl;
iter = myset.find(search);
if (iter == myset.end())
{
continue;
}
else
{
//Stack<string> object;
object.push(temp);
object.push(search);
//object.printStack(cout);
}
}
}
search[2] = temp[2];
for (int i = 97; i < 123; i++)
{
search[3] = i;
if (search[3] == temp[3])
{
continue;
}
else
{
//cout << search << endl;
iter = myset.find(search);
if (iter == myset.end())
{
continue;
}
else
{
//Stack<string> object;
object.push(temp);
object.push(search);
//object.printStack(cout);
}
}
}
search[3] = temp[3];
for (int i = 97; i < 123; i++)
{
search[4] = i;
if (search[4] == temp[4])
{
continue;
}
else
{
//cout << search << endl;
iter = myset.find(search);
if (iter == myset.end())
{
continue;
}
else
{
//Stack<string> object;
object.push(temp);
object.push(search);
//object.printStack(cout);
}
}
}
search[4] = temp[4];
object.printStack(cout);
infile.close();
}
stack class code:
Code:
#ifndef STACK_H
#define STACK_H
#include <list>
#include <ostream>
using namespace std;
template<class T>
class Stack
{
public:
Stack(void); // default constructor
bool empty(void); // returns true if the Stack is empty, false otherwise
int size(void); // returns number of items in the Stack
void push(const T& item); // add item to the Stack
void pop(); // removes value from the Stack
T top(void); // returns value of item at the top of the Stack
void printStack(ostream &out); // prints the values in stack to stream out
private:
list<T> stackList;
};
template<class T>
Stack<T>::Stack(void)
{
}
template<class T>
void Stack<T>::push(const T& item)
{
stackList.insert(stackList.end(),item);
}
template<class T>
void Stack<T>::printStack(ostream &out)
{
list<T>::iterator iter;
for (iter = stackList.begin(); iter != stackList.end(); iter++)
out << *iter << endl;
}
template<class T>
bool Stack<T>::empty(void)
{
return stackList.empty();
}
template<class T>
int Stack<T>::size(void)
{
return stackList.size();
}
template<class T>
void Stack<T>::pop()
{
list<T>::iterator iter;
iter = --stackList.end();
if (!empty())
stackList.erase(iter);
}
template<class T>
T Stack<T>::top(void)
{
if (!empty())
return stackList.back();
}
#endif