자료구조: 연결리스트(Linked List) 1

ROK·2022년 10월 6일
0

열혈 자료구조

목록 보기
3/30

연결리스트 1


리스트 자료구조 ADT

  • void ListInit(List * plist);
    • 초기화할 리스트의 주소 값을 인자로 전달
    • 리스트 생성 후 제일 먼저 호출해야함
  • void LInsert(List * plist, LData data);
    • 리스트에 데이터를 저장, 매개변수 data로 전달된 값 저장
  • int LFirst(List plist, LData pdata);
    • 첫 번째 데이터가 pdata가 가르키는 메모리에 저장
    • 데이터의 참조를 위한 초기화 진행
    • 참조 성공 시 TRUE(1), 실패 시 FALSE(0) return
  • int LNext(List plist, LData pdata);
    • 참조된 데이터의 다음 데이터가 pdata가 가르키는 메모리에 저장
    • 순차적인 참조를 위해 반복 호출 가능
    • 참조를 새로 시작하려면 먼저 LFirst 함수를 호출해야함
    • 참조 성공 시 TRUE(1), 실패 시 FALSE(0) return
  • LData LRemove(List * plist);
    • LFirst or LNext 함수가 반환한 데이터 삭제
    • 삭제된 데이터를 반환
    • 마지막 반환 데이터를 삭제하기 때문에 반복 호출 허용 안함
  • int LCount(List * plist);
    • 리스트에 저장되어 있는 데이터 수 반환

헤더파일 : ArrayList.h

#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__

#define TRUE 1				// '참'을 표현하기 위한 정의
#define FALSE 0				// '거짓'을 표현하기 위한 정의

#define LIST_LEN 100		

typedef int LData;			// LData에 대한 typedef 선언	

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 <stdlib.h>
#include "ArrayList.h"

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

void LInsert(List *plist, LData data)
{
   if (plist->numOfData >= LIST_LEN)
   {
      puts("저장 불가");
      return;
   }

   plist->arr[plist->numOfData] = data;
   (plist->numOfData)++;
}

int LFirst(List *plist, LData *pdata)
{
   if (plist->numOfData == 0) 			// 데이터가 없으면 FALSE
   {
      return FALSE;
   }

   (plist->curPosition) = 0;			// 데이터 위치 0으로 초기화
   *pdata = plist->arr[0];				// 인덱스 0번 데이터
   return TRUE;
}

int LNext(List *plist, LData *pdata)
{
   if (plist->curPosition >= (plist->numOfData) - 1)	// 데이터 위치가 배열을 넘어가면 FALSE
   {
      return FALSE;
   }

   (plist->curPosition)++;								
   *pdata = plist->arr[plist->curPosition];				// 해당위치 데이터 
   return TRUE;
}

LData LRemove(List *plist)
{
   int rpos = plist->curPosition;
   int num = plist->numOfData;
   int i;
   LData rdata = plist->arr[rpos];

   for (i = rpos; i < num - 1; i++)
   {
      plist->arr[i] = plist->arr[i + 1];				// 데이터 앞으로 하나씩 이동
   }

   (plist->numOfData)--;								// 삭제했으니 개수 감소
   (plist->curPosition)--;								// 삭제 전으로 위치 변경
   return rdata;
}

int LCount(List *plist)
{
   return plist->numOfData;
}

메인파일 : ListMain.c

#include <stdio.h>
#include "ArrayList.h"

int main()
{
   List list;
   int data;
   ListInit(&list);

   LInsert(&list, 11);
   LInsert(&list, 11);
   LInsert(&list, 22);
   LInsert(&list, 22);
   LInsert(&list, 33);

   printf("현재 데이터 수 : %d ", LCount(&list));

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

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

   // 숫자 22 모두 삭제
   if (LFirst(&list, &data))
   {
      if (data == 22)
      {
         LRemove(&list);
      }

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

   printf("현재 데이터 수 : %d ", LCount(&list));

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

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

   return 0;
}

결과값

현재 데이터 수 : 5 11 11 22 22 33
현재 데이터 수 : 3 11 11 33
profile
하루에 집중하자

0개의 댓글