[자료구조] 순차리스트

2023. 12. 24. 15:59·Computer Science/Data Structure
  • 리스트에 대한 설명은 다음을 참조

[자료구조] 배열을 이용한 리스트 구현


순차리스트 구현


  • ArrayList.h
#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__
#define TRUE 1
#define FALSE 0

#define LIST_LEN 100
typedef int LData;

typedef struct _ArrayList {
    LData arr[LIST_LEN];
    int numOfData;
    int curPosition;
} ArrayList;

typedef ArrayList List;
void ListInit(List *plist);
void LInsert(List *plist, LData data);
int LFirst(List *plist, LData *pdata);
int LNext(List *plist, LData *pdata);
LData LRemove(List *plist);
int LCount(List *plist);
#endif
  • ArrayList.c
#include <stdio.h>
#include "ArrayList.h"

void ListInit(List *plist) {
    plist->numOfData = 0; // 초기화
    // plist->curPosition = 0;
    plist->curPosition = -1; // 초기화
}

void LInsert(List *plist, LData data) {
    if (plist->numOfData < LIST_LEN) {
        // 데이터를 배열에 저장
        plist->arr[plist->numOfData] = data;
        // 배열에 저장된 데이터의 수 증가
        plist->numOfData++;
    }
}

int LFirst(List *plist, LData *pdata) {
    if (plist->numOfData > 0) {
        plist->curPosition = 0;
        *pdata = plist->arr[plist->curPosition];
        return 1;
    }
    else {
        return 0;
    }
}

int LNext(List *plist, LData *pdata) {
    if (++plist->curPosition >= plist->numOfData) {
        return 0;
    }
    else {
        *pdata = plist->arr[plist->curPosition];
        return 1;
    }
}

LData LRemove(List *plist) {
    int result = plist->arr[plist->curPosition];
    for (int i = plist->curPosition; i < plist->numOfData - 1; i++) {
        plist->arr[i] = plist->arr[i + 1];
    }
    plist->curPosition--; // 놓친 부분
    plist->numOfData--;
    return result;
}

int LCount(List *plist) {
    return plist->numOfData;
}
  • ListMain.c
#include <stdio.h>
#include "ArrayList.h"
#include "ArrayList.c"
#define ANSWER 11

int main(void)
{
    /*** ArrayList의 생성 및 초기화 ***/
    List list;
    int data;
    ListInit(&list);

    /*** 5개의 데이터 저장 ***/
    LInsert(&list, 11);  LInsert(&list, 11);
    LInsert(&list, 22);  LInsert(&list, 22);
    LInsert(&list, 33);

    /*** 저장된 데이터의 전체 출력 ***/
    printf("현재 데이터의 수: %d \n", LCount(&list));

    if(LFirst(&list, &data))    // 첫 번째 데이터 조회
    {
        printf("%d ", data);

        while(LNext(&list, &data))    // 두 번째 이후의 데이터 조회
            printf("%d ", data);
    }
    printf("\n\n");

    /*** Answer를 탐색하여 모두 삭제 ***/
    if(LFirst(&list, &data))
    {
        if(data == ANSWER)
            LRemove(&list);

        while(LNext(&list, &data))
        {
            if(data == ANSWER)
                LRemove(&list);
        }
    }

    /*** 삭제 후 저장된 데이터 전체 출력 ***/
    printf("현재 데이터의 수: %d \n", LCount(&list));

    if(LFirst(&list, &data))
    {
        printf("%d ", data);

        while(LNext(&list, &data))
            printf("%d ", data);
    }
    printf("\n\n");
    return 0;
}

참조) 윤성우의 열혈 자료구조 (윤성우, 오렌지미디어)

'Computer Science > Data Structure' 카테고리의 다른 글

[자료구조] 스택(Stack)  (0) 2023.12.31
[자료구조] 배열의 응용 : 희소행렬  (0) 2023.12.26
[자료구조] 배열의 응용 : 다항식  (0) 2023.12.26
[자료구조] 배열을 이용한 리스트 구현  (0) 2023.12.24
[자료구조] 추상 자료형(Abstract Data Type)  (0) 2023.12.24
'Computer Science/Data Structure' 카테고리의 다른 글
  • [자료구조] 배열의 응용 : 희소행렬
  • [자료구조] 배열의 응용 : 다항식
  • [자료구조] 배열을 이용한 리스트 구현
  • [자료구조] 추상 자료형(Abstract Data Type)
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (463)
      • 개발 일지 (0)
        • Performance (0)
        • TroubleShooting (0)
        • Refactoring (0)
        • Code Style, Convetion (0)
        • Architecture (0)
      • Software Engineering (36)
        • Test (8)
        • 이론 (18)
        • Clean Code (10)
      • Java (72)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (13)
        • JVM (2)
      • Spring (53)
        • Framework (12)
        • MVC (23)
        • Transaction (3)
        • AOP (11)
        • Boot (0)
        • AI (0)
      • DB Access (16)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (130)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (6)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[자료구조] 순차리스트
상단으로

티스토리툴바