๊ตญ๋น์ง์ 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;
}
}
๋ฐ์ํ
'์๋ > ๊ตญ๋น์ง์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[D+29] DB ํ ์ด๋ธ ์์ฑ ๋ฐ ๋ฐ์ดํฐ ์ถ๊ฐ/์ญ์ , ์กฐํ, ์๋ธ์ฟผ๋ฆฌ (0) | 2022.11.02 |
---|---|
[D+28] ๋ฐ์ดํฐ๋ฒ ์ด์ค Database (0) | 2022.11.01 |
[D+26] ์์ฉSW ๊ธฐ์ด ๊ธฐ์ ํ์ฉ (0) | 2022.10.27 |
[D+25] ๊ธฐ๋ณธ API (0) | 2022.10.26 |
[D+24] ์์ธ์ฒ๋ฆฌ (0) | 2022.10.25 |
๋๊ธ