Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ds.cs61b;
- // a SLList is a list of intergers, which hides the terrible truth of the nakedness within
- public class SLList<LochNess> {
- public class StuffNode {
- public LochNess item;
- public StuffNode next;
- public StuffNode(LochNess i, StuffNode n) {
- item = i;
- next = n;
- }
- public void displayNode() {
- System.out.println("{ " + item + " } ");
- }
- }
- private StuffNode sentinel;
- private int size;
- public SLList(LochNess x) {
- sentinel = new StuffNode(x, null);
- sentinel.next = new StuffNode(x, null);
- size += 1;
- }
- /**
- * Adds an item to the front of the list.
- */
- public void addFirst(LochNess x) {
- sentinel.next = new StuffNode(x, sentinel.next);
- size += 1;
- }
- /**
- * Retrieves the front item from the list.
- */
- public LochNess getFirst() {
- return sentinel.next.item;
- }
- /**
- * Adds an item to the end of the list.
- */
- public void addLast(LochNess x) {
- size += 1;
- StuffNode p = sentinel;
- while (p.next != null) {
- p = p.next;
- }
- p.next = new StuffNode(x, null);
- }
- public int size3() {
- return size;
- }
- /**
- * Returns the number of items in the list using recursion.
- */
- public int size() {
- return size;
- }
- public void displayList() {
- System.out.println("List (first --> last) ");
- StuffNode current = sentinel.next;
- while (current != null) {
- current.displayNode();
- current = current.next;
- }
- System.out.println();
- }
- }
- // public int size() {
- // StuffNode p = sentinel;
- // int count = 1;
- //
- // while (p.next != null) {
- // p = p.next;
- // count++;
- // }
- //
- // return count;
- // }
- //
- // public static int size2(SLList.StuffNode p) {
- // if (p.next == null) {
- // return 1;
- // }
- // return 1 + size2(p.next);
- //
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement