์ฐ๊ฒฐ๋ฆฌ์คํธ ์ถ์ ์๋ฃํ
void ListInit(List *plist)
- ์ด๊ธฐํํ ๋ฆฌ์คํธ์ ์ฃผ์ ๊ฐ์ ์ธ์๋ก ์ ๋ฌ ํด์ผํ๋ค.
- ๋ฆฌ์คํธ ์์ฑ ํ ์ ์ผ ๋จผ์ ํธ์ถ๋์ด์ผ ํ๋ ํจ์์ด๋ค.
void LInsert(List *plist, LData data)
- ๋ฆฌ์คํธ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค. ๋งค๊ฐ๋ณ์ data์ ์ ๋ฌ๋ ๊ฐ์ ์ ์ฅํ๋ค.
int LFirst(List *plist)
- ์ฒซ ๋ฒ์งธ ๋ฐ์ดํฐ๊ฐ pdata๊ฐ ๊ฐ๋ฅดํค๋ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๋ค.
- ๋ฐ์ดํฐ์ ์ฐธ์กฐ๋ฅผ ์ํ ์ด๊ธฐํ๊ฐ ์งํ๋๋ค.
- ์ฐธ์กฐ ์ฑ๊ณต ์ 1, ์คํจ์ 0์ ๋ฐํํ๋ค.
int LNext(List *plist)
- ์ฐธ์กฐ๋ ๋ฐ์ดํฐ์ ๋ค์ ๋ฐ์ดํฐ๊ฐ pdata๊ฐ ๊ฐ๋ฅดํค๋ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๋ค.
- ์์ฐจ์ ์ธ ์ฐธ์กฐ๋ฅผ ์ํด์ ๋ฐ๋ณต ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
- ์ฐธ์กฐ๋ฅผ ์๋ก ์์ํ๋ ค๋ฉด ๋จผ์ LData ํจ์๋ฅผ ํธ์ถํด์ผ ํ๋ค.
- ์ฐธ์กฐ ์ฑ๊ณต์ 1, ์คํจ์ 0์ ๋ฐํํ๋ค.
LData LRemove(List *plist)
- LFirst ๋๋ LNext ํจ์์ ๋ง์ง๋ง ๋ฐํ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ค.
int LCount(List *plist)
- ๋ฆฌ์คํธ์ ์ ์ฅ ๋์ด ์๋ ๋ฐ์ดํฐ์ ์๋ฅผ ๋ฐํํ๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ํ์ฉํ์ฌ ๋ค์ํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ตฌํํ ์ ์๊ธฐ์ ๋ค์ ๋จ๊ณ๋ฅผ ์ํ ์ ์ ํ ์ดํด๊ฐ ํ์ํ๋ค!!!.
typedef struct _node
{
LData data;
struct _node *next;
} Node;
์ฌ๊ธฐ์
struct _node *next; ๊ฐ ์๋ฏธํ๋ ๋ฐ๋ ํ์ฌ ๋ ธ๋์ ๋ค์ ๋ ธ๋์ ์ฃผ์๊ฐ์ ์ ์ฅํ๋ค๋ ์๋ฏธ์ด๋ค. ๋ ธ๋๋ฅผ ๋ฐ๊ตฌ๋ next๋ณ์๋ ๋ค์ ๋ ธ๋๋ฅผ ์ก๊ณ ์๋ ์ค ์ ๋๋ก ์ดํดํ๋ฉด ํธํ๋ค.
typedef struct __ArrayList
{
Node *head;
Node *cur;
Node *before;
int numOfData;
} LinkedList;
head๋ ๊ฐ์ฅ ์์ ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํจ๋ค.
void ListInit(List *plist)
{
plist->head = (Node *)malloc(sizeof(Node));
plist->head->next = NULL;
plist->numOfData = 0;
}
์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด head์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ ๋นํ๋ค. ์ ๋๋ฏธ ๋ ธ๋๋ฅผ ๋ง๋๋ ํ๋ฉด ๋๋ฏธ๋ ธ๋๊ฐ ์์ด ์ฝ๋๋ฅผ ๊ตฌ์ฑํ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ๋ ธ๋์ ๋๋ฒ์งธ ๋ ธ๋์ ์ญ์ ์์ ์ฐจ์ด๊ฐ ์๊ฒจ ์ฝ๋ ๊ธธ์ด๊ฐ ๋์ด๋๋ ์ํฉ์ด ์๊ธฐ๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ ๊ธฐ์ ๊ทธ๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ๊ธฐ ์ํด head๊ฐ ๋๋ฏธ ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํค๋๋ก ๊ตฌ์ฑ ํ๋ค.
int LFirst(List *plist, LData *pdata)
{
if (plist->head->next == NULL)
return FALSE;
plist->before = plist->head;
plist->cur = plist->head->next;
*pdata = plist->cur->data;
return TRUE;
}
int LNext(List *plist, LData *pdata)
{
if (plist->cur->next == NULL)
return FALSE;
plist->before = plist->cur;
plist->cur = plist->cur->next;
*pdata = plist->cur->data;
return TRUE;
}
์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด
before๊ฐ cur์ ํญ์ ๋ค์ ๋์ด๋ ๊ฑธ ๋ณผ์ ์๋๋ฐ, ์ญ์ ์ ์ด์ before๋ ธ๋๋ก cur์ด ์ด๋ ํ ์
์๊ฒ ํด์ฃผ๋ ์ญํ ์ํ๋ค.
LData LRemove(List *plist)
{
int Ddata = plist->cur->data;
Node *DNode = plist->cur;
plist->before->next = plist->cur->next;
plist->cur = plist->before;
free(DNode);
(plist->numOfData)--;
return Ddata;
}
์ญ์ ์ ๋ํ ์ฝ๋์ด๋ค. ์ญ์ ์ cur์ด ๋ค์ ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํค๋ ๊ฒ์ด ์๋๋ผ before ์ฆ ์ด์ ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํค๋ ์ด์ ๋ ์์ง ์ฐธ์กฐ๋์ง ์์ ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํค๊ธฐ ๋๋ฌธ์ด๋ค.
์ญ์ ์ (LRemove ํจ์ ํธ์ถ ์ )
์ญ์ ํ(LRemove ํจ์ ํธ์ถ ํ)
๋ ธ๋ ๊ธฐ๋ฐ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ์ฝ๋: https://boiling-fowl-6d6.notion.site/LinkedList-fb60710c9c914575b74d06bd619dd4bf
๋ฐฐ์ด ๊ธฐ๋ฐ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ์ฝ๋: https://boiling-fowl-6d6.notion.site/ArrayList-25e61bb8962c4f53ad4e228489f9912c
'์๋ฃ ๊ตฌ์กฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ๊ฐ๋ (0) | 2023.02.28 |
---|---|
ํธ๋ฆฌ ๊ฐ๋ ๋ฐ ๊ตฌํ (0) | 2023.02.28 |
ํ ๊ฐ๋ (0) | 2023.02.28 |
์คํ ๊ฐ๋ (0) | 2023.02.28 |
์๋ฃ ๊ตฌ์กฐ ์ด ์ ๋ฆฌ (0) | 2023.02.28 |