• Source
    1. // A C++ program to demonstrate implementation of k stacks in a single
    2. // array in time and space efficient way
    3. #include<iostream>
    4. #include<climits>
    5. using namespace std;
    6.  
    7. // A C++ class to represent k stacks in a single array of size n
    8. class kStacks
    9. {
    10. int *arr; // Array of size n to store actual content to be stored in stacks
    11. int *top; // Array of size k to store indexes of top elements of stacks
    12. int *next; // Array of size n to store next entry in all stacks
    13. // and free list
    14. int n, k;
    15. int free; // To store beginning index of free list
    16. public:
    17. //constructor to create k stacks in an array of size n
    18. kStacks(int k, int n);
    19.  
    20. // A utility function to check if there is space available
    21. bool isFull() { return (free == -1); }
    22.  
    23. // To push an item in stack number 'sn' where sn is from 0 to k-1
    24. void push(int item, int sn);
    25.  
    26. // To pop an from stack number 'sn' where sn is from 0 to k-1
    27. int pop(int sn);
    28.  
    29. // To check whether stack number 'sn' is empty or not
    30. bool isEmpty(int sn) { return (top[sn] == -1); }
    31. };
    32.  
    33. //constructor to create k stacks in an array of size n
    34. kStacks::kStacks(int k1, int n1)
    35. {
    36. // Initialize n and k, and allocate memory for all arrays
    37. k = k1, n = n1;
    38. arr = new int[n];
    39. top = new int[k];
    40. next = new int[n];
    41.  
    42. // Initialize all stacks as empty
    43. for (int i = 0; i < k; i++)
    44. top[i] = -1;
    45.  
    46. // Initialize all spaces as free
    47. free = 0;
    48. for (int i=0; i<n-1; i++)
    49. next[i] = i+1;
    50. next[n-1] = -1; // -1 is used to indicate end of free list
    51. }
    52.  
    53. // To push an item in stack number 'sn' where sn is from 0 to k-1
    54. void kStacks::push(int item, int sn)
    55. {
    56. // Overflow check
    57. if (isFull())
    58. {
    59. cout << "\nStack Overflow\n";
    60. return;
    61. }
    62.  
    63. int i = free; // Store index of first free slot
    64.  
    65. // Update index of free slot to index of next slot in free list
    66. free = next[i];
    67.  
    68. // Update next of top and then top for stack number 'sn'
    69. next[i] = top[sn];
    70. top[sn] = i;
    71.  
    72. // Put the item in array
    73. arr[i] = item;
    74. }
    75.  
    76. // To pop an from stack number 'sn' where sn is from 0 to k-1
    77. int kStacks::pop(int sn)
    78. {
    79. // Underflow check
    80. if (isEmpty(sn))
    81. {
    82. cout << "\nStack Underflow\n";
    83. return INT_MAX;
    84. }
    85.  
    86.  
    87. // Find index of top item in stack number 'sn'
    88. int i = top[sn];
    89.  
    90. top[sn] = next[i]; // Change top to store next of previous top
    91.  
    92. // Attach the previous top to the beginning of free list
    93. next[i] = free;
    94. free = i;
    95.  
    96. // Return the previous top item
    97. return arr[i];
    98. }
    99.  
    100. /* Driver program to test twStacks class */
    101. int main()
    102. {
    103. // Let us create 3 stacks in an array of size 10
    104. int k = 3, n = 10;
    105. kStacks ks(k, n);
    106.  
    107. // Let us put some items in stack number 2
    108. ks.push(15, 2);
    109. ks.push(45, 2);
    110.  
    111. // Let us put some items in stack number 1
    112. ks.push(17, 1);
    113. ks.push(49, 1);
    114. ks.push(39, 1);
    115.  
    116. // Let us put some items in stack number 0
    117. ks.push(11, 0);
    118. ks.push(9, 0);
    119. ks.push(7, 0);
    120.  
    121. cout << "Popped element from stack 2 is " << ks.pop(2) << endl;
    122. cout << "Popped element from stack 1 is " << ks.pop(1) << endl;
    123. cout << "Popped element from stack 0 is " << ks.pop(0) << endl;
    124.  
    125. return 0;
    126. }