ivanakarpuzova

Palindrom

Aug 20th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. //package palindromedll;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10.  
  11. import java.util.Scanner;
  12.  
  13. class DLLNode<E> {
  14.     protected E element;
  15.     protected DLLNode<E> pred, succ;
  16.  
  17.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  18.         this.element = elem;
  19.         this.pred = pred;
  20.         this.succ = succ;
  21.     }
  22.  
  23.     @Override
  24.     public String toString() {
  25.         return "<-" + element.toString() + "->";
  26.     }
  27. }
  28.  
  29. class DLL<E> {
  30.     private DLLNode<E> first, last;
  31.  
  32.     public DLL() {
  33.         // Construct an empty SLL
  34.         this.first = null;
  35.         this.last = null;
  36.     }
  37.  
  38.     public void deleteList() {
  39.         first = null;
  40.         last = null;
  41.     }
  42.  
  43.     public int length() {
  44.         int ret;
  45.         if (first != null) {
  46.             DLLNode<E> tmp = first;
  47.             ret = 1;
  48.             while (tmp.succ != null) {
  49.                 tmp = tmp.succ;
  50.                 ret++;
  51.             }
  52.             return ret;
  53.         } else
  54.             return 0;
  55.  
  56.     }
  57.  
  58.     public void insertFirst(E o) {
  59.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  60.         if (first == null)
  61.             last = ins;
  62.         else
  63.             first.pred = ins;
  64.         first = ins;
  65.     }
  66.  
  67.     public void insertLast(E o) {
  68.         if (first == null)
  69.             insertFirst(o);
  70.         else {
  71.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  72.             last.succ = ins;
  73.             last = ins;
  74.         }
  75.     }
  76.  
  77.     public void insertAfter(E o, DLLNode<E> after) {
  78.         if (after == last) {
  79.             insertLast(o);
  80.             return;
  81.         }
  82.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  83.         after.succ.pred = ins;
  84.         after.succ = ins;
  85.     }
  86.  
  87.     public void insertBefore(E o, DLLNode<E> before) {
  88.         if (before == first) {
  89.             insertFirst(o);
  90.             return;
  91.         }
  92.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  93.         before.pred.succ = ins;
  94.         before.pred = ins;
  95.     }
  96.  
  97.     public E deleteFirst() {
  98.         if (first != null) {
  99.             DLLNode<E> tmp = first;
  100.             first = first.succ;
  101.             if (first != null) first.pred = null;
  102.             if (first == null)
  103.                 last = null;
  104.             return tmp.element;
  105.         } else
  106.             return null;
  107.     }
  108.  
  109.     public E deleteLast() {
  110.         if (first != null) {
  111.             if (first.succ == null)
  112.                 return deleteFirst();
  113.             else {
  114.                 DLLNode<E> tmp = last;
  115.                 last = last.pred;
  116.                 last.succ = null;
  117.                 return tmp.element;
  118.             }
  119.         }
  120.         // else throw Exception
  121.         return null;
  122.     }
  123.  
  124.     @Override
  125.     public String toString() {
  126.         String ret = new String();
  127.         if (first != null) {
  128.             DLLNode<E> tmp = first;
  129.             ret += tmp + "<->";
  130.             while (tmp.succ != null) {
  131.                 tmp = tmp.succ;
  132.                 ret += tmp + "<->";
  133.             }
  134.         } else
  135.             ret = "Prazna lista!!!";
  136.         return ret;
  137.     }
  138.  
  139.     public DLLNode<E> getFirst() {
  140.         return first;
  141.     }
  142.  
  143.     public DLLNode<E> getLast() {
  144.  
  145.         return last;
  146.     }
  147.  
  148. }
  149.  
  150. public class PalindromeDLL {
  151.    
  152.     public static int isItPalindrome(DLL<Integer> list){
  153.         //Vashiot kod tuka...
  154.                DLLNode<Integer> first = list.getFirst();
  155.             DLLNode<Integer> last = list.getLast();
  156.         while(first != last)
  157.             {
  158.                 if(first.element != last.element)
  159.                 {
  160.                     return -1;
  161.                 }
  162.            
  163.             first = first.succ;
  164.             last = last.pred;
  165.             }
  166.         return 1;
  167.      
  168.     }
  169.    
  170.     public static void main(String[] args) throws IOException{
  171.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  172.         DLL<Integer> lista = new DLL<>();
  173.        
  174.         String s = br.readLine();
  175.         int N = Integer.parseInt(s);
  176.        
  177.         s=br.readLine();
  178.         String [] pomniza = s.split(" ");
  179.         for(int i=0;i<N;i++)
  180.         {
  181.             lista.insertLast(Integer.parseInt(pomniza[i]));
  182.         }
  183.         System.out.print(isItPalindrome(lista));
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment