Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Task2 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- Elem head = null, p;
- for (int i = 0; i < n; i++) {
- p = new Elem();
- p.value = in.nextInt();
- p.next = head;
- head = p;
- }
- p = head;
- while (p != null) {
- if (p.value % 2 == 0) {
- insert(p);
- p = p.next;
- }
- p = p.next;
- }
- System.out.println(toString(head));
- p = head;
- int k = in.nextInt();
- while (p != null) {
- if (p.value % k == 0)
- delete(p);
- p = p.next;
- }
- System.out.println(toString(head));
- }
- public static void insert(Elem p) {
- Elem q = new Elem();
- q.value = 1;
- q.next = p.next;
- p.next = q;
- Elem c = new Elem();
- c.value = p.value;
- c.next = p.next;
- p.next = c;
- p.value = 0;
- }
- public static void delete(Elem p) {
- if (p.next != null) {
- p.value = p.next.value;
- p.next = p.next.next;
- } else p = null;
- }
- public static void sort() {
- }
- public static String toString(Elem head) {
- Elem p = head;
- String str = "";
- while (p != null) {
- str += p.value + " ";
- p = p.next;
- }
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement