Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class DLLNode<E> {
  4. protected E element;
  5. protected DLLNode<E> pred, succ;
  6. public DLLNode(E elem, DLLNode<E>
  7. pred, DLLNode<E> succ) {
  8. this.element = elem;
  9. this.pred = pred;
  10. this.succ = succ;
  11. }
  12. }
  13. class DLL<E> {
  14. private DLLNode<E> first, last;
  15. public DLL () {
  16. // kreiranje prazna lista
  17. this.first = null;
  18. this.last = null;
  19. }
  20. public DLLNode<E> getFirst() {
  21. return first;
  22. }
  23. public DLLNode<E> getLast() {
  24. return last;
  25. }
  26. public void insertFirst(E o){
  27. DLLNode<E> ins = new
  28. DLLNode<E>(o, null, first);
  29. if (first == null)
  30. last = ins;
  31. else
  32. first.pred = ins;
  33. first = ins;
  34. }
  35. public void insertLast(E o){
  36. if (first == null)
  37. insertFirst(o);
  38. else {
  39. DLLNode<E> ins = new
  40. DLLNode<E>(o, last, null);
  41. last.succ = ins;
  42. last = ins;
  43. }
  44. }
  45. public E deleteFirst(){
  46. if (first != null){
  47. DLLNode<E> tmp = first;
  48. first = first.succ;
  49. first.pred = null;
  50. if (first == null) last = null;
  51. return tmp.element;
  52. }
  53. else return null;
  54. }
  55. public E deleteLast(){
  56. if (first != null){
  57. if (first.succ == null)
  58. return deleteFirst();
  59. else {
  60. DLLNode<E> tmp = last;
  61. last = last.pred;
  62. last.succ = null;
  63. return tmp.element;
  64. }
  65. }
  66. //else throw Exception
  67. return null;
  68. }
  69. public void insertAfter(E o, DLLNode<E> after) {
  70. if(after==last){
  71. insertLast(o);
  72. return;
  73. }
  74. DLLNode<E> ins = new DLLNode<E>(o, after,
  75. after.succ);
  76. after.succ.pred = ins;
  77. after.succ = ins;
  78. }
  79. public void insertBefore(E o, DLLNode<E> before) {
  80. if(before == first){
  81. insertFirst(o);
  82. return;
  83. }
  84. DLLNode<E> ins = new DLLNode<E>(o, before.pred,
  85. before);
  86. before.pred.succ = ins;
  87. before.pred = ins;
  88. }
  89. public int length() {
  90. int ret;
  91. if (first != null) {
  92. DLLNode<E> tmp = first;
  93. ret = 1;
  94. while (tmp.succ != null) {
  95. tmp = tmp.succ;
  96. ret++;
  97. }
  98. return ret;
  99. } else
  100. return 0;
  101. }
  102.  
  103. }
  104.  
  105.  
  106. public class PalindromeDLL {
  107. public static int DLLPalindrom(DLL<Integer> palindrom) {
  108. DLLNode<Integer> tmp1 = palindrom.getFirst();
  109. DLLNode<Integer> tmp2 = palindrom.getLast();
  110. while(tmp1!=tmp2) {
  111. if(tmp1.element==tmp2.element) {
  112. tmp1=tmp1.succ;
  113. tmp2=tmp2.pred;
  114. }
  115. else {
  116. return -1;
  117. }
  118. }
  119. return 1;
  120. }
  121.  
  122. public static void main(String[] args) {
  123. Scanner in = new Scanner(System.in);
  124. DLL<Integer> palindrom = new DLL<Integer>();
  125. int n = in.nextInt();
  126. for(int i = 0;i<n;i++) {
  127. palindrom.insertLast(in.nextInt());
  128. }
  129. System.out.println(DLLPalindrom(palindrom));
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement