Question

22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what...

22.7 Lab: Word ladder

Write a word ladder program. Read this wikipedia article that describes what a word ladder is: Word Ladder Your program must use a list to store the dictionary of words and then use stacks of strings and a queue of these stacks to solve for and output the word ladder. You are required to use the Standard Template Library (STL) list, stack, and queue. You must implement the WordLadder class. The class is declared in WordLadder.h. You may not add any data members or add any public member functions to this class. You will most likely want to add private helper functions though. Algorithm for finding a word ladder create a Stack containing just the first word in the ladder enqueue this Stack on to a Queue of Stacks while this Queue of Stacks is not empty get the word on top of the front Stack of this Queue for each word in the dictionary if the word is off by just one letter from the top word create a new Stack that is a copy of the front Stack and push on this off-by-one word found if this off-by-one word is the end word of the ladder, this new Stack contains the entire word ladder. You are done! otherwise, enqueue this new Stack and remove this word from the dictionary dequeue this front stack if the Queue is empty and you didn't find the word ladder, a word ladder does not exist for these 2 words

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The screenshots of code implementing the wordLadder class along with output generated corresponding to a few test cases are attached below:

The C++ code is given below. The wordLadder class has been implemented using queue of stack of strings as mentioned in question. This class may be moved to separate .h file as per the needs.

#include <bits/stdc++.h>

using namespace std;

// put this class in WordLadder.h

class WordLadder{

list<string> dictionary;

//only constructor is public

public:

WordLadder(list<string> dictionary,const string &start, const string &end){

this->dictionary=dictionary;

output(start,end);

}

private:

// helper function

// implements algorithm

void output(const string &start, const string &end)

{

stack<string> words;

words.push(start);

queue<stack<string>> ladder;

ladder.push(words);

dictionary.remove(start);

while (!ladder.empty()) {

for (list<string>::iterator i = dictionary.begin(); i != dictionary.end(); ++i){

if (Differ_by_one(*i, ladder.front().top())){

if (*i == end){

stack<string> output;

while (!ladder.front().empty()){

output.push(ladder.front().top());

ladder.front().pop();

}

while (!output.empty()){

cout << output.top() << " ";

output.pop();

}

cout << *i <<endl;

return;

}

else{

stack<string> temp = ladder.front();

temp.push(*i);

ladder.push(temp);

i = dictionary.erase(i);

}

}

}

ladder.pop();

}

cout << "No word ladder exists for the pair of words '" <<start<<"' & '"<<end<<"'"<< endl;

}

//checks if the two strings differ by just one character

bool Differ_by_one(const string &dictionaryWord, const string &stackWord){

int count = 0;

for (int i = 0; i < stackWord.size(); ++i)

if (dictionaryWord.at(i) != stackWord.at(i))

++count;

if (count <= 1)

return true;

return false;

}

};

//=================class ends================

//testing in main

int main() {

//testing

list<string>dict;

dict.push_back("pot");

dict.push_back("hat");

dict.push_back("top");

dict.push_back("hot");

dict.push_back("hop");

dict.push_back("pat");

WordLadder(dict,"hot","top");

WordLadder(dict,"pot","top");

WordLadder(dict,"pat","top");

return 0;

}

Add a comment
Know the answer?
Add Answer to:
22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers) C++ Stack and Queue should contain Insertion (Push/Enqueue) Deletion (Pop/Dequeue) Print/Display Sort (Stacks: Value or Color, Queue: Alphabetical) Search (Contains, position, and how many instances as three separate functions) Clear/empty Size (if empty, print that it’s empty) Each data structure should contain a set of overloaded operators (Respect innate object behavior): ‘+’ (addition) that allows you to add two objects of the same...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT