Advertisement
Guest User

Untitled

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