Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node<T> {
- T cargo;
- Node<T> tail;
- public Node(T nCargo) {
- cargo = nCargo;
- tail = null;
- }
- public Node<T> getNext() {
- return tail;
- }
- public T getCargo() {
- return cargo;
- }
- public boolean isLast() {
- return tail == null;
- }
- public Node<T> getLast() {
- Node<T> curNode = this;
- while (!curNode.isLast()) {
- curNode = curNode.getNext();
- }
- return curNode;
- }
- public void add(T nCargo) {
- tail = new Node<T>(nCargo);
- }
- public boolean canLink() {
- return this.getNext().getNext() != null;
- }
- public void deleteNext() {
- if (this.canLink()) { tail = tail.getNext();
- } else { tail = null; }
- }
- }
- public class Vect<T> {
- Node<T> head;
- int vSize;
- public Vect() {
- head = null;
- vSize = 0;
- }
- public Vect(T cargo) {
- head = new Node<T>(cargo);
- vSize = 1;
- }
- public void pushBack(T cargo) {
- Node<T> last = head.getLast();
- vSize += 1;
- last.add(cargo);
- }
- private Node<T> getNodeAt(int i) {
- Node<T> curNode = head;
- for (int cI = 0; i > cI; cI++) {
- curNode = curNode.getNext();
- }
- return curNode;
- }
- public T getAt(int i) {
- return getNodeAt(i).getCargo();
- }
- public int length() {
- return vSize;
- }
- public void deleteHead() {
- if (head.getNext() != null) { head = head.getNext();
- } else { head = null; }
- vSize -= 1;
- }
- public void deleteAt(int i) {
- if (i == 0) {
- deleteHead();
- } else {
- Node<T> curNode = this.getNodeAt(i - 1);
- curNode.deleteNext();
- vSize -= 1;
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Vect <Integer> v = new Vect<Integer>(2);
- v.pushBack(3);
- v.pushBack(4);
- v.pushBack(5);
- v.pushBack(6);
- for (int i = 0; i < v.length(); ++i) {
- System.out.println(v.getAt(i));
- }
- v.deleteAt(2);
- for(int i = 0; i < v.length(); ++i) {
- System.out.println(v.getAt(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement