ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 배열 리스트 (Array List)
    언어/C, C++ 2020. 12. 29. 04:03
    #pragma once
    
    #include <iostream>
    using namespace std;
    
    const int MAX = 100;
    
    class ArrayList
    {
    private:
    	int m_count;
    	int m_data[MAX];
    
    public:
    	ArrayList();
    	ArrayList(const ArrayList& _arraylist);
    	~ArrayList();
    
    	void Insert(int _position, int _item);
    	void Delete(int _position);
    	int Retrieve(int _position);
    
    	bool IsFull();
    	bool IsEmpty();
    	int GetLength();
    
    };

    ArrayList.h

     

     

    #include "ArrayList.h"
    
    ArrayList::ArrayList() : m_count(0)
    {
    	for (int i = 0; i < MAX; i++)
    		m_data[i] = 0;
    
    }
    
    ArrayList::ArrayList(const ArrayList& _arraylist) : m_count(_arraylist.m_count)
    {
    	for (int i = 1; i <= _arraylist.m_count; ++i)
    		m_data[i - 1] = _arraylist.m_data[i - 1];
    
    }
    
    ArrayList::~ArrayList()
    {
    
    }
    
    void ArrayList::Insert(int _position, int _item)
    {
    	for (int current = m_count - 1; current >= _position; current--)
    		m_data[current + 1] = m_data[current];
    
    	m_data[_position] = _item;
    	m_count++;
    
    }
    
    void ArrayList::Delete(int _position)
    {
    	for (int current = _position - 1; current < m_count; current++)
    		m_data[current] = m_data[current + 1];
    
    	m_count--;
    
    }
    
    int ArrayList::Retrieve(int _position)
    {
    	return IsEmpty() ? -1 : m_data[_position];
    
    }
    
    bool ArrayList::IsFull()
    {
    	return m_count == MAX;
    
    }
    
    bool ArrayList::IsEmpty()
    {
    	return m_count == 0;
    
    }
    
    int ArrayList::GetLength()
    {
    	return m_count;
    
    }
    

    ArrayList.cpp

    '언어 > C, C++' 카테고리의 다른 글

    연결 리스트 스택 (Linked List Stack)  (0) 2021.01.13
    배열 스택 (Array Stack)  (0) 2021.01.12
    이중 연결 리스트 (DoubleLinkedList)  (0) 2021.01.05
    연결 리스트 (Linked List)  (0) 2020.12.31
    로또(Lotto) 프로그램  (0) 2020.08.20

    댓글