[자료구조] 순차리스트

2023. 12. 24. 15:59·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;
}

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

'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
'Data Structure/자료구조' 카테고리의 다른 글
  • [자료구조] 배열의 응용 : 희소행렬
  • [자료구조] 배열의 응용 : 다항식
  • [자료구조] 배열을 이용한 리스트 구현
  • [자료구조] 추상 자료형(Abstract Data Type)
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Spring
        • MVC
        • DB
        • 핵심 원리
        • JPA
      • WEB
        • HTML
        • CSS
        • HTTP
        • Application
      • Computer Science
        • Network
        • Database
        • OS
        • 시스템 프로그래밍
        • 컴퓨터구조
      • Algorithm
        • Divide&Conquer
        • Sort
        • Greedy
        • DP
        • Backtracking
        • NP-Complete
        • Graph
      • Data Structure
        • 자료구조
        • C++ STL
        • Java Collection
      • 소프트웨어 공학
        • 시험 공부 정리
        • Theorem
      • Programming Language
        • Python
        • Java
        • C
        • C++
        • Rust
        • Theory
      • Unix_Linux
        • Common
      • React
      • PS
        • BOJ
        • Tip
        • 프로그래머스
        • CodeForce
      • Book Review
        • Clean Code
      • Math
        • Linear Algebra
      • AI
        • DL
        • ML
        • DA
        • Concepts
      • 우아한테크코스
        • 프리코스
      • Project Review
      • LegacyPosts
      • Android
      • Apple
        • Mac
        • IPhone
        • IPad
      • 모니터
      • Diary
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

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

티스토리툴바