Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. class Node<T> {
  2. T cargo;
  3. Node<T> tail;
  4.  
  5. public Node(T nCargo) {
  6. cargo = nCargo;
  7. tail = null;
  8. }
  9.  
  10. public Node<T> getNext() {
  11. return tail;
  12. }
  13.  
  14. public T getCargo() {
  15. return cargo;
  16. }
  17.  
  18. public boolean isLast() {
  19. return tail == null;
  20. }
  21.  
  22. public Node<T> getLast() {
  23. Node<T> curNode = this;
  24. while (!curNode.isLast()) {
  25. curNode = curNode.getNext();
  26. }
  27. return curNode;
  28. }
  29.  
  30. public void add(T nCargo) {
  31. tail = new Node<T>(nCargo);
  32. }
  33.  
  34. public boolean canLink() {
  35. return this.getNext().getNext() != null;
  36. }
  37.  
  38. public void deleteNext() {
  39. if (this.canLink()) { tail = tail.getNext();
  40. } else { tail = null; }
  41. }
  42.  
  43. }
  44.  
  45. public class Vect<T> {
  46. Node<T> head;
  47. int vSize;
  48.  
  49. public Vect() {
  50. head = null;
  51. vSize = 0;
  52. }
  53. public Vect(T cargo) {
  54. head = new Node<T>(cargo);
  55. vSize = 1;
  56. }
  57.  
  58. public void pushBack(T cargo) {
  59. Node<T> last = head.getLast();
  60. vSize += 1;
  61. last.add(cargo);
  62. }
  63.  
  64. private Node<T> getNodeAt(int i) {
  65. Node<T> curNode = head;
  66. for (int cI = 0; i > cI; cI++) {
  67. curNode = curNode.getNext();
  68. }
  69. return curNode;
  70. }
  71.  
  72. public T getAt(int i) {
  73. return getNodeAt(i).getCargo();
  74. }
  75.  
  76. public int length() {
  77. return vSize;
  78. }
  79.  
  80. public void deleteHead() {
  81. if (head.getNext() != null) { head = head.getNext();
  82. } else { head = null; }
  83. vSize -= 1;
  84. }
  85.  
  86. public void deleteAt(int i) {
  87. if (i == 0) {
  88. deleteHead();
  89. } else {
  90. Node<T> curNode = this.getNodeAt(i - 1);
  91. curNode.deleteNext();
  92. vSize -= 1;
  93. }
  94. }
  95. }
  96.  
  97. public class Main {
  98.  
  99. public static void main(String[] args) {
  100. Vect <Integer> v = new Vect<Integer>(2);
  101. v.pushBack(3);
  102. v.pushBack(4);
  103. v.pushBack(5);
  104. v.pushBack(6);
  105.  
  106. for (int i = 0; i < v.length(); ++i) {
  107. System.out.println(v.getAt(i));
  108. }
  109.  
  110. v.deleteAt(2);
  111.  
  112. for(int i = 0; i < v.length(); ++i) {
  113. System.out.println(v.getAt(i));
  114. }
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement