Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. class DLLNode<E> {
  4. protected E element;
  5. protected DLLNode<E> pred, succ;
  6.  
  7. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  8. this.element = elem;
  9. this.pred = pred;
  10. this.succ = succ;
  11. }
  12.  
  13. @Override
  14. public String toString() {
  15. return element.toString();
  16. }
  17. }
  18. class DLL<E> {
  19. private DLLNode<E> first, last;
  20.  
  21. public DLL() {
  22. // Construct an empty SLL
  23. this.first = null;
  24. this.last = null;
  25. }
  26.  
  27. public void deleteList() {
  28. first = null;
  29. last = null;
  30. }
  31.  
  32. public int length() {
  33. int ret;
  34. if (first != null) {
  35. DLLNode<E> tmp = first;
  36. ret = 1;
  37. while (tmp.succ != null) {
  38. tmp = tmp.succ;
  39. ret++;
  40. }
  41. return ret;
  42. } else
  43. return 0;
  44.  
  45. }
  46.  
  47. public DLLNode<E> find(E o) {
  48. if (first != null) {
  49. DLLNode<E> tmp = first;
  50. while (tmp.element != o && tmp.succ != null)
  51. tmp = tmp.succ;
  52. if (tmp.element == o) {
  53. return tmp;
  54. } else {
  55. System.out.println("Elementot ne postoi vo listata");
  56. }
  57. } else {
  58. System.out.println("Listata e prazna");
  59. }
  60. return first;
  61. }
  62.  
  63. public void insertFirst(E o) {
  64. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  65. if (first == null)
  66. last = ins;
  67. else
  68. first.pred = ins;
  69. first = ins;
  70. }
  71.  
  72. public void insertLast(E o) {
  73. if (first == null)
  74. insertFirst(o);
  75. else {
  76. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  77. last.succ = ins;
  78. last = ins;
  79. }
  80. }
  81.  
  82. public void insertAfter(E o, DLLNode<E> after) {
  83. if(after==last){
  84. insertLast(o);
  85. return;
  86. }
  87. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  88. after.succ.pred = ins;
  89. after.succ = ins;
  90. }
  91.  
  92. public void insertBefore(E o, DLLNode<E> before) {
  93. if(before == first){
  94. insertFirst(o);
  95. return;
  96. }
  97. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  98. before.pred.succ = ins;
  99. before.pred = ins;
  100. }
  101.  
  102. public E deleteFirst() {
  103. if (first != null) {
  104. DLLNode<E> tmp = first;
  105. first = first.succ;
  106. if (first != null) first.pred = null;
  107. if (first == null)
  108. last = null;
  109. return tmp.element;
  110. } else
  111. return null;
  112. }
  113.  
  114. public E deleteLast() {
  115. if (first != null) {
  116. if (first.succ == null)
  117. return deleteFirst();
  118. else {
  119. DLLNode<E> tmp = last;
  120. last = last.pred;
  121. last.succ = null;
  122. return tmp.element;
  123. }
  124. }
  125. // else throw Exception
  126. return null;
  127. }
  128.  
  129. public E delete(DLLNode<E> node) {
  130. if(node==first){
  131. deleteFirst();
  132. return node.element;
  133. }
  134. if(node==last){
  135. deleteLast();
  136. return node.element;
  137. }
  138. node.pred.succ = node.succ;
  139. node.succ.pred = node.pred;
  140. return node.element;
  141.  
  142. }
  143.  
  144. @Override
  145. public String toString() {
  146. String ret = new String();
  147. if (first != null) {
  148. DLLNode<E> tmp = first;
  149. ret += tmp.element + "<->";
  150. while (tmp.succ != null) {
  151. tmp = tmp.succ;
  152. if (tmp.succ==null){
  153. ret+=tmp.element;
  154. }else
  155. ret += tmp.element + "<->";
  156. }
  157. } else
  158. ret = "Prazna lista!!!";
  159. return ret;
  160. }
  161.  
  162. public String toStringR() {
  163. String ret = new String();
  164. if (last != null) {
  165. DLLNode<E> tmp = last;
  166. ret += tmp + "<->";
  167. while (tmp.pred != null) {
  168. tmp = tmp.pred;
  169. ret += tmp + "<->";
  170. }
  171. } else
  172. ret = "Prazna lista!!!";
  173. return ret;
  174. }
  175.  
  176. public DLLNode<E> getFirst() {
  177. return first;
  178. }
  179.  
  180. public DLLNode<E> getLast() {
  181.  
  182. return last;
  183. }
  184.  
  185. public void izvadiDupliIPrebroj(){
  186.  
  187. }
  188. public boolean isVowel(E ch){
  189. if(ch.equals("a") || ch.equals("e") || ch.equals("i") || ch.equals("o") || ch.equals("u") )
  190. return true;
  191. else
  192. return false;
  193. }
  194. }
  195.  
  196. public class SamISamogl {
  197. public static void izbrisi(DLL<String> lista){
  198. DLLNode<String> pok1=lista.getFirst();
  199. DLLNode<String> pok2=lista.getFirst().succ;
  200. while (pok1.succ!=null) {
  201. while (pok2 != null) {
  202. if (pok2.element.equals(pok1.element)) {
  203. if (pok2.succ == null) {
  204. lista.deleteLast();
  205. break;
  206. } else {
  207. pok2 = pok2.succ;
  208. lista.delete(pok2.pred);
  209. }
  210. } else {
  211. if (pok2.succ != null)
  212. pok2 = pok2.succ;
  213. else
  214. break;
  215. }
  216. }
  217. if (pok1.succ!= null) {
  218. pok1 = pok1.succ;
  219. if (pok1.succ != null && pok1 != null)
  220. pok2 = pok1.succ;
  221. }
  222. }
  223. izmeni(lista);
  224. }
  225. public static void izmeni(DLL<String> lista){
  226. DLLNode<String> pok1=lista.getFirst();
  227. while (pok1.succ!=null) {
  228. if (lista.isVowel(pok1.element) && lista.isVowel(pok1.succ.element)) {
  229. lista.delete(pok1.succ);
  230. }
  231. else if (!lista.isVowel(pok1.element) && !lista.isVowel(pok1.succ.element)){
  232. lista.delete(pok1.succ);
  233.  
  234. }else
  235. pok1=pok1.succ;
  236. }
  237. }
  238. public static void main(String[] args)throws IOException{
  239. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  240. String[] s=stdin.readLine().split("<->");
  241. DLL<String> lista=new DLL<>();
  242. for (int i=0;i<s.length;i++){
  243. lista.insertLast(s[i]);
  244. }
  245. izbrisi(lista);
  246. System.out.println(lista.toString());
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement