본문 바로가기
c#/수업 내용

자료구조 Linked List 연결리스트(Node 연습 )

by 이지훈26 2021. 9. 15.

1. 클래스 다이어그램 사용하기

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        
        public App()
        {
            Node node = new Node(5);
            Node head = node;
            
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class Node
    {
        public int data;
        public Node next;

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

        void Next()
        {

        }
    }
}

 

 

2.참조 관계 연결 끊기(null 사용)

리터럴(literal)이란 소스 코드의 고정된 값을 대표하는 용어다.

null 키워드는 개체를 참조하지 않는 null 참조를 나타내는 리터럴입니다

 

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        
        public App()
        {
            Node head = new Node(5);
            head = null;

            
        }
    }
}

 

3.참조 관계 변경

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        
        public App()
        {
            Node head = new Node(5);
            head = new Node(6);

            
        }
    }
}

 

4.참조 관계 연결하기

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        
        public App()
        {
            Node head = new Node(5);
            head.next = new Node(6);

        }
    }
}

 

5.참조 관계 여러개 연결하기

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        
        public App()
        {
            Node head = new Node(5);
            head.next = new Node(6);
            head.next.next = new Node(11);
            head.next.next.next = null;

        }
    }
}

 

head의 마지막 Node의 next를 찾는 알고리즘

1.알고리즘 다이어그램

2.순서화

수정

->

알고리즘 다이어그램 5번, 6번 에서 node를 수정

 

3.코드화

 

코드

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Helloworld1
{
    
    class App
    {
        Node head;
        
        public App()
        {

            Node node = new Node(3);
            AddNode(node);
            Console.WriteLine(head.data);

            node = new Node(5);
            AddNode(node);
            Console.WriteLine(head.next.data);

            node = new Node(7);
            AddNode(node);
            Console.WriteLine(head.next.next.data);

        }

        void AddNode(Node node)
        {
            //head가 없다면
            if(head == null)
            {
                //head에 할당
                head = node;
            }
            //head가 있다면 
            else
            {
                //head의 next의 마지막 노드의 next에 할당
                Node temp = head;
                while(true)
                {
                    if(temp.next == null)
                    {
                        temp.next = node;
                        break;
                    }
                    else
                    {
                        temp = temp.next;
                    }
                }
            }
        }
    }
}

 

노드의 객수를 반환하는 메서드를 정의하고 구현하세요.

1.알고리즘 다이어그램

2.순서도

3.코드

 

데이터가 3인 노드를 찾아서 head로부터 연결된 노드에서 제외

1.알고리즘 다이어그램

2.순서도

3.코드

 

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

자료구조 재귀 함수 정의  (0) 2021.09.16
자료구조 순서도 정의  (0) 2021.09.15
배열 swap 그리기 연습  (0) 2021.09.14
동적배열 (Dynamic Array)그리기 연습  (0) 2021.09.14
자료구조(배열)  (0) 2021.09.14