Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <iostream>
  4. #include <cassert>
  5. using namespace std;
  6.  
  7. struct NodeChunk{
  8. string * val;
  9. NodeChunk * next;
  10. };
  11.  
  12. struct Stack{
  13. int chunkSize;
  14. int topElt;
  15. NodeChunk * firstChunk;
  16. };
  17.  
  18.  
  19. NodeChunk* createNewNodeChunk (int N) {
  20. NodeChunk* New = new NodeChunk;
  21. New -> val = new string[N];
  22. New -> next = NULL;
  23.  
  24. return New;
  25. }
  26.  
  27. void initStack (int chunkSize, Stack &s) {
  28. s.chunkSize = chunkSize;
  29. s.topElt = 0;
  30. s.firstChunk = NULL;
  31. }
  32.  
  33. bool isEmpty (Stack s) {
  34. return s.firstChunk == NULL;
  35. }
  36.  
  37. void push (string val, Stack &s) {
  38. //if the firstChunk is full, create new Chunk and stick between stack and currrent firstChunk
  39. if (isEmpty(s)){
  40. s.firstChunk = createNewNodeChunk(s.chunkSize);
  41. s.firstChunk -> val[s.topElt] = val;
  42. }
  43. else{
  44. if (s.topElt == (s.chunkSize - 1)){
  45. s.topElt = 0;
  46. NodeChunk* temp = createNewNodeChunk(s.chunkSize);
  47. temp -> val[s.topElt] = val;
  48. temp -> next = s.firstChunk;
  49. s.firstChunk = temp;
  50.  
  51. }
  52. else{
  53. s.topElt++;
  54. s.firstChunk -> val[s.topElt] = val;
  55. }
  56. }
  57. }
  58.  
  59. void pop (Stack &s) {
  60. assert(!isEmpty(s));
  61.  
  62. if (s.topElt == 0){
  63. NodeChunk* temp = s.firstChunk;
  64. s.firstChunk = s.firstChunk -> next;
  65. delete[] temp -> val;
  66. delete temp;
  67. s.topElt = s.chunkSize - 1;
  68. }
  69. else{
  70. s.firstChunk -> val[s.topElt] = ""; //Maybe dont need, just ignore it?
  71. s.topElt--;
  72. }
  73. }
  74.  
  75. string top (const Stack& s) {
  76. assert(!isEmpty(s));
  77. return s.firstChunk -> val[s.topElt];
  78. }
  79.  
  80. int size (const Stack& s) {
  81. if (isEmpty(s))
  82. return 0;
  83. int counter = s.topElt + 1;
  84. if (s.firstChunk->next == NULL)
  85. return counter;
  86. NodeChunk *temp = s.firstChunk->next;
  87. while(true) {
  88. counter += s.chunkSize;
  89. if (temp->next == NULL)
  90. break;
  91. temp = temp->next;
  92. }
  93. return counter;
  94. }
  95.  
  96. void swap (Stack& s) {
  97. assert(size(s) >= 2);
  98.  
  99. //topElt is the first element in a new chunk
  100. if(s.topElt == 0 && s.firstChunk -> next != NULL){
  101. string temp2 = s.firstChunk -> val[s.topElt];
  102. s.firstChunk -> val[s.topElt] = s.firstChunk -> next -> val[s.chunkSize -1];
  103. s.firstChunk -> next -> val[s.chunkSize -1] = temp2;
  104. }
  105. else{
  106. //Normal Swap
  107. string temp2 = s.firstChunk -> val[s.topElt];
  108. s.firstChunk -> val[s.topElt] = s.firstChunk -> val[s.topElt - 1];
  109. s.firstChunk -> val[s.topElt - 1] = temp2;
  110. }
  111. }
  112.  
  113. void print(Stack s) {
  114. assert(!isEmpty(s));
  115. NodeChunk *temp = s.firstChunk;
  116. for(int i = 0; i <= s.topElt; i++)
  117. cout << temp->val[i] << endl;
  118. if (temp->next == NULL)
  119. return;
  120. temp = temp->next;
  121. while (true) {
  122. for(int i = 0; i < s.chunkSize; i++)
  123. cout << temp->val[i] << endl;
  124. if (temp->next == NULL)
  125. break;
  126. temp = temp->next;
  127. }
  128. return;
  129. }
  130.  
  131. int main (int argc, char* argv[]) {
  132.  
  133. // Stack s;
  134. // initStack (3, s);
  135. // push("first", s);
  136. // push("second", s);
  137. // print(s);
  138. // swap(s);
  139. // print(s);
  140.  
  141.  
  142.  
  143. Stack s1;
  144. initStack (1, s1);
  145. push ("a", s1);
  146. push ("b", s1);
  147. push ("c", s1);
  148. cout << size(s1) << endl;
  149. pop(s1);
  150. cout << top(s1) << endl;
  151. swap(s1);
  152. cout << top(s1) << endl;
  153. pop(s1);
  154. cout << top(s1) << endl;
  155.  
  156. cout << size(s1) << endl;
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement