C#/수업 내용

[C#] Linked List (연결리스트), Flowchart (순서도)

JSH1 2021. 9. 15. 11:07

    class Node
    {
        public int data;
        public Node next;

        public Node(int data)
        {
            this.data = data;
        }
    }
            Node node = new Node(5);
            Node head = node;

            Node head = new Node(5);

            Node head = new Node(5);
            head = null;

            Node head = new Node(5);
            head = new Node(6);

            Node head = new Node(5);
            head.next = new Node(6);

            Node head = new Node(5);
            head.next = new Node(6);
            head.next.next = new Node(7);

Flowchart (순서도)


    class App
    {
        Node head;

        public App()
        {
            AddNode(new Node(5));
            AddNode(new Node(6));
            AddNode(new Node(7));
            AddNode(new Node(8));

            if (head != null)
            {
                head.GetCount(1);
            }
        }

        void AddNode(Node node)
        {
            if (head == null)
            {
                head = node;
            }

            else
            {
                head.Next(node);
            }
        }
    }
    class Node
    {
        public int data;
        public Node next;

        public Node(int data)
        {
            this.data = data;
        }

        public void Next(Node node)
        {
            if (this.next == null)
            {
                this.next = node;
            }

            else
            {
                this.next.Next(node);
            }
        }

        public void GetCount(int number)
        {
            Console.WriteLine("data: {0}", this.data);

            if (this.next != null)
            {
                number++;
                this.next.GetCount(number);
                return;
            }

            Console.WriteLine("GetCount: {0}", number);
        }
    }


        Node head;

        public App()
        {
        	head.RemoveNode(5, RemoveHead);
            head.RemoveNode(1, RemoveHead);
            head.RemoveNode(7, RemoveHead);
            head.RemoveNode(3, RemoveHead);
        }
        
        void RemoveHead ()
        {
            head = null;
            Console.WriteLine("RemoveNode(head)");
        }
        public delegate void Del();
        
        public void RemoveNode(int data, Del callback)
        {
            if (this.data == data)
            {
                Console.WriteLine("delete data: {0}", this.data);


                if (this.next == null)
                {
                    callback();
                    return;
                }

                else
                {
                    Node temp = next;
                    this.next = null;
                    this.data = temp.data;
                    this.next = temp.next;
                    return;
                }
            }

            else if (this.next.data == data && this.next.next == null)
            {
                Console.WriteLine("delete data: {0}", this.next.data);
                this.next = null;
                return;
            }

            this.next.RemoveNode(data, callback);
        }


                head.InsertNode(3, new Node(4));
        public void InsertNode(int targetNodeData, Node newNode)
        {
            if (this.data == targetNodeData)
            {
                if (this.next == null)
                {
                    this.next = newNode;
                }

                else
                {
                    Node temp = this.next;
                    this.next = newNode;
                    this.next.next = temp;
                }
            }

            else if(this.next != null)
            {
                this.next.InsertNode(targetNodeData, newNode);
            }
        }


 

'C# > 수업 내용' 카테고리의 다른 글

[C#] Stack을 Linked List로 구현하기  (0) 2021.09.17
[C#] 재귀함수  (0) 2021.09.16
[C#] 동적 배열  (0) 2021.09.14
[C#] params, out, ref  (0) 2021.09.13
[C#] thread, lock  (0) 2021.09.12