Using Multi-dimensional Array to Simulate Linked List for STACK Construction

This function is supposed to add a random value at the top of the stack in a valid random space of the 2D array. I can't understand what is wrong. Sometimes, when I use Push(), I do not have -1 in the first node which is supposed to be [rand][0] = random value, [rand][1] = -1,

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
 void Push(int list[capacity][column_number])
{

	int random_index = rand() % capacity;
	int random_value = rand();

 	for (int i = 0; i < capacity; i++)
	{
		if (/*global variable*/occupied[i] == random_index)
		{
			random_index = rand() % capacity;
			i = 0;
		}
	}

	if (Empty(list) == true)
	{
		list[random_index][0] = random_value;
		list[random_index][1] = -1;
		/*global variable*/my_top = random_index;
		/*global variable*/occupied[occupied_index] = my_top;
		/*global variable*/occupied_index++; 
		cout << "Index: " << occupied_index << " : " << list[random_index][0] << " | " << list[random_index][1] << endl;
	}
	else
	{
		list[random_index][0] = random_value;
		list[random_index][1] = my_top;
		/*global variable*/my_top = random_index;
		/*global variable*/occupied[occupied_index] = my_top;
		/*global variable*/occupied_index++;
		cout << "Index: " << occupied_index << " : " << list[random_index][0] << " | " << list[random_index][1] << endl;
	}
}



Just in case here is the full code
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
136
137
138
139
140
141
142
143
144
145
146
 
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//Global Variables
const int capacity = 25;
const int column_number = 2;
int my_top = 0;
int occupied[capacity];
int occupied_index = 0;
//Prototypes
void Clear(int list[capacity][column_number]);
bool Empty(int list[capacity][column_number]);
void Push (int list[capacity][column_number]);

//------------MAIN-FUNCTION------------MAIN-FUNCTION---------MAIN-FUNCTION----------------
void main()
{
	//Seed for the random number generator
	srand(time(0));

	int list[capacity][column_number];
	Clear(list);

	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list); 
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list); 
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list);
	Push(list); 
	Push(list);

	for (int i = 0; i < capacity; i++)
	{
		cout << "Index: " << i << " : " << list[i][0] << " | " << list[i][1] << endl;
	}



	
	/*for (int i2 = 0; i2 < capacity; i2++)
	{
		cout << "Index: " << i2 << " | " << list[i2][0] << " | " << list[i2][1] << endl;
	}*/
}
//------------MAIN-FUNCTION------------MAIN-FUNCTION---------MAIN-FUNCTION----------------

//Function assignes value of -1 to the every single elemnt of the 2D array
//If the value of an element is -1, it is EMPTY
//(This function is not required)
void Clear(int list[capacity][column_number])
{
	for (int i = 0; i < column_number; i++)
	{
		for (int i2 = 0; i2 < capacity; i2++)
		{
			list[i2][i] = -1;
		}
	}

	for (int i = 0; i < capacity; i++)
	{
		occupied[i] = -1;
	}
}

//Function checks whether the list (stack) is empty or not
//If returns TRUE, it's empty. FALSE, if it is not
bool Empty(int list[capacity][column_number])
{
	int available_space = 0;

	for (int i = 0; i < column_number; i++)
	{
		for (int i2 = 0; i2 < capacity; i2++)
		{
			if (list[i2][i] == -1)
			{
				available_space++;
			}
		}
	}

	if (available_space == capacity*column_number/*Every single element*/)
		return true;
	else
		return false;
}

//Function adds a random value at the top of the stack
//in a valid random space of the 2D array
void Push(int list[capacity][column_number])
{

	int random_index = rand() % capacity;
	int random_value = rand();

 	for (int i = 0; i < capacity; i++)
	{
		if (occupied[i] == random_index)
		{
			random_index = rand() % capacity;
			i = 0;
		}
	}

	if (Empty(list) == true)
	{
		list[random_index][0] = random_value;
		list[random_index][1] = -1;
		my_top = random_index;
		occupied[occupied_index] = my_top;
		occupied_index++; 
		cout << "Index: " << occupied_index << " : " << list[random_index][0] << " | " << list[random_index][1] << endl;
	}
	else
	{
		list[random_index][0] = random_value;
		list[random_index][1] = my_top;
		my_top = random_index;
		occupied[occupied_index] = my_top;
		occupied_index++;
		cout << "Index: " << occupied_index << " : " << list[random_index][0] << " | " << list[random_index][1] << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.