cimona

Palindrom

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