Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. package lab1;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5.  
  6. public class Languages
  7. {
  8.  
  9. ArrayList<String> items;
  10.  
  11. public void intializeValues(String fileName) throws IOException
  12. {
  13. items = TextReader.readFile(fileName);
  14. }
  15.  
  16. public boolean L5(String inputValue)
  17. {
  18. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  19. Stack_LinkedList<String> stackL5F = new Stack_LinkedList<String>();
  20. Stack_LinkedList<String> stackL5R = new Stack_LinkedList<String>();
  21. ArrayList<String> temp = new ArrayList<String>();
  22. boolean verdict = false;
  23.  
  24.  
  25. for (int i = 0; i < inputValue.length(); i++)
  26. {
  27. temp.add(inputValue.substring(i, i + 1));
  28. }
  29.  
  30. if ((temp.size() % 2) != 0)
  31. {
  32. for (int k = 0; k < (temp.size() / 2); k++)
  33. {
  34. stackL5F.push(temp.get(k));
  35. }
  36. for (int j = temp.size() - 1; j > (temp.size() / 2); j--)
  37. {
  38. stackL5R.push(temp.get(j));
  39. }
  40.  
  41. } else if (temp.size() % 2 == 0)
  42. {
  43. for (int k = 0; k < (temp.size() / 2); k++)
  44. {
  45. stackL5F.push(temp.get(k));
  46. }
  47. for (int j = temp.size() - 1; j > (temp.size() / 2 - 1); j--)
  48. {
  49. stackL5R.push(temp.get(j));
  50. }
  51. }
  52. if (stackL5F.isEmpty() == true && stackL5R.isEmpty() == true)
  53. {
  54. verdict = true;
  55. } else
  56. {
  57.  
  58. while (stackL5F.isEmpty() == false && stackL5R.isEmpty() == false)
  59. {
  60. if (stackL5F.peek().equals(stackL5R.peek()))
  61. {
  62. stackL5F.pop();
  63. stackL5R.pop();
  64. } else
  65. {
  66. verdict = false;
  67. break;
  68. }
  69. verdict = true;
  70. }
  71. }
  72.  
  73.  
  74. return verdict;
  75. }
  76.  
  77. ///////////////////////////////////////////////////////////////////
  78. public boolean L4(String inputValue)
  79. {
  80. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  81. Stack_LinkedList<String> stackL4 = new Stack_LinkedList<String>();
  82. Stack_LinkedList<String> stackTemp = new Stack_LinkedList<String>();
  83. boolean verdict = false;
  84. String inputString = "";
  85. boolean incorrectValue = false;
  86.  
  87. inputString = inputValue.substring(0) + inputValue.substring(0);
  88.  
  89. for (int j = 0; j < inputString.length(); j++)
  90. {
  91. String values = inputString.substring(j, j + 1);
  92. if (values.equals("A") == false && values.equals("B") == false)
  93. {
  94. verdict = false;
  95. incorrectValue = true;
  96. break;
  97. } else
  98. {
  99. stackLL.push(values);
  100. }
  101. }
  102.  
  103. while (stackLL.isEmpty() == false && incorrectValue == false)
  104. {
  105. stackL4.push(stackLL.pop());
  106. }
  107.  
  108. while (stackL4.isEmpty() == false && incorrectValue == false)
  109. {
  110.  
  111. String tempString = "";
  112.  
  113. while (stackL4.peek().equals("A"))
  114. {
  115. tempString = tempString + stackL4.pop();
  116. }
  117.  
  118. while (stackL4.peek().equals("B"))
  119. {
  120. tempString = tempString + stackL4.pop();
  121. if (stackL4.isEmpty())
  122. {
  123. break;
  124. }
  125. }
  126.  
  127. if (stackLL.isEmpty() == true)
  128. {
  129. stackLL.push(tempString);
  130. } else if (stackLL.isEmpty() == false
  131. && stackLL.peek().equals(tempString))
  132. {
  133. stackLL.pop();
  134. verdict = true;
  135. } else
  136. {
  137. verdict = false;
  138. break;
  139. }
  140.  
  141. }
  142. return verdict;
  143. }
  144.  
  145. ///////////////////////////////////////////////////////
  146. public boolean L1_RJ(String inputValue)
  147. {
  148. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  149. Stack_LinkedList<String> stackL1A = new Stack_LinkedList<String>();
  150. Stack_LinkedList<String> stackL1B = new Stack_LinkedList<String>();
  151. boolean verdict = false;
  152.  
  153. for (int j = 0; j < inputValue.length(); j++)
  154. {
  155. String values = inputValue.substring(j, j + 1);
  156. stackLL.push(values);
  157. }
  158.  
  159. while (stackLL.isEmpty() == false)
  160. {
  161. if (stackLL.peek().equals("A"))
  162. {
  163. stackL1A.push(stackLL.pop());
  164. } else if (stackLL.peek().equals("B"))
  165. {
  166. stackL1B.push(stackLL.pop());
  167. } else
  168. {
  169. stackLL.pop();
  170. }
  171. }
  172.  
  173. while (stackL1A.isEmpty() == false)
  174. {
  175. if (stackL1B.isEmpty() == true)
  176. {
  177. verdict = false;
  178. break;
  179. } else
  180. {
  181. stackL1A.pop();
  182. stackL1B.pop();
  183. }
  184.  
  185. }
  186.  
  187. if (stackL1A.isEmpty() == true && stackL1B.isEmpty() == true)
  188. {
  189. verdict = true;
  190.  
  191. } else
  192. {
  193. verdict = false;
  194. }
  195.  
  196. return verdict;
  197. }
  198.  
  199. ///////////////////////////////////////////////
  200. public boolean L3(String inputValue)
  201. {
  202.  
  203. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  204. Stack_LinkedList<String> stackL3 = new Stack_LinkedList<String>();
  205. boolean verdict = false;
  206.  
  207. for (int j = 0; j < inputValue.length(); j++)
  208. {
  209. String values = inputValue.substring(j, j + 1);
  210. stackLL.push(values);
  211. }
  212.  
  213. while (stackLL.isEmpty() == false)
  214. {
  215. stackL3.push(stackLL.pop());
  216. }
  217.  
  218. if (stackL3.isEmpty())
  219. {
  220. verdict = true;
  221. } else if (stackL3.peek().equals("A") == false)
  222. {
  223. verdict = false;
  224. } else
  225. {
  226. while (stackL3.peek().equals("A"))
  227. {
  228. stackLL.push(stackL3.pop());
  229. stackLL.push("A");
  230. }
  231.  
  232. while (stackL3.peek().equals("B"))
  233. {
  234.  
  235. stackLL.pop();
  236. stackL3.pop();
  237. if (stackL3.isEmpty() || stackLL.isEmpty())
  238. {
  239. break;
  240. }
  241. }
  242. }
  243.  
  244. if (stackL3.isEmpty() && stackLL.isEmpty())
  245. {
  246. verdict = true;
  247. } else
  248. {
  249. verdict = false;
  250. }
  251.  
  252. return verdict;
  253. }
  254.  
  255. /////////////////////////////////////////////////////////
  256. public boolean L2(String inputValue)
  257. {
  258. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  259. Stack_LinkedList<String> stackL2 = new Stack_LinkedList<String>();
  260. boolean verdict = false;
  261.  
  262. for (int j = 0; j < inputValue.length(); j++)
  263. {
  264. String values = inputValue.substring(j, j + 1);
  265. stackLL.push(values);
  266. }
  267.  
  268. while (stackLL.isEmpty() == false)
  269. {
  270. stackL2.push(stackLL.pop());
  271. }
  272.  
  273. if (stackL2.isEmpty())
  274. {
  275. verdict = true;
  276. } else if (stackL2.peek().equals("A") == false)
  277. {
  278. verdict = false;
  279. } else
  280. {
  281. while (stackL2.peek().equals("A"))
  282. {
  283. stackLL.push(stackL2.pop());
  284.  
  285. }
  286.  
  287. while (stackL2.peek().equals("B"))
  288. {
  289.  
  290. stackLL.pop();
  291. stackL2.pop();
  292. if (stackL2.isEmpty() || stackLL.isEmpty())
  293. {
  294. break;
  295. }
  296. }
  297. }
  298.  
  299. if (stackL2.isEmpty() && stackLL.isEmpty())
  300. {
  301. verdict = true;
  302. } else
  303. {
  304. verdict = false;
  305. }
  306.  
  307. return verdict;
  308. }
  309.  
  310. ///////////////////////////////////////////////////
  311. public boolean L1(String inputValue)
  312. {
  313. // Stack stack = new Stack(inputValue.length());
  314. Stack_LinkedList<String> stackLL = new Stack_LinkedList<String>();
  315. Stack_LinkedList<String> stackL1 = new Stack_LinkedList<String>();
  316. boolean verdict = false;
  317.  
  318. for (int j = 0; j < inputValue.length(); j++)
  319. {
  320. String values = inputValue.substring(j, j + 1);
  321. stackLL.push(values);
  322. }
  323.  
  324. // stackL1 has the reverse of the inputString
  325. while (stackLL.isEmpty() == false)
  326. {
  327. stackL1.push(stackLL.pop());
  328. }
  329.  
  330. if (stackL1.isEmpty() == true)
  331. {
  332. verdict = true;
  333. }
  334.  
  335. else
  336. {
  337. if (stackL1.peek().equals("A"))
  338. {
  339.  
  340. while (stackL1.isEmpty() == true)
  341. {
  342. if (stackL1.peek().equals("A"))
  343. {
  344. stackLL.push(stackL1.pop());
  345. } else if (stackL1.peek().equals("B"))
  346. {
  347. stackLL.pop();
  348. } else
  349. {
  350. verdict = false;
  351. break;
  352. }
  353. System.out.println(stackLL.peek());
  354. }
  355.  
  356. if (stackLL.isEmpty() == true)
  357. {
  358. verdict = true;
  359. } else
  360. {
  361. verdict = false;
  362. }
  363.  
  364. return verdict;
  365.  
  366. } else if (stackL1.peek().equals("B"))
  367. {
  368. while (stackL1.isEmpty() == false)
  369. {
  370. if (stackL1.peek().equals("B"))
  371. {
  372. stackLL.push(stackL1.pop());
  373. } else if (stackL1.peek().equals("A"))
  374. {
  375. stackLL.pop();
  376. } else
  377. {
  378. verdict = false;
  379. break;
  380. }
  381. }
  382.  
  383. if (stackLL.isEmpty() == true)
  384. {
  385. verdict = true;
  386. } else
  387. {
  388. verdict = false;
  389. }
  390.  
  391. }
  392. }
  393. return verdict;
  394. }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement