Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class SLLC {
- NodeSLLC head, tail;
- int size;
- SLLC() {
- head = tail = null;
- size = 0;
- }
- boolean isEmpty() {
- return size == 0;
- }
- void addFirst(NodeSLLC input) {
- if (isEmpty()) {
- head = tail = input;
- tail.next = head;
- }
- else {
- input.next = head;
- tail.next = input;
- head = input;
- }
- size++;
- }
- void addLast(NodeSLLC input) {
- if (isEmpty()) {
- head = tail = input;
- tail.next = head;
- }
- else {
- tail.next = input;
- input.next = head;
- tail = input;
- }
- size++;
- }
- void removeFirst() {
- if (!isEmpty()) {
- if (size == 1) head = tail = null;
- else {
- NodeSLLC temp = head.next;
- tail.next = temp;
- head = temp;
- }
- size--;
- }
- else throw new NoSuchElementException();
- }
- void removeLast() {
- if (!isEmpty()) {
- if (size == 1) head = tail = null;
- else {
- NodeSLLC temp = head;
- while (temp.next != tail) {
- temp = temp.next;
- }
- temp.next = head;
- tail = temp;
- }
- size--;
- }
- else throw new NoSuchElementException();
- }
- private void checkIndex(int index) {
- if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
- }
- Object get(int index) {
- checkIndex(index);
- NodeSLLC temp = head;
- for (int i = 0; i < index; i++) temp = temp.next;
- return temp.data;
- }
- @Override
- public String toString() {
- NodeSLLC p = head;
- String s = "[";
- for (int i = 0; i < size; i++) {
- if (i != 0) s += ", ";
- s += p.data;
- p = p.next;
- }
- s += "]";
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement