SinglyLinkedList 구현 코드

한시삼십사분·2022년 1월 2일

자료구조 JAVA

목록 보기
7/16
post-thumbnail
  • List Interface 코드는 ArrayList 내용과 같다.

Node class

public class Node<E> {
	E data;
	Node<E> next;
	
	Node(E data){
		this.data = data;
		this.next = null;
	}
}

SLinkedList class

import java.util.NoSuchElementException;

public class SLinkedList<E> implements List<E> {
	
	private Node<E> head;
	private Node<E> tail;
	private int size;
	
	public SLinkedList() {
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	
	private Node<E> search(int index){
		if(index <0 || index>size) {
			throw new IndexOutOfBoundsException();
		}
		
		Node<E> n = head;
		
		for(int i=0; i<index; i++) {
			n = n.next;
		}
		
		return n;
	}
	
	public void addFirst(E data) {
		Node<E> newNode = new Node<E>(data);
		newNode.next = this.head;
		this.head = newNode;
		size++;
		
		//데이터가 newNode밖에 없을 경우 해당 노드를 tail이자 head로 설정
		if(head.next == null)
			tail = head;
	}
	
	public boolean add(E data) {
		addLast(data);
		return true;
	}
	
	public void addLast(E data) {
		Node<E> newNode = new Node<E>(data);
		
		if(size==0) {
			addFirst(data);
		}
		tail.next = newNode;
		this.tail = newNode;
		size++;
	}
	
	public void add(int index, E data) {
		if(index <0 || index>size) {
			throw new IndexOutOfBoundsException();
		}
		
		if(size==0) {
			addFirst(data);
			return;
		}
		
		if(size==index) {
			addLast(data);
			return;
		}
		
		Node<E> prevNode = search(index-1);
		Node<E> nextNode = prevNode.next;
		
		Node<E> newNode = new Node<E>(data);
		
		prevNode.next = null;
		prevNode.next = newNode;
		newNode.next = nextNode;
		size++;
	}
	
	
	@SuppressWarnings("unused")
	public E remove() {
		Node<E> headNode = head;
		Node<E> nextNode = headNode.next;
		
		if(headNode == null) {
			throw new NoSuchElementException();
		}
		
		E element = headNode.data;
		headNode.data = null;
		headNode.next = null;
		
		head = nextNode;
		size--;
		
		if(size == 0) {
			tail = null;
		}
		return element;
	}
	
	public E remove(int index) {
		if(index <0 || index>size) {
			throw new IndexOutOfBoundsException();
		}
		if(head == null) {
			throw new NoSuchElementException();
		}
		if(index == 0) {
			remove();
		}
		
		Node<E> prevNode = search(index-1);
		Node<E> removeNode = search(index);
		Node<E> nextNode = removeNode.next;
		E element = removeNode.data;
		
		removeNode.next = null;
		removeNode.data = null;
		prevNode.next = nextNode;
		
		size--;
		return element;
	}
	
	public boolean remove(Object data) {
		boolean hasValue = false;
		Node<E> prevNode = head;
		Node<E> n = head;
		
		for (; n != null; n = n.next) {
			if (data.equals(n.data)) {
				hasValue = true;
				break;
			}
			prevNode = n;
		}
		
		if(hasValue == false) {
			return false;
		}
		
		if(n.equals(head)) {
			remove();
			return true;
		}
		
		else {
			prevNode.next = n.next;
			
			n.data = null;
			n.next = null;
			size--;
			return true;
		}
	}
	
	
	public E get(int index) {
		Node<E> n = search(index);
		return n.data;
	}
	
	public void set(int index, E value) {
		Node<E> n = search(index);
		n.data = null;
		n.data = value;
	}
	
	public int indexOf(Object value) {
		int index = 0;
		for(Node<E> x = head; x != null; x = x.next) {
			if(x.equals(value)) {
				return index;
			}
			index++;
		}
		return -1;
	}
	
	
	public boolean contains(Object value) {
		if(indexOf(value) > -1) {
			return true;
		}
		else
			return false;
	}
	
	public int size() {
		return this.size;
	}
	
	public boolean isEmpty() {
		if(size == 0)
			return true;
		else
			return false;
	}
	
	public void clear() {
		head = null;
		tail = null;
		size = 0;
	}
	
	

}

https://st-lab.tistory.com/161?category=856997
내가 직접 코딩해보고 사이트를 확인하여 수정하는 방법으로 구현을 진행했음

profile
인간은 망각의 동물이라지만 이건 너무한 거 아니냐고

0개의 댓글