Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Task2 {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         int n = in.nextInt();
  7.         Elem head = null, p;
  8.         for (int i = 0; i < n; i++) {
  9.             p = new Elem();
  10.             p.value = in.nextInt();
  11.             p.next = head;
  12.             head = p;
  13.         }
  14.         p = head;
  15.         while (p != null) {
  16.             if (p.value % 2 == 0) {
  17.                 insert(p);
  18.                 p = p.next;
  19.             }
  20.             p = p.next;
  21.         }
  22.         System.out.println(toString(head));
  23.         p = head;
  24.         int k = in.nextInt();
  25.         while (p != null) {
  26.             if (p.value % k == 0)
  27.                 delete(p);
  28.             p = p.next;
  29.         }
  30.         System.out.println(toString(head));
  31.     }
  32.  
  33.     public static void insert(Elem p) {
  34.         Elem q = new Elem();
  35.         q.value = 1;
  36.         q.next = p.next;
  37.         p.next = q;
  38.  
  39.         Elem c = new Elem();
  40.         c.value = p.value;
  41.         c.next = p.next;
  42.         p.next = c;
  43.         p.value = 0;
  44.  
  45.     }
  46.  
  47.     public static void delete(Elem p) {
  48.         if (p.next != null) {
  49.             p.value = p.next.value;
  50.             p.next = p.next.next;
  51.         } else p = null;
  52.     }
  53.  
  54.     public static void sort() {
  55.  
  56.     }
  57.  
  58.  
  59.     public static String toString(Elem head) {
  60.         Elem p = head;
  61.         String str = "";
  62.         while (p != null) {
  63.             str += p.value + " ";
  64.             p = p.next;
  65.         }
  66.         return str;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement