Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.uniupo.disit.se.lab01;
- public class LinkedList {
- private NodeList head = null;
- public LinkedList() {
- }
- public LinkedList(String string) {
- if(!string.isEmpty()) {
- String[] array = string.split(" ");
- for(String element : array)
- addLast(Double.parseDouble(element));
- }
- }
- @Override
- public String toString() {
- StringBuilder result = new StringBuilder("[");
- NodeList current = head;
- while(current != null) {
- result.append(current.getValue()).append(" ");
- current = current.next();
- }
- return result.toString().trim() + "]";
- }
- public void addLast(double value) {
- if(head == null)
- head = new NodeList(value);
- else {
- NodeList current = head;
- while(current.next() != null)
- current = current.next();
- current.setNext(new NodeList(value));
- }
- }
- public void addFirst(double value) {
- if(head == null)
- head = new NodeList(value);
- else {
- NodeList current = new NodeList(value);
- current.setNext(head);
- head = current;
- }
- }
- public boolean removeFirst() {
- if(head == null)
- return false;
- head = head.next();
- return true;
- }
- public boolean removeLast() {
- if(head == null)
- return false;
- if(head.next() == null) {
- head = null;
- return true;
- }
- NodeList current = head;
- while(current.next().next() != null)
- current = current.next();
- current.setNext(null);
- return true;
- }
- public boolean remove(double value) {
- if(head != null) {
- if(head.getValue() == value) {
- head = head.next();
- return true;
- }
- NodeList current = head;
- while(current.next() != null) {
- if(current.next().getValue() == value) {
- current.setNext(current.next().next());
- return true;
- }
- current = current.next();
- }
- }
- return false;
- }
- public double mean() {
- if(head == null) return 0;
- double mean = 0, cnt = 0;
- NodeList current = head;
- while(current != null) {
- mean += current.getValue();
- cnt++;
- current = current.next();
- }
- return mean/cnt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement