Created
September 16, 2015 03:09
-
-
Save mchayapol/a11a5fcb2e7973249bd6 to your computer and use it in GitHub Desktop.
Linked List Skeleton 01
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 edu.au.scitech.sc2101; | |
| public class IntegerLinkedList { | |
| Node head; | |
| public class Node { | |
| int value; | |
| Node next; | |
| // Constructor for this node | |
| public Node(int value) { | |
| this.value = value; | |
| } | |
| } | |
| // Constructor for this list | |
| public IntegerLinkedList() { | |
| head = null; | |
| } | |
| public void add(int newValue) { | |
| // create a new node with newValue | |
| Node n = new Node(newValue); | |
| n.next = null; | |
| // Update the head | |
| head = n; | |
| } | |
| public String toString() { | |
| StringBuffer sb = new StringBuffer(); | |
| // return content of the list as string | |
| sb.append("["); | |
| for(Node currentNode = head; | |
| currentNode != null; | |
| currentNode = currentNode.next) | |
| { | |
| sb.append( currentNode.value ); | |
| sb.append(","); | |
| } | |
| sb.append("]"); | |
| return sb.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment