How to add a Queue in a List?

Hi everybody!

I'm a student of Computer Science and i have a problem.

We need to do a Queue(with strings) and a List(with the queues).

I've done the queue but the problem is how to add the queue into a List.

I'm very lost, anybody can help me? Thanks.

Show your current code. Particularly, show the definition of List.
We need to do a Queue(with strings) and a List(with the queues).

I've done the queue but the problem is how to add the queue into a List.
It's not really clear what you're asking.

What do you mean by add the queue into a List.?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <list>
#include <queue>
#include <string>

int main()
{
    typedef std::queue<std::string> queue_type ; // queue of strings
    typedef std::list<queue_type> list_type ; // list of queues

    list_type list(2) ; // list containg two empty queues

    // push some strings into the two queues in the list
    for( std::string s : { "abc", "defg", "hi" } ) list.front().push(s) ;
    for( std::string s :  { "jklmn", "op", "qrst", "uvw" } ) list.back().push(s) ;

    queue_type q ;
    q.push("hello") ;
    q.push("world") ;

    // add a third (non-empty) queue at the front of the list
    list.push_front(q) ;

    // etc.
}
Sorry, but my english is very limited.

Well, let's see..

Queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

class nodo {
   public:
    nodo(string c, nodo *sig = NULL) {
       letra = c;
       siguiente = sig;
    }

   private:
    string letra;
    nodo *siguiente;
        
   friend class cola;
};
 
typedef nodo *pnodo;
 
class cola {
   public:
    //Constructor Cola
    cola() : ultimo(NULL), primero(NULL) {}
	//Destructor Cola
    ~cola();
    //AÒadir char a la cola
    void Anadir(string c);
	//Leer la cabeza de la cola[OJO, DESTRUYE ELEMENTO DE LA COLA]
    string Leer();
	
    
   private:
    pnodo primero, ultimo;
};


And Queue.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "cola.h"

cola::~cola() {
   while(primero) Leer();
}

void cola::Anadir(string c) {
   pnodo nuevo;
    
   /* Crear un nodo nuevo */
   nuevo = new nodo(c);
   /* Si la cola no estaba vacÌa, aÒadimos el nuevo a continuaciÛn de ultimo */
   if(ultimo) ultimo->siguiente = nuevo;
   /* Ahora, el ˙ltimo elemento de la cola es el nuevo nodo */
   ultimo = nuevo;
   /* Si primero es NULL, la cola estaba vacÌa, ahora primero apuntar· tambiÈn al nuevo nodo */
   if(!primero) primero = nuevo;
}

string cola::Leer() {

   pnodo nodo; /* variable auxiliar para manipular nodo */
   string c;      /* variable auxiliar para retorno */
   
   /* Nodo apunta al primer elemento de la pila */
   nodo = primero;
   if(!nodo) return 0; /* Si no hay nodos en la pila retornamos 0 */
   /* Asignamos a primero la direcciÛn del segundo nodo */
   primero = nodo->siguiente;
   /* Guardamos el valor de retorno */
   c = nodo->letra; 
   /* Borrar el nodo */
  
   delete nodo;
   
   /* Si la cola quedÛ vacÌa, ultimo debe ser NULL tambiÈn*/
   if(!primero) ultimo = NULL;
   return c;
}


Now, i need to add the queue into a list.

An example:

Queue:
1st string: "thing1"
2nd string: "abcd"
3rd string: "accc"
4th string: "super"

These strings should go into a queue ordered by the first character of the string:
1st queue: abcd,accc
2º queue: super
3rd queue: thing1

These queues i need to add into a list ordered alphabetically:
List:
a--> abcd,accc
b-->empty
c-->empty
….
….
s--> super
t-->thing1
….
….



I hope you understand what i want…
All the examples of implementation of lists is with char or int but with a queue i don't find anywhere.

Thanks a lot.
Last edited on
All the examples of implementation of lists is with char or int but with a queue i don't find anyone.

It shouldn't be any different. Just create a list that's for items of type queue, rather than int. Then just use the same interface.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <map>
#include <deque>
#include <string>
#include <algorithm>

int main()
{
    const std::string my_strings[] =
    {
        "thing1", "abcd", "accc", "super",
        "consider", "using", "the", "standard", "library",
        "it", "makes", "life", "a", "lot", "easier"
    };

    // http://www.cplusplus.com/reference/map/map/
    // http://www.cplusplus.com/reference/deque/deque/
    std::map< char, std::deque<std::string> > dict ;

    // add the strings to the dictionary in any order
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::string& str : my_strings )
        if( !str.empty() ) dict[ str[0] ].push_back(str) ;

    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto& pair : dict ) // sort strings under each alphabet
    {
        auto& deque = pair.second ;
        // http://www.cplusplus.com/reference/algorithm/sort/
        std::sort( std::begin(deque), std::end(deque) ) ;
    }

    for( const auto& pair : dict ) // print out the contemts
    {
        std::cout << "key: '" << pair.first << "'  values: [ " ;
        for( const std::string& str : pair.second )
            std::cout << "'" << str << "' " ;
        std::cout << "]\n" ;
    }
}

http://ideone.com/h2j7En
Topic archived. No new replies allowed.