C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 13th, 2013, 09:11 PM
vhlamburger vhlamburger is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 vhlamburger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 34 sec
Reputation Power: 0
Memory & arrays - 2d array to simulate linked list for stack construction

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?
Source-code:
c++ Code:
Original - c++ Code
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int myTop, index, nex = -1;
  5. int twoDimentionalArray[25][3], L[25];
  6.  
  7. void construction(int twodimention[25][3], int list[25])
  8. {
  9.     myTop = -1;
  10.     for(int p=0; p<25;p++)
  11.         twoDimentionalArray[p][2] = -1;
  12. }
  13.  
  14. void empty()
  15. {
  16.     if (myTop == -1)
  17.         cout << "The stack is empty.";
  18.     else
  19.         cout << "The stack is not empty.";
  20. }
  21.  
  22. void push(int value)
  23. {
  24.     if (myTop < 24)
  25.     {   
  26.         myTop++;
  27.         L[myTop] = value;
  28.         index = rand()%25;
  29.         twoDimentionalArray[index][0] = index;
  30.         twoDimentionalArray[index][1] = value;
  31.         twoDimentionalArray[index][2] = nex;
  32.         nex = index;
  33.     }
  34.     else
  35.         cout << "The stack is full.";       
  36. }
  37.  
  38. void top()
  39. {
  40.     if (myTop != -1)
  41.         cout << "The top of stack value is: " << L[myTop];
  42.     else
  43.         cout << "The stack is empty.";
  44.     cout << endl;
  45. }
  46.  
  47. void pop()
  48. {
  49.     if (myTop != -1)
  50.     {
  51.         myTop --;
  52.         twoDimentionalArray[index][2] = -1;
  53.     }
  54.  
  55. }
  56.  
  57. void display()
  58. {
  59.     for (int row=0; row<25; row++)
  60.     {
  61.         for (int column=0; column<3; column++)
  62.         {
  63.             cout << twoDimentionalArray[row][column] << "\t";
  64.             if (column == 2)
  65.                 cout << endl;
  66.         }
  67.     }
  68.     cout << endl;
  69. }
  70.  
  71. int main()
  72. {
  73.     construction(twoDimentionalArray, L);
  74.     for (int i=0; i < 11; i++)
  75.         push(i);
  76.     display();
  77.  
  78.     system ("pause");
  79. }


Here's what I got from pushing 10 elements:

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
24 5 19


And one more thing: how do I get straight 0 from 25 index in the first coulumn? Thanks!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - 2d array to simulate linked list for stack construction


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap