10.31 인덱서를 이용해 LinkedList 클래스 만들기
C#/실습 2019. 10. 31. 16:22예제:
코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
class App
{
public App()
{
LinkedList list = new LinkedList(4);
Item item1 = new Item(1, "단검");
list.PrintByIndex();
list.PrintByIndex();
list.PrintByNode();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class Item
{
public int Id { get; private set; }
public string Name { get; private set; }
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class LinkedList
{
public Node[] nodes;
public Node first;
public int count = 0;
public int Count
{
get
{
return count;
}
}
public LinkedList(int capacity)
{
Console.WriteLine("LinkedList 생성자가 호출되었습니다.");
nodes = new Node[capacity];
}
public void PrintByNode()
{
Console.WriteLine("노드 순으로 출력:");
if(first != null)
{
Node node = first;
while (node != null)
{
node = node.next;
}
}
}
public void PrintByIndex()
{
Console.WriteLine("인덱스 순으로 출력:");
foreach (Node node in nodes)
{
if (node != null)
{
}
}
}
public int CheckNumofNull()
{
{
if (nodes[i] != null)
nullCount--;
}
return nullCount;
}
private Node FindNode(Item item)
{
{
if (nodes[i].item == item)
{
return nodes[i];
}
}
return null;
}
public void Remove(Item item)
{
//1.아이템을 담고있는 해당 노드를 찾는다
var node = FindNode(item);//nodes[i]
if (node == null)
{
Console.WriteLine("해당 아이템을 찾을 수 없습니다.");
return;
}
//2.1 해당 노드가 first 인 경우 자신의 참조를 끊고 first 값을 자신의 next 값으로 바꿔준다.
//2.2 해당 노드를 next값으로 갖고있는 노드를 찾고 그걸 해당노드의 next값으로 바꿔준다.
if (node == first)
{
first = node.next;
count--;
}
else
{
{
if (nodes[i].next == node)
{
nodes[i].next = node.next;
count--;
break;
}
}
}
//3. 해당 노드를 참조하고있는 배열의 인덱스를 null로 바꾼다.
{
if (nodes[i] == node)
{
nodes[i] = null;
break;
}
}
//4. 해당 노드를 지운다. --> 해당 노드가 아무것도 참조받지 않고 참조 하지 않게한다.
}
public Node FindLastNode(Node node)
{
//배열이랑 상관이 없다.
{
return node;
}
return FindLastNode(node.next);
}
public void Add(Item item)
{
if (CheckNumofNull() == 0)
{
Console.WriteLine("배열에 공간이 없습니다.");
return;
}
Node node = new Node(item);
if (first == null) // 첫번째 노드가 없을때
{
first = node;
count++;
{
if (nodes[i] == null)
{
nodes[i] = node;
Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
break;
}
}
}
else // 첫번째 노드가 있을때.
{
Node lastNode = FindLastNode(this.first);
count++;
{
if (nodes[i] == null)
{
nodes[i] = node;
Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
break;
}
}
}
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class Node
{
public Node next;
public Item item;
public Node(Item item)
{
this.item = item;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'C# > 실습' 카테고리의 다른 글
11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기. (0) | 2019.11.07 |
---|---|
11.05 Unity 객체, 이벤트, prefeb파일 (2) | 2019.11.05 |
10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 비활용) (0) | 2019.10.31 |
10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용) (0) | 2019.10.31 |
10.30 클래스로 LinkedList 구현하기 (메서드, 속성의 재귀호출) (0) | 2019.10.30 |