๊ตญ๋น์ง์ 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;
}
}
๋ฐ์ํ
'์๋ > ๊ตญ๋น์ง์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[D+23] ์ธํฐํ์ด์ค, ์ถ์ํด๋์ค (0) | 2022.10.24 |
---|---|
[D+22] Linked List (0) | 2022.10.24 |
[D+21] ์์ ๊ต์ฌ์ ๋ฆฌ, ์ธํฐํ์ด์ค (0) | 2022.10.20 |
[D+20] ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ํ์ฉ (0) | 2022.10.20 |
[D+19] ์ค๋ฒ๋ผ์ด๋ฉ๊ณผ ์์๊ด๊ณ (0) | 2022.10.19 |
๋๊ธ