• Home
  • About
    • Yerim Oh photo

      Yerim Oh

      Happy and worthwhile day by day :)

    • Learn More
    • Email
    • LinkedIn
    • Instagram
    • Github
    • Youtube
  • Posts
    • All Posts
    • All Tags
  • Projects

[14] Java inked List를 main클래스에서 사용

04 Jul 2020

Reading time ~2 minutes

Table of Contents
  • 목차
    • 전체 코드
  • main 클래스
    • LinkedList인스턴스 생성
    • 문제
  • 이중 연결 리스트

목차

  • 전체 코드
  • main 클래스
    • LinkedList인스턴스 생성
    • 문제
  • 이중 연결 리스트

전체 코드

LinkedList

public class LinkedList {
	
	// 마직막 블록과 이르려면 마지막블록을 찾아야함
	// 그러기위해선 첫 번쨰 놈을 알아야함
	// 실제 데이터를 저장할 공간하고 그 데이터 공간을 서로 연결할 고리를 만들어보겠습니다.
	
	//  최초의 노드, 시작점 구현
	private Node head = null;
	
	public void insert(int data)
	{
		// 새로운 노드를 인스턴스로 만듦
		Node newBlock = new Node();
		// 인스턴트 안의 변수 선언(노드 값)
		newBlock.data = data;
		
	
	    //
		if(head == null)
		{
				head = newBlock;
				return;
		}
	
		//head 부터 시작
		Node current = head;
		// current가 곧 head
		// 노드의 next가 비어있지 않으면 계속 다음 블럭으로 가서 next를 확인한다
		while(current.next != null)
		{
			current = current.next;
		}
		
		// 마지막이면 while문 빠져나와 그 next에 주소를 붙인다.(연결한다)
		current.next = newBlock;
	}
	
	
	// 출력함수
	public void print()
	{
		
	
		Node current = head;
		while(current != null)
		{
			System.out.println(current.data);
			current = current.next;
		}
	}
	
	
	// 특정 값 출력 
	public int getData(int idx)
	{
		Node current = head;
		for(int i = 0;i<idx;i++)
			current = current.next;
			return current.data;
	}
	

	
	//head값 바꾸기
	public void insertHead(int data)
	{
	
		Node newBlock = new Node();
		newBlock.data = data;
		
		newBlock.next = head;
		head = newBlock;
	}
	public int getLastData()
	{
		Node current = head;
		while(current.next != null)
			current = current.next;
		
		return current.data;
	}
	

	// 맨 마지막 값 지우기
	public void deleteLast()
	{
		if(head == null)
			return;
		Node current = head;
		//if(head.next == null)
		// head 하나만 현재 LinkedList에 있을 경우도 특별한 처리가 필요합니다. 그런데 엄청 쉬워요
	
		while(current.next.next != null)
			current = current.next;
		
		current.next = null;
	}
	
	
	public void deleteAt(int idx)
	{
		// 헤드가 널이면 지우면 안돼
		if(head == null)
			return;
	
		// 헤드를 지워버리는 방법
		else if(idx == 0)
		{
			// head 를 지울꺼에요.
			// 두번째 블럭이 헤드가 되면 된다
			// 원래는 첫번 쨰 블럭이 헤드
			head = head.next;
		}
	
		else
		{
			Node current = head;
			for(int i = 0;i<idx-1;i++)
				current = current.next;
			current.next = current.next.next;
		}
	}
	
	//노드 사이에 넣기
	public void insertAt(int idx, int data)
	{
		Node newBlock = new Node();
		newBlock.data = data;
		
		// 추가할 위치까지 간다
		Node current = head;
		for(int i = 0;i<idx;i++)
			current = current.next;
	
		Node temp = current.next;
		current.next = newBlock;
		newBlock.next = temp;
	}

}


main 클래스

이를 만들어서 작동 확인
[file]-[new]-[class]
클래스 이름은 LinkedList
public static void를 체크
image

LinkedList인스턴스 생성

LinkedList list = new LinkedList();

[구현]

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	LinkedList list = new LinkedList();
	
	list.insert(2);
	list.insert(3);
	list.insert(6);
	}

}

문제

결과값을 예상해 보세요

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	LinkedList list = new LinkedList();
	
	list.insert(2);
	list.insert(3);
	list.insert(6);
	list.insert(8);
	
	list.insertAt(0, 7);
	list.print();
	}

}

[A]
2
7
3
6
8


이중 연결 리스트

노드와 노드가 서로 연결되어 있음 image image



BasicJava Share Tweet +1