Skip to content

Instantly share code, notes, and snippets.

@Youngchangoon
Created May 13, 2019 23:09
Show Gist options
  • Save Youngchangoon/b60c96b72e6d60e45baaf3610eb8c5ed to your computer and use it in GitHub Desktop.
Save Youngchangoon/b60c96b72e6d60e45baaf3610eb8c5ed to your computer and use it in GitHub Desktop.
Circula Linked List
using System;
namespace ConsoleApp1
{
public class Program
{
private static CirculaLinkedList<int> circulaLinkedList;
static void Main(string[] args)
{
circulaLinkedList = new CirculaLinkedList<int>();
// Insert
circulaLinkedList.InsertNode(10);
circulaLinkedList.InsertNode(20);
circulaLinkedList.InsertNode(5, false);
circulaLinkedList.InsertNode(15, 2);
circulaLinkedList.InsertNode(30);
circulaLinkedList.InsertNode(30);
circulaLinkedList.InsertNode(30);
// Add Fail Case
circulaLinkedList.InsertNode(100, 9);
// Remove
circulaLinkedList.RemoveNode(20);
circulaLinkedList.RemoveNode(30, true);
circulaLinkedList.RemoveAt(1);
circulaLinkedList.RemoveAt(100);
circulaLinkedList.PrintNode();
}
}
public class CirculaLinkedList<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 CirculaLinkedList()
{
// 먼저 머리와 꼬리 노드를 만들어준다. ㅁ(head) -> ㅁ(tail)
head = new Node<T>();
tail = new Node<T>();
// (head) -> tail
head.next = tail;
// (tail) -> head
tail.next = head;
}
/// <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()
{
var startNode = head;
for (var i = 0; i < 50; ++i)
{
if (startNode == head)
{
Console.Write("HEAD->");
startNode = head.next;
}
else if (startNode == tail)
{
Console.WriteLine("TAIL->");
startNode = tail.next;
}
else
{
Console.Write(startNode.data + "->");
startNode = startNode.next;
}
}
Console.WriteLine("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment