C# LinkedList

Algorithm 2021. 3. 15. 15:56
반응형
using System;

namespace Test
{
    public class Test
    {
        //생성자 
        public Test()
        {
            LinkedList linkedList = new LinkedList();

            Node head = linkedList.CreateNode(1);
            linkedList.AppendNode(head);

            Node node = linkedList.CreateNode(2);
            linkedList.AppendNode(node);

            var count = linkedList.GetNodeCount(head);
            Console.WriteLine(count);
        }
    }
}
using System;
namespace Test
{
    public class Node
    {
        public int data;
        public Node nextNode;
    }

    public class LinkedList
    {
        private Node head;
        //constructor 
        public LinkedList()
        {
            Console.WriteLine("LinkedList Constructor"); ;
        }
        //노드 생성 
        public Node CreateNode(int data)
        {
            Node node = new Node();
            node.data = data;
            node.nextNode = null;
            return node;
        }
        //노드 소멸 
        public void DestoryNode(Node node)
        {
            node = null;
        }
        //노드 추가 
        public void AppendNode(Node node)
        {
            if (head == null)
            {
                this.head = node;
            }
            else
            {
                Node tail = head;
                while (tail.nextNode != null)
                {
                    tail = tail.nextNode;
                }
                tail.nextNode = node;
            }
        }
        //노드 삽입 
        public void InsertAfter(Node current, Node newNode)
        {
            newNode.nextNode = current.nextNode;
            current.nextNode = newNode;
        }

        public void InsertNewHead(Node head, Node newHead)
        {
            if (head == null)
            {
                head = newHead;
            }
            else
            {
                newHead.nextNode = head;
                head = newHead;
            }
        }

        //노드 제거 
        public void RemoveNode(Node head, Node remove)
        {
            if (head == remove)
            {
                head = remove.nextNode;
            }
            else
            {
                Node current = head;
                while (current != null && current.nextNode != remove)
                {
                    current = current.nextNode;
                }
                if (current != null)
                {
                    current.nextNode = remove.nextNode;
                }
            }
        }

        //노드가져오기 
        public Node GetNodeAt(Node head, int location)
        {
            Node current = head;
            while (current != null && (--location) >= 0)
            {
                current = current.nextNode;
            }
            return current;
        }

        //노드의 수 
        public int GetNodeCount(Node head)
        {
            int count = 0;
            Node current = head;

            while (current != null)
            {
                current = current.nextNode;
                count++;
            }
            return count;
        }

    }//end class 
}//end namespace 

반응형

'Algorithm' 카테고리의 다른 글

링크드 리스트  (0) 2021.03.19
C# Array  (0) 2021.03.15
Simple Binary Tree  (0) 2021.03.05
LCRSTree  (0) 2021.03.04
완주하지 못한 선수  (0) 2021.01.07
: