Created
August 11, 2021 17:16
-
-
Save afraenkel/0618ee238908eb30e9e4f8f9a655b57a to your computer and use it in GitHub Desktop.
This file contains 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
public class IntLinkedList { | |
public class IntNode { | |
public int item; | |
private IntNode next; | |
public IntNode(int i, IntNode n) { | |
item = i; | |
next = n; | |
} | |
} | |
private int size; | |
private IntNode sentinal = new IntNode(-518273, null); | |
public IntLinkedList() { | |
size = 0; | |
} | |
public IntLinkedList(int x) { | |
sentinal.next = new IntNode(x, null); | |
size = 1; | |
} | |
public void addFirst(int x) { | |
IntNode node = new IntNode(x, sentinal.next); | |
sentinal.next = node; | |
size += 1; | |
} | |
public int getFirst() { | |
return sentinal.next.item; | |
} | |
public void addLast(int x) { | |
IntNode p = sentinal; | |
size += 1; | |
// if not empty | |
while (p.next != null) { | |
p = p.next; | |
} | |
p.next = new IntNode(x, null); | |
} | |
public int size() { | |
return size; | |
} | |
public void clear() { | |
// code -- remove all elements from list | |
} | |
public void insert(int idx, int item) { | |
// code -- insert item at index idx | |
} | |
public static void main(String[] args) { | |
IntLinkedList L1 = new IntLinkedList(); | |
L1.addLast(3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment