creating a queue of stacks

This is a discussion on creating a queue of stacks within the C++ Programming forums, part of the General Programming Boards category; i am creating a word ladder program. i have already written code to find all words in the "words.txt" file ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    creating a queue of stacks

    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
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You make a queue of stack objects and push_back each stack object onto your queue after you build it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 10:09 PM
  5. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21