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

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

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

 

 

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

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

 


 

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

Q) Linked List๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.

 

• Main.java

public class Main {
	
	// ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ getํ•˜๋Š” ํ•จ์ˆ˜
	public static Node getLastNode(Node head)
	{
		Node lastNode = head;
		
		while(lastNode.getNext() != null)
		{
			lastNode = lastNode.getNext();
		}
		
		return lastNode;
	}
	
	// ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜ 
	public static void InsertNode(Node head, Node newNode)
	{
		Node lastNode = null;
		
		// 1. ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
		lastNode = getLastNode(head);
		
		// 2. ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•œ๋‹ค. 
		lastNode.setNext(newNode);
	}
	
	// ๋…ธ๋“œ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ 
	public static int getNodeCount(Node head)
	{
		Node temp = head;
		int count = 1;
		
		while(temp.getNext() != null)
		{
			temp = temp.getNext();
			count++;
		}
		
		return count;
	}
	
	// ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜ --> 0๋ฒˆ ๋…ธ๋“œ๋Š” ์•ˆ์ง€์šฐ๋Š” ํ•จ์ˆ˜ 
	// index : ์ง€์šธ ๋Œ€์ƒ์˜ ์ธ๋ฑ์Šค
	public static boolean DeleteNode(Node head, int index)
	{
		int count;
		Node target = head;		// ์ง€์šธ๋Œ€์ƒ ๋…ธ๋“œ 
		Node before = null;		// target์˜ ์ด์ „๋…ธ๋“œ 
		
		count = getNodeCount(head);
		
		if((index >= count) || (index <= 0))
		{
			return false;
		}
		
		for(int i = 0; i < index; i++)
		{
			before = target;
			target = target.getNext();
		}
		
		if(before == null)
		{
			return false;
		}
		
		else
		{
			before.setNext(before.getNext());
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		Node head = new Node(0, null);		// intValue, strValue
		
		for(int i = 0; i < 10; i++)
		{
			InsertNode(head, new Node(i+1, null));
		}
		
		DeleteNode(head, 3);

	}

}

 

• Node.java

public class Node {
	private NodeData data;
	private Node next;
	
	///////////////////////////////////////////////////////////////
	// ์ƒ์„ฑ์ž
	
	// ๊ธฐ๋ณธ์ƒ์„ฑ์ž 
	public Node()
	{
		data = new NodeData();		// null์„ ์„ค์ •ํ•˜๋Š” ๊ฑด ์˜๋ฏธ๊ฐ€ ์—†์Œ.  
		next = null;
	}
	
	// ์˜ค๋ฒ„๋กœ๋”ฉ๋œ ์ƒ์„ฑ์ž
	public Node(NodeData data, Node next)
	{
		this.data = data;
		this.next = next;
	}
	
	public Node(int intValue, String strValue, Node next)
	{
		this.data = new NodeData(intValue, strValue);
		this.next = next;
	}
	
	public Node(int intValue, String strValue)
	{
		this.data = new NodeData(intValue, strValue);
	}
	
	///////////////////////////////////////////////////////////////
	// 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;
	}
}

 

• 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;
	}
	
	
}

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€