Advertisement
Guest User

Untitled

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