-

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study12 { internal class Program { static void Main(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; namespace Study12 { public class LinkedList { public Node head; public void Add(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; } } public void Remove(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; } public int Count() { int cnt = 0; var current = this.head; while (current != null) { cnt++; current = current.next; } return cnt; } public void AddAfter(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; namespace Study12 { public class Node { //데이터 public string data; //포인터 public Node next; //생성자 public Node(string data) { this.data = data; this.next = null; } } }