using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceStudy12
{
internalclassProgram
{
staticvoidMain(string[] args)
{
var linkedlist = new LinkedList();
var node = new Node("홍길동");
linkedlist.Add(node);
Node node1 = new Node("임꺽정");
linkedlist.Add(node1);
Node node2 = new Node("장길산");
linkedlist.Add(node2);
Node node3 = new Node("임꺽정");
linkedlist.Remove(node3);
linkedlist.AddAfter(node, node1);
linkedlist.Count();
var a = linkedlist.getNode(2);
Console.WriteLine(a.data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceStudy12
{
publicclassLinkedList
{
public Node head;
publicvoidAdd(Node node)
{
//비어있다면if (this.head == null)
{
this.head = node;
}
else
{
//비어있지 않다면, 헤드가 있다면//this.head.next = node;var current = this.head;
while (current != null && current.next !=null)
{
current = current.next;
}
current.next = node;
}
}
publicvoidRemove(Node removeNode)
{
//head가 null이면if (head == null || removeNode == null) return;
//삭제할 노드가 head이면if (this.head.data == removeNode.data)
{
this.head = this.head.next;
}
else//삭제할 노드가 head가 아니면
{
var current = head;
while (current.next !=null && current.next.data != removeNode.data)
{
current = current.next;
}
if (current != null)
{
var temp = current.next.next;
current.next.next = null;
current.next = temp;
}
}
}
public Node getNode(int index)
{
var current = head;
for (int i = 0; i < index && current != null; i++)
{
current = current.next;
}
return current;
}
publicintCount()
{
int cnt = 0;
var current = this.head;
while (current != null)
{
cnt++;
current = current.next;
}
return cnt;
}
publicvoidAddAfter(Node current, Node newNode)
{
if (this.head == null || current == null) return;
newNode.next = current.next;
current.next = newNode;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceStudy12
{
publicclassNode
{
//데이터publicstring data;
//포인터public Node next;
//생성자publicNode(string data)
{
this.data = data;
this.next = null;
}
}
}