Advertisement
1cutcut1

vtora

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