Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct Element{
  9. int key;
  10. int value;
  11. };
  12.  
  13. struct ElementLL{
  14. Element elem;
  15. ElementLL *next, *prev;
  16. };
  17.  
  18. // Two-way ordered cycled list with sentinel
  19. struct List2W{
  20. ElementLL *sentinel;
  21. };
  22.  
  23. void init(List2W& l){
  24. l.sentinel = new ElementLL();
  25. l.sentinel->next = l.sentinel;
  26. l.sentinel->prev = l.sentinel;
  27. }
  28.  
  29. void insertElem(List2W & l, Element elem){
  30. ElementLL *element = l.sentinel;
  31. do {
  32. if (element->next == l.sentinel || elem.key < element->next->elem.key)
  33. {
  34. ElementLL *newelem = new ElementLL;
  35. newelem->elem = elem;
  36. newelem->prev = element->next->prev;
  37. newelem->next = element->next;
  38. element->next->prev = newelem;
  39. element->next = newelem;
  40. break;
  41. }
  42. element = element->next;
  43. } while (element != l.sentinel);
  44. }
  45.  
  46. bool findKey(List2W & l, int key, Element &elem){
  47. ElementLL *element = l.sentinel->next;
  48. while (element != l.sentinel)
  49. {
  50. if (element->elem.key == key)
  51. {
  52. elem = element->elem;
  53. return true;
  54. }
  55. element = element->next;
  56. }
  57. return false;
  58. }
  59.  
  60. void removeAllKeys(List2W& l, int key){
  61. if (l.sentinel != l.sentinel->next)
  62. {
  63. ElementLL *element = l.sentinel->next;
  64. while (element != l.sentinel)
  65. {
  66. if (element->elem.key == key)
  67. {
  68. element->prev->next = element->next;
  69. element->next->prev = element->prev;
  70. element = element->next;
  71. }
  72. else element = element->next;
  73. }
  74. }
  75. }
  76.  
  77. void showListFromHead(List2W& l){
  78. ElementLL *elem = l.sentinel->next;
  79. while (elem != l.sentinel)
  80. {
  81. cout << elem->elem.key << "(" << elem->elem.value << "),";
  82. elem = elem->next;
  83. }
  84. cout << endl;
  85. }
  86.  
  87. void showListFromTail(List2W& l){
  88. ElementLL *elem = l.sentinel->prev;
  89. while (elem != l.sentinel)
  90. {
  91. cout << elem->elem.key << "(" << elem->elem.value << "),";
  92. elem = elem->prev;
  93. }
  94. cout << endl;
  95. }
  96.  
  97. void clearList(List2W& l){
  98. ElementLL *elem = l.sentinel->next;
  99. while (elem != l.sentinel)
  100. {
  101. ElementLL *tmp = elem;
  102. elem = elem->next;
  103. delete tmp;
  104. }
  105. l.sentinel->next = l.sentinel;
  106. l.sentinel->prev = l.sentinel;
  107. }
  108.  
  109. void addList(List2W& l1, List2W& l2){
  110. if (&l1 != &l2)
  111. {
  112. ElementLL *element = l2.sentinel->next;
  113. while (element != l2.sentinel)
  114. {
  115. insertElem(l1, element->elem);
  116. element = element->next;
  117. }
  118. l2.sentinel->next = l2.sentinel;
  119. l2.sentinel->prev = l2.sentinel;
  120. }
  121. }
  122.  
  123. void showBool(bool val){
  124. if (val)
  125. cout << "true" << endl;
  126. else
  127. cout << "false" << endl;
  128. }
  129.  
  130. bool isCommand(const string command, const char *mnemonic){
  131. return command == mnemonic;
  132. }
  133.  
  134. int main(){
  135. string line;
  136. string command;
  137. List2W *list = NULL;
  138. int currentL = 0;
  139. int value;
  140. cout << "START" << endl;
  141. while (true){
  142. getline(cin, line);
  143. std::stringstream stream(line);
  144. stream >> command;
  145. if (line == "" || command[0] == '#')
  146. {
  147. // ignore empty line and comment
  148. continue;
  149. }
  150.  
  151. // copy line on output with exclamation mark
  152. cout << "!" << line << endl;;
  153.  
  154. // change to uppercase
  155. command[0] = toupper(command[0]);
  156. command[1] = toupper(command[1]);
  157.  
  158. // zero-argument command
  159. if (isCommand(command, "HA")){
  160. cout << "END OF EXECUTION" << endl;
  161. break;
  162. }
  163.  
  164. if (isCommand(command, "SH")) //*
  165. {
  166. showListFromHead(list[currentL]);
  167. continue;
  168. }
  169.  
  170. if (isCommand(command, "ST")) //*
  171. {
  172. showListFromTail(list[currentL]);
  173. continue;
  174. }
  175.  
  176. if (isCommand(command, "CL")) //*
  177. {
  178. clearList(list[currentL]);
  179. continue;
  180. }
  181.  
  182. if (isCommand(command, "IN")) //*
  183. {
  184. init(list[currentL]);
  185. continue;
  186. }
  187.  
  188. // read next argument, one int value
  189. stream >> value;
  190.  
  191. if (isCommand(command, "FK"))
  192. {
  193. Element elem;
  194. bool ret = findKey(list[currentL], value, elem);
  195. if (ret)
  196. cout << elem.key << '(' << elem.value << ')' << endl;
  197. else
  198. cout << "false" << endl;
  199. continue;
  200. }
  201.  
  202. if (isCommand(command, "RK"))
  203. {
  204. removeAllKeys(list[currentL], value);
  205. continue;
  206. }
  207.  
  208. if (isCommand(command, "CH"))
  209. {
  210. currentL = value;
  211. continue;
  212. }
  213.  
  214. if (isCommand(command, "IE"))
  215. {
  216. int variable2;
  217. stream >> variable2;
  218. Element elem;
  219. elem.key = value;
  220. elem.value = variable2;
  221. insertElem(list[currentL], elem);
  222. continue;
  223. }
  224.  
  225. if (isCommand(command, "GO"))
  226. {
  227. list = new List2W[value];
  228. continue;
  229. }
  230.  
  231. if (isCommand(command, "AD"))
  232. {
  233. addList(list[currentL], list[value]);
  234. continue;
  235. }
  236. cout << "wrong argument in test: " << command << endl;
  237. }
  238. return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement