Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Task2 {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. Random r = new Random();
  8. int n = in.nextInt();
  9. Elem head = null, p;
  10. for (int i = 0; i < n; i++) {
  11. p = new Elem();
  12. p.value = r.nextInt(100);
  13. p.next = head;
  14. head = p;
  15. }
  16. //sort
  17. boolean b = false;
  18. while (!b) {
  19. p = head;
  20. b = true;
  21. while (p != null && p.next != null && b) {
  22. if (p.value > p.next.value) {
  23. int temp = p.value;
  24. p.value = p.next.value;
  25. p.next.value = temp;
  26. b = false;
  27. }
  28. p = p.next;
  29. }
  30. }
  31. System.out.println(toString(head));
  32. //insert
  33. p = head;
  34. while (p != null) {
  35. if (p.value % 2 == 0) {
  36. insert(p);
  37. p = p.next;
  38. }
  39. p = p.next;
  40. }
  41. System.out.println(toString(head));
  42. //delete
  43. p = head;
  44. int k = in.nextInt();
  45. while (p != null) {
  46. if (p.value % k == 0) {
  47. if (p.next != null) {
  48. p.value = p.next.value;
  49. p.next = p.next.next;
  50. } else p = null;
  51. } else
  52. p = p.next;
  53. }
  54. if (head != null && head.value % k == 0)
  55. head = null;
  56.  
  57. System.out.println(toString(head));
  58. //sort
  59.  
  60. }
  61.  
  62. public static void insert(Elem p) {
  63. Elem q = new Elem();
  64. q.value = 1;
  65. q.next = p.next;
  66. p.next = q;
  67.  
  68. Elem c = new Elem();
  69. c.value = p.value;
  70. c.next = p.next;
  71. p.next = c;
  72. p.value = 0;
  73.  
  74. }
  75.  
  76. public static void delete(Elem p) {
  77.  
  78. }
  79.  
  80. public static void sort() {
  81.  
  82. }
  83.  
  84.  
  85. public static String toString(Elem head) {
  86. Elem p = head;
  87. String str = "";
  88. while (p != null) {
  89. str += p.value + " ";
  90. p = p.next;
  91. }
  92. return str;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement