Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. class SLLNode<E>
  2. {
  3. E element;
  4. SLLNode<E> succ;
  5.  
  6. public SLLNode(E element, SLLNode<E> succ) {
  7. this.element = element;
  8. this.succ = succ;
  9. }
  10. }
  11. class SLL<E>
  12. {
  13. SLLNode<E> first;
  14.  
  15. public SLL()
  16. {
  17. first = null;
  18. }
  19. public void insertFirst(E element)
  20. {
  21. SLLNode<E> nov = new SLLNode(element,first);
  22. first = nov;
  23. }
  24. public void insertLast(E element)
  25. {
  26. if(first==null)
  27. {
  28. insertFirst(element);
  29. }
  30. else
  31. {
  32. SLLNode<E> nov = new SLLNode(element,null);
  33. SLLNode<E> dvizi = first;
  34. while(dvizi.succ!=null)
  35. {
  36. dvizi = dvizi.succ;
  37. }
  38. dvizi.succ = nov;
  39. }
  40. }
  41. @Override
  42. public String toString()
  43. {
  44. String s = new String();
  45. SLLNode<E> dvizi = first;
  46. while(dvizi!=null)
  47. {
  48. s = s + dvizi.element + " ";
  49. dvizi = dvizi.succ;
  50. }
  51.  
  52.  
  53. return s;
  54. }
  55. }
  56. public class Test
  57. {
  58. public static void main(String[] args) {
  59. /*SLLNode<Integer> jazol1 = new SLLNode(5,null);
  60. SLLNode<String> jazol2 = new SLLNode("Dragan",null);
  61. SLLNode<Integer> jazol3 = new SLLNode(32,jazol1);*/
  62.  
  63. SLL<Integer> lista = new SLL();
  64. lista.insertFirst(21);
  65. lista.insertFirst(1);
  66. lista.insertLast(4);
  67. lista.insertLast(12);
  68.  
  69.  
  70. System.out.println(lista);
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement