Created
May 13, 2019 23:08
-
-
Save Youngchangoon/3a7616b67cd38c2d5024abb18f6b6088 to your computer and use it in GitHub Desktop.
Singly linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp1 | |
{ | |
public class Program | |
{ | |
private static SingleLinkedList<int> singleLinkedList; | |
static void Main(string[] args) | |
{ | |
singleLinkedList = new SingleLinkedList<int>(); | |
// Insert | |
singleLinkedList.InsertNode(10); | |
singleLinkedList.InsertNode(20); | |
singleLinkedList.InsertNode(5, false); | |
singleLinkedList.InsertNode(15, 2); | |
singleLinkedList.InsertNode(30); | |
singleLinkedList.InsertNode(30); | |
singleLinkedList.InsertNode(30); | |
// Add Fail Case | |
singleLinkedList.InsertNode(100, 9); | |
// Remove | |
singleLinkedList.RemoveNode(20); | |
singleLinkedList.RemoveNode(30, true); | |
singleLinkedList.RemoveAt(1); | |
singleLinkedList.RemoveAt(100); | |
singleLinkedList.PrintNode(); | |
} | |
} | |
public class SingleLinkedList<T> | |
{ | |
public class Node<T> | |
{ | |
public Node() { } | |
public Node(T newData) | |
{ | |
data = newData; | |
} | |
public T data; | |
public Node<T> next; | |
} | |
private Node<T> head; | |
private Node<T> tail; | |
public SingleLinkedList() | |
{ | |
// 먼저 머리와 꼬리 노드를 만들어준다. ㅁ(head) -> ㅁ(tail) | |
head = new Node<T>(); | |
tail = new Node<T>(); | |
// (head) -> tail | |
head.next = tail; | |
// (tail) -> NULL | |
tail.next = null; | |
} | |
/// <summary> | |
/// 맨 앞이나 맨 뒤에 Data를 삽입한다. | |
/// </summary> | |
/// <param name="newData"> 새로 넣을 데이터 </param> | |
/// <param name="addTail"> 꼬리에 추가할 것인지 </param> | |
public int InsertNode(T newData, bool addTail = true) | |
{ | |
var count = 0; | |
var addNode = new Node<T>(newData); | |
var tempNode = head; | |
if (addTail) | |
{ | |
while (tempNode.next != tail) | |
{ | |
tempNode = tempNode.next; | |
++count; | |
} | |
} | |
addNode.next = tempNode.next; | |
tempNode.next = addNode; | |
return count; | |
} | |
/// <summary> | |
/// 원하는 index에 Data를 추가한다. | |
/// </summary> | |
/// <param name="newData"></param> | |
/// <param name="index"></param> | |
/// <returns></returns> | |
public int InsertNode(T newData, int index) | |
{ | |
var count = 0; | |
var addNode = new Node<T>(newData); | |
var tempNode = head; | |
for (var i = 0; i < index; ++i) | |
{ | |
if (tempNode == tail) | |
{ | |
Console.WriteLine("Add Fail.."); | |
return 0; | |
} | |
tempNode = tempNode.next; | |
++count; | |
} | |
addNode.next = tempNode.next; | |
tempNode.next = addNode; | |
return count; | |
} | |
/// <summary> | |
/// 해당 데이터를 만나면 해당 노드를 삭제한다. | |
/// </summary> | |
/// <param name="removeNode"></param> | |
public int RemoveNode(T removeData, bool matchRemoveAll = false) | |
{ | |
var count = 0; | |
var deleteNode = head; | |
var deletePrevNode = head; | |
while (deleteNode != tail) | |
{ | |
// 지우려는 데이터가 같다면 | |
if (deleteNode.data.Equals(removeData)) | |
{ | |
deletePrevNode.next = deleteNode.next; | |
deleteNode = null; | |
if (matchRemoveAll) | |
{ | |
count += RemoveNode(removeData, matchRemoveAll); | |
break; | |
} | |
else | |
break; | |
} | |
deletePrevNode = deleteNode; | |
deleteNode = deleteNode.next; | |
++count; | |
} | |
return count; | |
} | |
public int RemoveAt(int index) | |
{ | |
var count = 0; | |
var deleteNode = head.next; | |
var deletePrevNode = head; | |
for (var i = 0; i < index; ++i) | |
{ | |
if (deleteNode == tail) | |
{ | |
Console.WriteLine("delete Fail.."); | |
return count; | |
} | |
deletePrevNode = deleteNode; | |
deleteNode = deleteNode.next; | |
} | |
deletePrevNode.next = deleteNode.next; | |
deleteNode = null; | |
return count; | |
} | |
public void PrintNode() | |
{ | |
Console.Write("HEAD->"); | |
for (var tempNode = head.next; tempNode != tail; tempNode = tempNode.next) | |
{ | |
Console.Write(tempNode.data + "->"); | |
} | |
Console.WriteLine("TAIL"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment