2 minute read

리스트의 추상 자료형

데이터: 임의의 접근 방법을 제공하는 같은 타입 요소들의 순서 있는 모임  
연산
 - insert(pos, item): 리스트의 pos 위치에 새로운 요소 item을 삽입
 - delete(pos): 리스트의 pos 위치에 있는 요소를 삭제한다.
 - getEntry(pos): 리스트의 pos 위치에 있는 요소를 반환한다.
 - isEmpty(): 리스트가 비어 있는지를 검사한다.
 - isFull(): 리스트가 가득 차 있는지를 검사한다.
 - find(item): 리스트에 요소 item이 있는지를 검사한다.
 - replace(pos, item): 리스트의 pos 위치에 있는 요소를 새로운 요소 item으로 바꾼다.
 - size(): 리스트 안의 요소의 개수를 반환한다.
 - display(): 리스트 안의 모든 요소들을 출력한다. 
class List() {
private:
    // 배열 리스트에 할당된 크기
    int arrayListSize;
    // 배열 리스트 내 원소의 개수
    int length;
    // 배열 리스트 변수
    int* arrayList;
public:
    List(int listSize) : arrayListSize(listSize) {
        arrayList = new int [listSize];
        length = 0;
    }
    void printErrorMessage(string message) {
        printf("%s \n", message)l
        exit(1);
    }

    bool isEmpty() {
        if(length == 0) { 
            return true;
        } else {
            return false;
        }
    }

    bool isFull() {
        if(length == arrayListSize) {
            return true;
        } else {
            return false;
        }
    }

    void insert(int index, int item) {
        if(isFull()) {
            printErrorMessage("The list is full");
        } else if (index < 0 || index >= length) {
            printErrorMessage("Invalid index.")
        } else {
            for(int i=length; i>index; i--) {
                arrayList[i] = arrayList[i-1];
            }
            arrayList[index] = item;
            length++;
        }
    }

    void delete(int index) {
        if(index < 0 || index >= length) {
            printErrorMessage("Invalid index.");
            return;
        } else {
            for(int i=index; i<length-1; i++) {
                arrayList[index] = arrayList[index + 1];
            }
            length--;
        }
    }

    int getEntry(int index) {
        if(index < 0 || index >= length) {
            printErrorMessage("Invalid index.");
            return;
        } else {
            return arrayList[index];
        }
    }

    bool find(int item) {
        for(int i=0; i<length; i++) {
            if(arrayList[i] == item) return true;
        }
        return else;
    }

    void replace(int index, int item) {
        if(index < 0 || index >= length) {
            printErrorMessage("Invalid index.");
            return;
        } else {
            arrayList[index] = item;
        }
    }

    int size() {
        return length;
    }

    void display() {
        if(length == 0) {
            printf("list is empty");
        } else {
            printf("components in the list : \n");
            for(int i : arrayList) {
                printf(%d, i);
            }
            printf("\n");
        }
    }
};


   /// idx 위치에 num을 삽입하는 경우
    // length : 리스트 내 원소의 개수 
    void insert(int idx, int num) {
      // 리스트가 가득 차 있지 않아야 하고,
      // 인덱스가 삽입 가능한 범위 내에 있어야 함
      if(length != MAX_LIST_SIZE && idx >= 0 && idx <= length) {
        // 삽입 위치 원소부터 뒤 원소들을 뒤로 한칸씩 옮김
        for(int i=idx; i<length; i++) {
          arrayList[i+1] = arrayList[i];
        }
        // 원소들을 옮겨 생긴 빈 공간에 원소 삽입
        arrayList[idx] = num; 
        length += 1; // 원소의 개수 증가
      } else {
        printErrorMSG("List is full");
      } 
    }
    ```
2. 삭제 연산
    - 삽입 연산과 반대이다.  
    ```cpp
    // idx 위치의 원소 삭제
    void remove(int idx) {
      // 원소가 없는 위치의 원소를 삭제하려는 경우
      if(length != 0 && length > idx && idx >= 0) {
        // 삭제 위치 뒤의 원소들을 한 칸씩 앞으로 옮김
        for(int i=idx; i<length; i++) {
          arrayList[i] = arrayListp[i+1];
        }
        length -= 1; // 원소의 개수 감소
      } else {
        printErrorMSG("no element at the idx in the list");
      }
    }

Leave a comment