구조체
<aside>
// 단순 연결 리스트의 노드 구조를 구조체로 정의
typedef struct _LISTNODE_ {
char data[MAXSTRLEN];
struct _LISTNODE_* next;
} LISTNODE;
// 리스트의 시작을 나타내는 head 노드를 구조체로 정의
typedef struct _LINKEDLIST_ {
LISTNODE* head;
int numOfData;
} LINKEDLIST;
구조
<aside>
LINKEDLIST (myList)
+---------------------------+
| head ------------------+ |
| numOfData: 3 | |
+------------------------|--+
|
v
+------------------------+
| LISTNODE (Node1) |
|------------------------|
| data: "Apple" |
| next ----------------+ |
+---------------------|--+
|
v
+------------------------+
| LISTNODE (Node2) |
|------------------------|
| data: "Banana" |
| next ----------------+ |
+---------------------|--+
|
v
+------------------------+
| LISTNODE (Node3) |
|------------------------|
| data: "Cherry" |
| next: NULL |
+------------------------+
</aside>
</aside>
InsertFirst
<aside>
void InsertFirst(LINKEDLIST* pList, char* x)
{
LISTNODE* newNode = NULL;
newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
strcpy_s(newNode->data, strlen(x) + 1, x);
newNode->next = NULL;
newNode->next = pList->head->next;
pList->head->next = newNode;
pList->numOfData += 1; // 데이터 수 증가
}
구조
<aside>
1. 초기 상태
pList->head -> [A] -> [B] -> [C] -> NULL
2. nextNode->next = pList->head->next;
newNode -> [A] -> [B] -> [C] -> NULL
3. pList->head->next = newNode;
pList->head
|
v
[newNode: "X"] -> [A] -> [B] -> [C] -> NULL
</aside>
</aside>
InsertLast
<aside>
void InsertLast(LINKEDLIST* pList, char* x)
{
LISTNODE* pre = NULL;
LISTNODE* cur = NULL;
LISTNODE* newNode = NULL;
newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
strcpy_s(newNode->data, strlen(x) + 1, x);
newNode->next = NULL;
pre = pList->head;
cur = pList->head->next; // head는 초기화 과정에서 dummy로 만들어주었으므로, head->next 부터 순회
while (cur != NULL)
{
pre = cur;
cur = cur->next;
}
//newNode->next = NULL; // NULL임
newNode->next = pre->next; // NULL 임
pre->next = newNode;
pList->numOfData += 1; // 데이터 수 증가
}
구조
SearchNode
<aside>
LISTNODE* SearchNode(LINKEDLIST* pList, char* x)
{
LISTNODE* cur = NULL;
cur = pList->head->next;
while (cur != NULL)
{
if (strncmp(cur->data, x, MAXSTRLEN) == 0)
{
return cur;
}
cur = cur->next;
}
return cur;
}
</aside>
DeleteNode