-> ๋ฐฐ์ด ๋๋ ํฌ์ธํฐ๋ก ํํํ ์ ์์
#include <stdio.h>
#include <stdlib.h>
#define MAX_LIST_SIZE 100
typedef int element;
typedef struct {
element array[MAX_LIST_SIZE];
int size;
}ArrayListType;
//์ค๋ฅ ์ฒ๋ฆฌ ํจ์
void error(char* message) {
fprintf(stderr, "%s\n", message);
}
//๋ฆฌ์คํธ ์ด๊ธฐํ ํจ์
void init(ArrayListType* L) {
L->size = 0;
}
//๊ณต๋ฐฑ ํจ์
int is_empty(ArrayListType* L) {
return L->size = 0;
}
//ํฌํ ํจ์
int is_full(ArrayListType* L) {
return L->size == MAX_LIST_SIZE;
}
//๋ฆฌ์คํธ ์ถ๋ ฅ
void print_list(ArrayListType* L) {
for (int i = 0;i < L->size;i++) {
printf("%d->", L->array[i]);
}
printf("\n");
}
//์ฝ์
ํจ์ (๋ง์ง๋ง์)
void insert_last(ArrayListType* L, element item) {
if (L->size >= MAX_LIST_SIZE) {
error("๋ฆฌ์คํธ ์ค๋ฒํ๋ก์ฐ");
}
L->array[L->size++] = item;
}
//์ฝ์
ํจ์ (์์ผ๋ก ๋น๊ธฐ๊ธฐ)
void insert(ArrayListType* L, int pos, element item) {
if (!is_full(L) && (pos >= 0) && (pos <= L->size)) {
for (int i = (L->size - 1);i >= pos;i--) {
L->array[i + 1] = L->array[i];
}
L->array[pos] = item;
L->size++;
}
}
//์ญ์ ํจ์ (pos์์น์ ๊ฒ)
element delete(ArrayListType* L, int pos) {
element item;
if (pos < 0 || pos >= L->size)
error("์์น ์ค๋ฅ");
item = L->array[pos];
for (int i = pos;i < (L->size - 1);i++) {
L->array[i] = L->array[i + 1];
}
L->size--;
return item;
}
int main(void)
{
ArrayListType list;
init(&list);
insert(&list, 0, 10); print_list(&list);
insert(&list, 0, 20); print_list(&list);
insert(&list, 0, 30); print_list(&list);
insert_last(&list, 40);print_list(&list);
delete(&list, 0); print_list(&list);
}
& ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ์ข
๋ฅ
1. ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ
2. ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ
3. ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ
& ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ์ฐ์ฐ
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode {
element data;
struct ListNode* link;
}ListNode;
//์ค๋ฅ ์ฒ๋ฆฌ ํจ์
void error(char* message) {
fprintf(stderr, "%s\n", message);
}
//insert_first ํจ์
ListNode* insert_first(ListNode* head, int value) {
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
p->data = value;
p->link = head;
head = p;
return head;
}
//insert ํจ์
ListNode* insert(ListNode* head, ListNode* pre, element value) {
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
p->data = value;
p->link = pre->link;
pre->link = p;
return head;
}
//delete_first ํจ์
ListNode* delete_first(ListNode* head) {
ListNode* removed;
if (head == NULL) return NULL;
removed = head;
head = removed->link;
free(removed);
return head;
}
//delete ํจ์
ListNode* delete(ListNode* head, ListNode* pre) {
ListNode* removed;
removed = pre->link;
pre->link = removed->link;
free(removed);
return head;
}
//print_list ํจ์
void print_list(ListNode* head) {
for (ListNode* p = head;p != NULL;p = p->link) {
printf("%d->", p->data);
}
printf("\n");
}
int main(void)
{
ListNode* head = NULL;
for (int i = 0;i < 5;i++) {
head = insert_first(head, i);
print_list(head);
}
for (int i = 0;i < 5;i++) {
head = delete_first(head);
print_list(head);
}
return 0;
}
& ๋จ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๊ตฌํํ๋ ๊ฒ ์ฝ๋ ์ด๋ ค์ ์. ์ฝ์
, ์ญ์ ์์ํ๋ฉด์ ์ฝ๋ ๋ณด๊ธฐ.
๋, ListNode* pํ๋ฉด p์์ฒด๊ฐ ํฌ์ธํฐ์!!!! head๋ ์์ฒด๋ก ํฌ์ธํฐ์!!!! ๊ทธ๊ฑธ ๊ฐ๋ฆฌํค๋ฉด, ์ฝ๋ ํท๊ฐ๋ฆฌ์ง ์์.
๋จ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ์์ ์ฒซ ๋ฒ์งธ์ ์ฝ์
, ์ญ์ / ์ค๊ฐ์ ์ฝ์
, ์ญ์ ํ๋ ๊ฒ ๋ ์ข
๋ฅ ์์์