Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TP4;
- import java.util.Iterator;
- public class SimpleLinkedList<ELEMENT> implements ILinkedList<ELEMENT> {
- //region Node Class
- private class Node<ELEMENT> {
- public ELEMENT item;
- public Node<ELEMENT> next;
- public Node() {
- this(null, null);
- }
- public Node(ELEMENT item) {
- this(item, null);
- }
- public Node(ELEMENT item, Node<ELEMENT> next) {
- this.item = item;
- this.next = next;
- }
- @Override
- public String toString() {
- return this.item.toString();
- }
- }
- //endregion
- //region Attributes
- protected Node<ELEMENT> head;
- protected int count;
- protected Node<ELEMENT> tail;
- //endregion
- //region Constructors
- public SimpleLinkedList() {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- //endregion
- //region Linked List Methods
- // Returns the number of elements in this list.
- public int size() {
- return this.count;
- }
- // Appends the specified element to the end of this list.
- public void addLast(ELEMENT item) {
- Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
- if (this.count == 0) {
- this.head = temp;
- } else {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- //region Object Methods
- @Override
- public String toString() {
- if (this.size() <=0) {
- return "";
- }
- // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
- StringBuilder sb = new StringBuilder();
- sb.append("[" + this.head.item.toString());
- for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
- sb.append(" " + skip.item.toString());
- }
- sb.append("]");
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement