๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+27] ์ฝ”๋”ฉ ๋ฐ์ด

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 10. 30.

 

 

๊ตญ๋น„์ง€์› D+27

-  ์ฝ”๋”ฉ๋ฐ์ด -

 


 

์ฝ”๋”ฉ๋ฐ์ด

Q) ๊ตฌ์ฒดํ™”๋œ Linked List๋ฅผ ์™„์„ฑํ•ด๋ณด์‹œ์˜ค.

 

• Main.java


public class Main {

	public static void main(String[] args) {
		ListContainer list = new ListContainer();
		
		list.insertNode(new Node(0, "0"));
		list.insertNode(new Node(1, "11"));
		list.insertNode(new Node(0, "22"));
		list.insertNode(new Node(1, "33"));
		list.insertNode(new Node(0, "44"));
		
		list.deleteNodeByIntValue(0);
		
		System.out.println(list.getNodeCount());

	}

}

 

• Node.java


public class Node {
	private NodeData data;
	private Node next;
	
	// ๊ธฐ๋ณธ์ƒ์„ฑ์ž 
	public Node()
	{
		data = new NodeData();
		next = null;
	}
	
	// ์˜ค๋ฒ„๋กœ๋”ฉ๋œ ์ƒ์„ฑ์ž
	public Node(NodeData data, Node next)
	{
		this.data = data;
		this.next = next;
	}
	
	public Node(int intValue, String strValue, Node next)
	{
		data = new NodeData(intValue, strValue);
		this.next = next;
	}
	
	public Node(int intValue, String strValue)
	{
		data = new NodeData(intValue, strValue);
		this.next = null;
	}
	
	// Getter, Setter
	public NodeData getData() {
		return data;
	}

	public void setData(NodeData data) {
		this.data = data;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}

	// ์žฌ์ •์˜ 
	@Override
	public String toString() {
		return "Node [data=" + data + "]";
	}
	
}

 

• NodeData.java


public class NodeData {
	private int intValue;
	private String strValue;
	
	// ๊ธฐ๋ณธ์ƒ์„ฑ์ž
	public NodeData()
	{
		intValue = 0;
		strValue = null;
	}
	
	// ์˜ค๋ฒ„๋กœ๋”ฉ๋œ ์ƒ์„ฑ์ž
	public NodeData(int intValue, String strValue)
	{
		this.intValue = intValue;
		this.strValue = strValue;
	}
	
	// Getter, Setter
	public int getIntValue() {
		return intValue;
	}

	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}

	public String getStrValue() {
		return strValue;
	}

	public void setStrValue(String strValue) {
		this.strValue = strValue;
	}

	// ์žฌ์ •์˜ 
	@Override
	public String toString() {
		return "NodeData [intValue=" + intValue + ", strValue=" + strValue + "]";
	}	
	
	
}

 

• ListContainer.java

public class ListContainer {
	private Node head;
	
	// ๋…ธ๋“œ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public int getNodeCount()
	{
		Node temp = head;
		int count = 1;
		
		if(head == null)
			return 0;
		
		while(temp.getNext() != null)
		{
			temp = temp.getNext();
			count++;
		}
		
		return count;
	}
	
	// ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
	public Node getLastNode()
	{
		Node lastNode = head;
		
		if(lastNode == null)
			return null;
		
		while(lastNode.getNext() != null)
			lastNode = lastNode.getNext();
		
		return lastNode;
	}
	
	// ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
	public void insertNode(Node newNode)
	{
		Node lastNode = null;
		
		if(this.head == null)
			head = newNode;
		
		else
		{
			// ๋งˆ์ง€๋ง‰๋…ธ๋“œ๋ฅผ ์ฐพ๊ณ  ๊ทธ ์œ„์น˜์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•œ๋‹ค. 
			lastNode = getLastNode();
			lastNode.setNext(newNode);
		}
	}
	
	// ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ver2
	// insert index๊ฐ€ ๊ธฐ์กด ์นด์šดํŠธ๋ณด๋‹ค ํฐ ๊ฐ’์ผ ๊ฒฝ์šฐ --> ๋…ธ๋“œ์˜ ๊ฐ€์žฅ ๋’ค์— 
	// index๊ฐ€ 0๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค. 
	public boolean insertNodeVer2(int index, Node newNode)
	{
		Node before = null;
		Node target = this.head;
		
		// index๊ฐ€ ์Œ์ˆ˜๋ผ๋ฉด --> false
		if(index < 0)
			return false;
		
		// index๊ฐ€ 0์ผ ๊ฒฝ์šฐ --> newNode์˜ next์— head ๋„ฃ์–ด์ฃผ๊ณ  head์— newNode๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
		if(index == 0)
		{
			newNode.setNext(this.head);
			this.head = newNode;
			
			return true;
		}
		
		// ๊ฐ€์žฅ ๋์— ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ทธ๋ณด๋‹ค ๋” ํฐ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•˜์„ ๊ฒฝ์šฐ --> insertNode๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ 
		if(index >= getNodeCount())
		{
			insertNode(newNode);
			
			return true;
		}
		
		// ๋ฐ˜๋ณต๋ฌธ ๋Œ์•„๊ฐ€๋ฉฐ index๋ฅผ ์ฐพ๊ณ  ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
		for(int i = 0; i < index; i++)
		{
			before = target;
			target = target.getNext();
		}
		
		newNode.setNext(before.getNext());
		before.setNext(newNode);
		
		return true;
	}
	
	// ํŠน์ • intํ˜• ๋ฐ์ดํ„ฐ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ ์‚ญ์ œ
	public boolean deleteNodeByIntValue(int intValue)
	{
		Node before = null;
		Node target = head;
		
		while(target != null)
		{
			// target์˜ intValue์™€ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด 
			if(target.getData().getIntValue() == intValue)
			{
				// target์ด ํ˜„์žฌ head์˜ ์ฐธ์กฐ๊ฐ’๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด --> head ์žฌ์„ค์ • 
				if(target == head)
				{
					head = target.getNext();
					
					target = head;
					
					continue;
				}
				
				// ์ด์ „๋…ธ๋“œ์™€์˜ ์—ฐ๊ฒฐ ๋Š์–ด๋ฒ„๋ฆฌ๊ณ  ์ด์ „๋…ธ๋“œ์˜ next๋Š” target์˜ next๋กœ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ์ž. 
				else
				{
					before.setNext(target.getNext());
					target = target.getNext();
					
					continue;
				}
			}
			
			// ์–ด๋Š๊ฒƒ๋„ ํ•ด๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค์Œ target์„ ํ™•์ธํ•˜์ž
			before = target;
			target = target.getNext();
		}
		
		return true;
	}
	
	// ํŠน์ • ์ธ๋ฑ์Šค์œ„์น˜์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
	public boolean deleteNode(int index)
	{
		int count;
		Node before = null;
		Node target = this.head;
		
		count = getNodeCount();
		
		// ์ง€์ •๋œ ์ธ๋ฑ์Šค๊ฐ€ ์ž˜๋ชป ์ž…๋ ฅ๋˜์—ˆ์„ ๊ฒฝ์šฐ --> false
		if((index >= count) || (index < 0))
			return false;
		
		// head๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ --> head ์žฌ์„ค์ •
		if(index == 0)
		{
			head = head.getNext();
			return true;
		}
		
		for(int i = 0; i < index; i++)
		{
			before = target;
			target = target.getNext();
		}
		
		if(before == null)
			return false;
		
		else
			before.setNext(target.getNext());
		
		return true;
	}
}

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€