Latkoski

Палиндром

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