SlavkovB

Палиндром

Nov 14th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. /** Дадена е двојно поврзана листа со N јазли каде секој јазел содржи по еден карактер (буква). Да се провери дали двојно поврзаната листа е палиндром: односно ако ја изминете од почеток до крај и од крај до почеток, дали ќе добиете ист збор. Во првиот ред од влезот даден е бројот на јазли во листата N, а во вториот ред се дадени броевите. На излез треба да се испечати 1 ако листата е палиндром, -1 ако не е.
  2.  
  3. Име на класата: PalindromeDLL
  4.  
  5. Sample input                    Sample output
  6. 5                               -1
  7. 1 2 3 1 2
  8. */
  9.  
  10. //  CODE
  11.  
  12.  
  13. import java.util.Scanner;
  14.  
  15. class DLLNode<E> {
  16.     protected E element;
  17.     protected DLLNode<E> pred, succ;
  18.  
  19.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  20.         this.element = elem;
  21.         this.pred = pred;
  22.         this.succ = succ;
  23.     }
  24.  
  25.     @Override
  26.     public String toString() {
  27.         return "<-" + element.toString() + "->";
  28.     }
  29. }
  30.  
  31. class DLL<E> {
  32.     private DLLNode<E> first, last;
  33.  
  34.     public DLL() {
  35.         // Construct an empty SLL
  36.         this.first = null;
  37.         this.last = null;
  38.     }
  39.  
  40.     public void deleteList() {
  41.         first = null;
  42.         last = null;
  43.     }
  44.  
  45.     public int length() {
  46.         int ret;
  47.         if (first != null) {
  48.             DLLNode<E> tmp = first;
  49.             ret = 1;
  50.             while (tmp.succ != null) {
  51.                 tmp = tmp.succ;
  52.                 ret++;
  53.             }
  54.             return ret;
  55.         } else
  56.             return 0;
  57.  
  58.     }
  59.  
  60.     public void insertFirst(E o) {
  61.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  62.         if (first == null)
  63.             last = ins;
  64.         else
  65.             first.pred = ins;
  66.         first = ins;
  67.     }
  68.  
  69.     public void insertLast(E o) {
  70.         if (first == null)
  71.             insertFirst(o);
  72.         else {
  73.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  74.             last.succ = ins;
  75.             last = ins;
  76.         }
  77.     }
  78.  
  79.     public void insertAfter(E o, DLLNode<E> after) {
  80.         if (after == last) {
  81.             insertLast(o);
  82.             return;
  83.         }
  84.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  85.         after.succ.pred = ins;
  86.         after.succ = ins;
  87.     }
  88.  
  89.     public void insertBefore(E o, DLLNode<E> before) {
  90.         if (before == first) {
  91.             insertFirst(o);
  92.             return;
  93.         }
  94.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  95.         before.pred.succ = ins;
  96.         before.pred = ins;
  97.     }
  98.  
  99.     public E deleteFirst() {
  100.         if (first != null) {
  101.             DLLNode<E> tmp = first;
  102.             first = first.succ;
  103.             if (first != null) first.pred = null;
  104.             if (first == null)
  105.                 last = null;
  106.             return tmp.element;
  107.         } else
  108.             return null;
  109.     }
  110.  
  111.     public E deleteLast() {
  112.         if (first != null) {
  113.             if (first.succ == null)
  114.                 return deleteFirst();
  115.             else {
  116.                 DLLNode<E> tmp = last;
  117.                 last = last.pred;
  118.                 last.succ = null;
  119.                 return tmp.element;
  120.             }
  121.         }
  122.         // else throw Exception
  123.         return null;
  124.     }
  125.  
  126.     @Override
  127.     public String toString() {
  128.         String ret = new String();
  129.         if (first != null) {
  130.             DLLNode<E> tmp = first;
  131.             ret += tmp + "<->";
  132.             while (tmp.succ != null) {
  133.                 tmp = tmp.succ;
  134.                 ret += tmp + "<->";
  135.             }
  136.         } else
  137.             ret = "Prazna lista!!!";
  138.         return ret;
  139.     }
  140.  
  141.     public DLLNode<E> getFirst() {
  142.         return first;
  143.     }
  144.  
  145.     public DLLNode<E> getLast() {
  146.  
  147.         return last;
  148.     }
  149.  
  150. }
  151.  
  152. public class PalindromeDLL {
  153.    
  154.     public static int isItPalindrome(DLL<Integer> list) {
  155.  
  156.         DLLNode<Integer> current = list.getFirst();
  157.         DLLNode<Integer> currentR = list.getLast();
  158.  
  159.         while (current != currentR) {
  160.             if (current.element != currentR.element)
  161.                 return -1;
  162.             current = current.succ;
  163.             currentR = currentR.pred;
  164.         }
  165.         return 1;
  166.     }
  167.    
  168.     public static void main(String[] args) {
  169.         Scanner in = new Scanner(System.in);
  170.         int n = in.nextInt();
  171.         DLL<Integer> list = new DLL<Integer>();
  172.         for (int i = 0; i < n; i++) {
  173.             list.insertLast(in.nextInt());
  174.         }
  175.         in.close();
  176.         System.out.println(isItPalindrome(list));
  177.     }
  178.  
  179. }
Add Comment
Please, Sign In to add comment