How to enqueue and dequeue in mult-idimensional array?

Its Semester break already I've been practicing what I've learn in data structures subject. I'm trying to make a program that en queue and de queue data structure in multi-dimensional array? I've been doing this for days and i can only do it in single dimension array. Can somebody Help me point out what are my mistakes in my codes i would really appreciate it.TIA
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


	const int LIMIT = 3;
	const int TRIP = 3;
	std::string ROUTE[TRIP];
	int BUSNO[LIMIT][3];

	/*class Queue
	{
	Private:
		Node front;
		Node rear;
		int counter;

	Public:
		Queue();
		~Dequeue();
		bool isEmpty();
		bool isFull();
		void Enqueue(Item:item);
		Item Dequeue(Item);
		int count();
	};
	bool Queue::isEmpty()
	{
		bool flag = false;
		if (counter == 0)
		{
			flag = true;

		}
		return flag;
	}
	bool Queue::isFull();
	{
		bool flag = false;
		if (counter == SIZE)
		{
			flag = true;

		}
		return flag;

		Item Queue::Enqueue(Item:item);
		Node *temp = new Node;
		if (counter < SIZE)
		{
			temp->item = item;
			temp->next = NULL;
			counter = counter + 1;
			if (front == NULL)
			{
			else
			{
				rear->next = Temp;
			}
			}
			rear = temp;
		}
		void Queue::Dequeue(Item);
		{
			item item;
			Node *temp = new Node;
			if (counter > 0)
			{
				item = front->item;
				temp = front;
				front = front->next;
				delete temp;
				counter = counter - 1;
			}
			return item;
		}
		int count();
		{
			return counter;
		}
		Queue();//constructor
		{
			front = NULL;
			rear = Null;
			counter = 0;
		}

		~Dequeue();//deconstructor
			Node temp = new Node;
		if (front != NULL)
		{
			temp = front;
			front = front->next;
			delete temp;
		}*/


	// Inputs bus routes and bus nos.
	int main(int argc, const char * argv[])
	{
		for (int row = 0; row < TRIP; row++)//input of Destination in string
		{
			std::cout << "Enter Bus Destination [" << row << "]: ";
			std::cin >> ROUTE[row];

			for (int col = 0; col < LIMIT; col++)//bus nos.
			{
				std::cout << "Enter Bus Nos:[" << row << "][" << col << "]: ";
				std::cin >> BUSNO[row][col];
			}

			std::cout << "\n";

		}

		//DISPLLAY ALL BUS ROUTES AND BUS NOS.
		std::cout << "\nDestination" << "  " << "Available Buses";
		for (int row = 0; row < LIMIT; row++)
		{
			std::cout << "\n" << ROUTE[row] << "\n\t";

			for (int col = 0; col < LIMIT; col++)
			{

				std::cout << BUSNO[row][col] << "\t";
			}
			std::cout << "\n";
		}

		std::cout << "\t";

		std::cout << "\nDone\n";
		system("pause");
	}
Last edited on
what, exactly, does a 2-d queue look like or do? The standard design is 1d. A 2-d might represent a priority queue, where each row is increasing priority and the columns are the items @ that priority. But this does not seems to be what you want either. Before we can help you, we really need to know what the structure represents.

please compile your code and address the trivial compile time errors first like access-specifiers beginning uppercase, dtor name different from class name etc and then repost
its line a A queueing of bus in each destination.

Route Bus Available
PASAY 009 008 006
Baclaran 007 006 005
Cavite 004 003 002

the output will be most likely like that the i wan to en queue and de queue on specific Route.
The above code structure was the code i got from my professor i just add it in my code to test it but its seems like it won't work.
> I'm trying to make a program that en queue and de queue data structure in multi-dimensional array?

Use a map of queues (or in a simpler scenario, a vector of queues), perhaps?
Something along these lines:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <algorithm>

struct task
{
    enum priority_t : unsigned int { IF_TIME_PERMITS = 0, LOW = 1, NORMAL = 2, HIGH = 3, URGENT = 4 };
    priority_t priority ; //
    std::string description ;
    friend std::ostream& operator<< ( std::ostream& stm, task t )
    {
        static const std::vector<std::string> descr { "IF_TIME_PERMITS", "LOW", "NORMAL", "HIGH", "URGENT" };
        if( t.priority < descr.size() ) stm << descr[t.priority] ;
        else stm << "*** " << t.priority << " *** (SOS)";
        return stm << ": " << t.description ;
    }
};

struct task_queue_2d // mult-idimensional queue
{
    bool empty() const
    { return std::all_of( pending.begin(), pending.end(), []( auto& q ) { return q.empty() ; } ) ; }

    void push( task t )
    {
        if( t.priority >= pending.size() ) pending.resize( t.priority+1 ) ;
        pending[t.priority].push( std::move(t) ) ;
    }

    task& front() // invariant: !empty()
    {
        for( auto iter = pending.rbegin() ; iter != pending.rend() ; ++iter )
            if( !iter->empty() ) return iter->front() ;

        throw std::runtime_error( "empty queue" ) ;
    }

    void pop() // invariant: !empty()
    {
        for( auto iter = pending.rbegin() ; iter != pending.rend() ; ++iter )
            if( !iter->empty() ) { iter->pop() ; return ; }
    }

    std::vector< std::queue<task> > pending ;
};

int main()
{
    task_queue_2d q2d ;

    q2d.push( { task::LOW, "say hello" } ) ;
    q2d.push( { task::IF_TIME_PERMITS, "see what is in the lounge" } ) ;
    q2d.push( { task::NORMAL, "comment code" } ) ;
    q2d.push( { task::HIGH, "peer review" } ) ;
    q2d.push( { task::priority_t(9), "make coffee" } ) ;
    q2d.push( { task::NORMAL, "improve performance" } ) ;
    q2d.push( { task::URGENT, "fix critical bug" } ) ;
    q2d.push( { task::LOW, "exploit C++17 features" } ) ;

    while( !q2d.empty() )
    {
        std::cout << q2d.front() << '\n' ;
        q2d.pop() ;
    }
}

http://coliru.stacked-crooked.com/a/c4881e8da22e6d26
JLBorges
what is that?
Topic archived. No new replies allowed.