Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

자료구조 21.03.29.수업내용 본문

C#/수업내용 - 자료구조 & 알고리즘

자료구조 21.03.29.수업내용

HappyFrog 2021. 3. 29. 16:18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study801
{
    class DynamicArray
    {
        public int capacity;
        public int count;
        public int[] arrs;        
        
        public DynamicArray(int capacity)
        {
            this.capacity = capacity;
            this.arrs = new int[capacity];  //DynamicArray로 만든 인스턴스로 불러올 수 있는 배열 arrs는 DynamicArray에서 직접 받은 capacity값 만큼의 크기를 가진다.
        }       

        public void Add(int element)
        {
            //확장
            
            if(this.count >= this.capacity) //만약 이것의 count가 이것의 수용량과 같거나 크다면
            {
                int newSize = this.capacity * 2;    //newSize는 이것의 수용량의 2배이며
                int[] temp = new int[newSize];      //temp배열의 크기는 newSize(arrs배열 크기의 2배)이다.
                for(int i = 0; i < this.arrs.Length; i++)   //temp배열에 arr값을 하나씩 옮겨주는 과정.
                {
                    temp[i] = arrs[i];
                }
                this.arrs = temp;       //temp로 옮겨간 배열의 값들을 arrs로 다시 옮겨주는 과정.
                this.capacity = newSize;    //배열 늘어날 때 마다 capacity값을 재할당 해줘서 반복적으로 늘어날 수 있게 해준다.
            }

            this.arrs[count] = element; //count 인덱스 값에 element를 넣는다.
            count++;    //한 번 할때마다 count는 1씩 증가한다.

        }
        public int Get(int index)   //누구세요
        {
            return -1;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;


namespace Study801
{
    class App
    {        
        public App()
        {
            DynamicArray arr = new DynamicArray(2);
            arr.count = 0;
            
            for(int i = 0; i < 100; i++)
            {
                arr.Add(i + 1);
                Console.WriteLine("인덱스의 값{0} / 배열의 인덱스{1} / 배열의 길이{2}", arr.arrs[i], i, arr.arrs.Length);
            }
        }
    }
}

정말 완벽하게 멋져

============================================================================

원형배열

배열 인덱스를 증가시킬때 모드연산자를 이용하여 끝 배열 다음은 첫번째 배열로 돌아오게 한다.

ArrayList = object타입의 동적배열

List<T> = Generic타입의 동적배열

 

연결 리스트( Linked List)

ㄴ 연결 리스트는 각 노드가 데이터와 포인터를 가지고 있으면서 노드들이 한 줄로 쭉 연결되어 있는 방식으로 데이터를 저장하는 자료 구조이다.

 

단일 연결 리스트의 노드 클래스는 그 노드가 기본적으로 가지고 있는 데이터 필드와 다음 노드를 가리키는 포인터(참조같은 느낌)를 가지고 있다.

 

이들 노드들을 단방향으로 연결한 단일 연결 리스트 클래스는 리스트의 첫 노드를 가리키는 헤드(Head) 필드를 가지게 되는데, 이 Head를 사용하여 전체 리스트를 순차적으로 엑세스하게 된다.

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

namespace Study801
{
    class SingleLinkedList
    {
        private Node head;  //Head부터 순차적 접근하기 위함
        //생성자
        public SingleLinkedList()
        {

        }

        //노드 추가
        public void Add(Node newNode)
        {
            if(head == null)
            {
                this.head = newNode;
            }
            else
            {
                //head부터 next가 null인 것 찾기
                //비교대상을 임시저장 
                Node current = this.head;
                while(current != null && current.next!=null)
                {
                    //next가 있음
                    current = current.next;                    
                }
                //새로운 노드 연결
                current.next = newNode;
            }
        }
    }

    public class Node
    {
        public int data;
        public Node next;
        public Node(int data)
        {
            this.data = data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;


namespace Study801
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

        }
    }
}

============================================================================

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

namespace Study801
{
    class SingleLinkedList
    {
        private Node head;  //Head부터 순차적 접근하기 위함
        //생성자
        public SingleLinkedList()
        {

        }

        //노드 추가
        public void Add(Node newNode)
        {
            if(head == null)
            {
                this.head = newNode;
            }
            else
            {
                //head부터 next가 null인 것 찾기
                //비교대상을 임시저장 
                Node current = this.head;
                while(current != null && current.next!=null)
                {
                    //next가 있음
                    current = current.next;                    
                }
                //새로운 노드 연결
                current.next = newNode;
            }
        }

        //노드 수 세기
        public int Count()
        {
            int count = 0;

            Node current = this.head;
            while (current != null)
            {
                count++;
                current = current.next;
            }

            return count;
        }
    }

    public class Node
    {
        public int data;
        public Node next;
        public Node(int data)
        {
            this.data = data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;


namespace Study801
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            SingleLinkedList list = new SingleLinkedList();

            Console.WriteLine(list.Count());	//0
            list.Add(new Node(1));
            Console.WriteLine(list.Count());	//1
            list.Add(new Node(2));
            Console.WriteLine(list.Count());	//2
        }
    }
}

============================================================================

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

namespace Study801
{
    class SingleLinkedList
    {
        private Node head;  //Head부터 순차적 접근하기 위함
        //생성자
        public SingleLinkedList()
        {

        }

        //노드 추가
        public void Add(Node newNode)
        {
            if (head == null)
            {
                this.head = newNode;
            }
            else
            {
                //head부터 next가 null인 것 찾기
                //비교대상을 임시저장 
                Node current = this.head;
                while (current != null && current.next != null)
                {
                    //next가 있음
                    current = current.next;
                }
                //새로운 노드 연결
                current.next = newNode;
            }
        }

        public void Delete()    //제일 끝 노드 지우기
        {
            if (this.head == null)
            {
                Console.WriteLine("이미 비어있습니다.");
            }
            else
            {
                Node current = this.head;
                while (current != null && current.next != null)
                {
                    current = current.next;
                }
                current = null;
            }
        }

        public void PrintAll()
        {
            if (this.head == null)
            {
                Console.WriteLine("아무 값도 없습니다.");
            }
            else
            {
                Node current = this.head;
                while (current != null)
                {
                    Console.WriteLine("데이터 값 : {0}", current.data);
                    current = current.next;
                }
            }
        }

        public void Insert(int indexNum, Node newNode)
        {

            if (this.head != null)
            {
                Node current = this.head;
                for (int i = 0; i < indexNum - 1; i++)
                {
                    current = current.next;
                }
                newNode.next = current.next;
                current.next = newNode;
            }
        }

        //노드 수 세기
        public int Count()
        {
            int count = 0;

            Node current = this.head;
            while (current != null)
            {
                current.id = count;
                count++;
                current = current.next;
            }
            return count;
        }

        //노드 추가
        public void AddAfter(Node current, Node newNode)
        {
            if (this.head == null || current == null || newNode == null)
            {
                throw new InvalidOperationException();
            }

            newNode.next = current.next;
            current.next = newNode;
        }

        public void Remove(Node target) //원하는 부분 잘라내기
        {
            Node current = this.head;
            if (this.head == null || target == null)
            {
                return;
            }
            else if (target == this.head)
            {
                this.head = this.head.next;
                target = null;
            }
            else
            {
                while (current != null && current.next != target)
                {
                    current = current.next;
                }
                current.next = target.next;
                target = null;
            }            
        }
    }

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


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


namespace Study801
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            SingleLinkedList list = new SingleLinkedList();

            var head = new Node(1);
            list.Add(head);
            list.Add(new Node(2));
            Node node3 = new Node(3);
            list.Add(node3);
            Console.WriteLine(list.Count());
            list.Insert(2, new Node(5));
            list.AddAfter(head.next, new Node(4));
            list.PrintAll();
            list.Remove(node3);
        }
    }
}