### 2D array to simulate linked list

I'm trying to implement 2d array to simulate linked list for stack and I faced several difficulties doing so.

How do I implement from Top to Bottom ordering?
How do I check random index value for validity (I need to add a value at the top of the stack in a valid random space of the 2d array)

Source-code:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include using namespace std; int myTop, index, nex = -1; int twoDimentionalArray[25][3], L[25]; void construction(int twodimention[25][3], int list[25]) { myTop = -1; for(int p=0; p<25;p++) twoDimentionalArray[p][2] = -1; } void empty() { if (myTop == -1) cout << "The stack is empty."; else cout << "The stack is not empty."; } void push(int value) { if (myTop < 24) { myTop++; L[myTop] = value; index = rand()%25; twoDimentionalArray[index][0] = index; twoDimentionalArray[index][1] = value; twoDimentionalArray[index][2] = nex; nex = index; } else cout << "The stack is full."; } void top() { if (myTop != -1) cout << "The top of stack value is: " << L[myTop]; else cout << "The stack is empty."; cout << endl; } void pop() { if (myTop != -1) { myTop --; twoDimentionalArray[index][2] = -1; } } void display() { for (int row=0; row<25; row++) { for (int column=0; column<3; column++) { cout << twoDimentionalArray[row][column] << "\t"; if (column == 2) cout << endl; } } cout << endl; } int main() { construction(twoDimentionalArray, L); for (int i=0; i < 11; i++) push(i); display(); system ("pause"); }``````

Here's what I got from pushing 10 elements:

 ``123456789101112131415161718192021222324`` ``````0 3 9 0 0 -1 0 0 -1 3 6 24 0 0 -1 5 10 14 0 0 -1 0 0 -1 8 7 3 9 2 17 0 0 -1 0 0 -1 12 8 8 0 0 -1 14 9 12 0 0 -1 16 0 -1 17 1 16 0 0 -1 19 4 0 0 0 -1 0 0 -1 0 0 -1 0 0 -1``````

And one more thing: how do I get straight 0 from 25 index in the first coulumn? Thanks!
Last edited on
Topic archived. No new replies allowed.