Last active
December 12, 2015 19:18
-
-
Save AnnaBoro/f0ea32675ec63a3b0979 to your computer and use it in GitHub Desktop.
MyList4 + addLast()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lesson7.mylist; | |
| public class SimpleLinkedList { | |
| private Node root; | |
| private int size; | |
| public SimpleLinkedList() { | |
| size = 0; | |
| } | |
| public void addFirst(Object o) { | |
| Node node = new Node(); | |
| node.object = o; | |
| if(root != null) { | |
| node.node = root; | |
| } | |
| root = node; | |
| size++; | |
| } | |
| public void addLast(Object o) { | |
| Node nodeNew = new Node(); | |
| nodeNew.object = o; | |
| if (root != null) { | |
| Node gapNode = root.node; | |
| Node gap = root.node; | |
| while (gapNode != null) { | |
| gap = gapNode; | |
| gapNode = gapNode.node; | |
| } | |
| gap.node = nodeNew; | |
| size++; | |
| } | |
| else addFirst(o); | |
| } | |
| public void addAfter(Object o, Object n) { | |
| } | |
| public int getSize() { | |
| return size; | |
| } | |
| private class Node { | |
| private Object object; | |
| private Node node; | |
| public Node() { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment