데이터를 링크로 연결해서 관리하는 자료구조
자료의 순서는 정해져 있지만, 메모리 상에 연속성이 보장되지는 않음
데이터의 저장단위로 값과 포인터로 구성
메모리 안에서 노드의 물리적 순서가 리스트의 논리적 순서와 일치할 필요는 없다.
리스트의 원소, 데이터값 저장
다른 노드의 주소값 저장 (포인터)
class Node{
int data;
Node next;
}
LinkedList의 첫번째 노드를 가리키는 변수
class LinkedList{
Node head;
LinkedList(){}
LinkedList(Node node){
this.head = node;
}
}
데이터 추가 위치(head, 중간, tail)에 따른 연결작업 필요
public void addData(int data){
if(this.head == null){
this.head = new Node(data, null);
} else {
Node cur = this.head;
while(cur.next != null){
cur = cur.next;
}
cur.next = new Node(data, null);
}
}
public void addData(int data, Integer beforeData){
if(this.head == null){
this.head = new Node(data, null);
} else if(beforeData == null) {
Node cur = this.head;
while(cur.next != null){
cur = cur.next;
}
cur.next = new Node(data, null);
} else {
Node cur = this.head;
Node pre = cur;
while(cur != null){
if(cur.data == beforeData){
if(cur== this.head){
this.head = new Node(data, this.head);
} else {
pre.next = new Node(data,cur);
}
break;
}
pre = cur;
cur = cur.next;
}
}
}
public void removeData(){
if(this.isEmpty()){
System.out.println("List is Empty");
return;
}
Node cur = this.head;
Node prev = cur;
while(cur.next != null){
prev = cur;
cur = cur.next;
}
if (cur == this.head){
this.head = null;
} else {
prev.next = null;
}
}
public void removeData(int data){
if(this.isEmpty()){
System.out.println("List is Empty");
return;
}
Node cur = this.head;
Node prev = cur;
while(cur.next != null){
if(cur.data == data) {
if(cur == this.head){
this.head = cur.next;
} else {
pre.next = cur.next;
}
break;
}
pre = cur;
cur = cur.next;
}
}
public void findData(int data){
if(this.isEmpty()){
System.out.println("List is empty");
return;
}
Node cur = this.head;
while(cur != null){
if(cur.data == data){
System.out.println("Data exist!");
return;
}
cur = cur.next;
}
System.out.println("Data not found");
}
class NodeBi{
int data;
NodeBi next;
NodeBi prev;
NodeBi(int data, NodeBi next, NodeBi prev){
this.data = data;
this.next = next;
this.prev = prev;
}
}
class DoubleLinkedList extends LinkedList{
NodeBi head;
NodeBi tail;
DoubleLinkedList(NodeBi node){
this.head = node;
this.tail = node;
}
public boolean isEmpty(){
if(this.head == null){
return true;
}
return false;
}
public void addData(int data, Integer beforeData){
if(this.head == null){
this.head = new NodeBi(data, null, null);
this.tail = this.head;
} else if (beforeData == null){
this.tail.next= new NodeBi(data, null, this.tail);
this.tail = this.tail.next;
} else {
NodeBi cur = this.head;
NodeBi pre = cur;
while(cur!= null){
if(cur.data == beforeData){
if(cur == this.head){
this.head = new NodeBi(data, this.head, null);
this.head.next.prev= this.head;
} else {
pre.next = new NodeBi(data, cur, pre);
cur.prev = pre.next;
}
break;
}
pre = cur;
cur = cur.next;
}
}
}
public void removeData(int data){
if(this.isEmpty()){
System.out.println("List is Empty");
return;
}
NodeBi cur = this.head;
NodeBi pre = cur;
while(cur != null){
if(cur.data == data){
if(cur == this.head && cur == this.tail){
this.head = null;
this.tail = null;
} else if(cur ==this.head) [
this.head = cur.next;
this.head.prev = null;
} else if(cur == this.tail){
this.tail = this.tail.prev;
this.tail.next = null;
} else {
pre.next = cur.next;
cur.next.prev = pre;
}
break;
}
pre = cur;
cur = cur.next;
}
}
public void showData(){
if(this.isEmpty()){
System.out.println("List is empty");
return;
}
NodeBi cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public void showDataFromTail(){
if(this.isEmpty()){
System.out.println("List is empty");
return;
}
NodeBi cur = this.tail;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.prev;
}
System.out.println();
}
}