Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package tse;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. class SLLNode<E> {
  7. public E element;
  8. public SLLNode<E> succ;
  9.  
  10. public SLLNode() {
  11. }
  12.  
  13. public SLLNode(E element, SLLNode<E> succ) {
  14. super();
  15. this.element = element;
  16. this.succ = succ;
  17. }
  18.  
  19. }
  20.  
  21. class SLL<E> {
  22. public SLLNode<E> first;
  23.  
  24. public SLL() {
  25. this.first = null;
  26. }
  27.  
  28. public void insertFirst(E o) {
  29. SLLNode<E> ins = new SLLNode<E>(o, first);
  30. first = ins;
  31. }
  32.  
  33. public void insertLast(E o) {
  34. if (first != null) {
  35. SLLNode<E> tmp = first;
  36. while (tmp.succ != null)
  37. tmp = tmp.succ;
  38. SLLNode<E> ins = new SLLNode<E>(o, null);
  39. tmp.succ = ins;
  40. } else {
  41. insertFirst(o);
  42. }
  43. }
  44.  
  45. public int length() {
  46. int ret;
  47. if (first != null) {
  48. SLLNode<E> tmp = first;
  49. ret = 1;
  50. while (tmp.succ != null) {
  51. tmp = tmp.succ;
  52. ret++;
  53. }
  54. return ret;
  55. } else
  56. return 0;
  57. }
  58.  
  59. public void show() {
  60. SLLNode<E> n = first;
  61. while (n != null) {
  62. System.out.print(n.element + " ");
  63. n = n.succ;
  64. }
  65. System.out.println();
  66. }
  67.  
  68. public E deleteFirst() {
  69. if (first != null) {
  70. SLLNode<E> tmp = first;
  71. first = first.succ;
  72. return tmp.element;
  73. } else {
  74. System.out.println("Listata e prazna");
  75. return null;
  76. }
  77. }
  78.  
  79. public E delete(SLLNode<E> node) {
  80. if (first != null) {
  81. SLLNode<E> tmp = first;
  82. if (first == node) {
  83. return this.deleteFirst();
  84. }
  85. while (tmp.succ != node && tmp.succ.succ != null)
  86. tmp = tmp.succ;
  87. if (tmp.succ == node) {
  88. tmp.succ = tmp.succ.succ;
  89. return node.element;
  90. } else {
  91. System.out.println("Elementot ne postoi vo listata");
  92. return null;
  93. }
  94. } else {
  95. System.out.println("Listata e prazna");
  96. return null;
  97. }
  98.  
  99. }
  100. public void insertBefore(E o, SLLNode<E> before) {
  101.  
  102. if (first != null) {
  103. SLLNode<E> tmp = first;
  104. if (first == before) {
  105. this.insertFirst(o);
  106. return;
  107. }
  108. //ako first!=before
  109. while (tmp.succ != before)
  110. tmp = tmp.succ;
  111. if (tmp.succ == before) {
  112. SLLNode<E> ins = new SLLNode<E>(o, before);
  113. tmp.succ = ins;
  114. } else {
  115. System.out.println("Elementot ne postoi vo listata");
  116. }
  117. } else {
  118. System.out.println("Listata e prazna");
  119. }
  120. }
  121. public void insertAfter(E o, SLLNode<E> node) {
  122. if (node != null) {
  123. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  124. node.succ = ins;
  125. } else {
  126. System.out.println("Dadenot jazol e null");
  127. }
  128. }
  129.  
  130.  
  131. }
  132.  
  133. public class sadasd {
  134.  
  135. public static void main(String[] args) {
  136. Scanner s=new Scanner (System.in);
  137. SLL<Vozac> sll= new SLL<>();
  138. int n=s.nextInt();
  139.  
  140. for(int i=0;i<n;i++)
  141. {
  142. Vozac b = new Vozac();
  143.  
  144.  
  145. b.ime=s.next();
  146. b.gorivo=s.nextInt();
  147.  
  148.  
  149. sll.insertLast(b);
  150.  
  151. }
  152.  
  153.  
  154. SLLNode<Vozac> dvizi=sll.first;
  155. int m=s.nextInt();
  156. for(int j=0;j<m;j++){
  157. Vozac d = new Vozac();
  158.  
  159. dvizi=sll.first;
  160. d.ime=s.next();
  161. d.gorivo=s.nextInt();
  162.  
  163.  
  164.  
  165. while(dvizi.succ!=null)
  166. {
  167. if(dvizi.element.ime.equals(d.ime))
  168. {
  169.  
  170. SLLNode<Vozac> tmp=dvizi;
  171. int gor=d.gorivo;
  172.  
  173. for(int i=0;i<gor;i++)
  174. {
  175.  
  176. if(tmp==null)
  177. {
  178. break;
  179. }
  180. tmp=tmp.succ;
  181.  
  182.  
  183. }
  184. d.gorivo=dvizi.element.gorivo-d.gorivo;
  185. sll.insertAfter(d,tmp);
  186. sll.delete(dvizi);
  187. dvizi=dvizi.succ;
  188.  
  189.  
  190.  
  191. break;
  192.  
  193.  
  194.  
  195. }else{
  196. dvizi=dvizi.succ;
  197.  
  198. }
  199.  
  200. }
  201. SLLNode<Vozac>pecati =sll.first;
  202.  
  203.  
  204. while(pecati!=null)
  205. {
  206. System.out.print(pecati.element.ime + "(" + pecati.element.gorivo + ")-> ");
  207. pecati=pecati.succ;
  208. }
  209. System.out.print("\n");
  210.  
  211.  
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. }
  220. }
  221.  
  222. class Vozac
  223. {
  224. public String ime;
  225. public int gorivo;
  226.  
  227.  
  228.  
  229.  
  230. public Vozac(String ime,int gorivo) {
  231. super();
  232. this.ime = ime;
  233. this.gorivo=gorivo;
  234.  
  235. }
  236.  
  237.  
  238.  
  239. public Vozac() {
  240.  
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement