[๐Ÿ“—c์–ธ์–ด ์‰ฝ๊ฒŒ ํ’€์–ด์“ด ์ž๋ฃŒ๊ตฌ์กฐ6] ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ1

์•ˆ์ง€์ˆ˜ยท2023๋…„ 2์›” 9์ผ
0

๐Ÿ‘‘ ๋ฆฌ์ŠคํŠธ ์ถ”์ƒ ๋ฐ์ดํ„ฐ ํƒ€์ž…

  • ์ง‘ํ•ฉ๊ณผ ๋‹ฌ๋ฆฌ ํ•ญ๋ชฉ ๊ฐ„์— ์ˆœ์„œ๊ฐ€ ์žˆ์Œ (์ง‘ํ•ฉ์€ ์—†์Œ)
  • insert(): ์‚ฝ์ž…
  • delete(): ์‚ญ์ œ

-> ๋ฐฐ์—ด ๋˜๋Š” ํฌ์ธํ„ฐ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Œ

๐Ÿ‘‘ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋œ ๋ฆฌ์ŠคํŠธ (์ˆœ์ฐจ์  ํ‘œํ˜„)

  • ์žฅ์ : ๊ตฌํ˜„์ด ์‰ฌ์›€, ์ž„์˜์˜ ํ•ญ๋ชฉ ์ถ”์ถœ ์‰ฌ์›€
  • ๋‹จ์ : ์ค‘๊ฐ„์— ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ์–ด๋ ค์›€, ํฌ๊ธฐ๊ฐ€ ์ œํ•œ๋จ
  • insert(): ์ค‘๊ฐ„ ์‚ฝ์ž…, pos์š”์†Œ๋ถ€ํ„ฐ ๋’ค๋กœ ์ด๋™ ํ›„ ์‚ฝ์ž…
#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);
}

๐Ÿ‘‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

  • ์žฅ์ : ๊ณต๊ฐ„ ์ œํ•œ ์—†์Œ (๋™์  ํ• ๋‹น), ์ค‘๊ฐ„ ์‚ฝ์ž… ์‚ญ์ œ ์–ด๋ ค์›€
  • ๋‹จ์ : ๊ตฌํ˜„ ์–ด๋ ต
  • ํ—ค๋“œ ์ค‘์š”!!! (์ฒซ ๋…ธ๋“œ)
  • ํฌ์ธํ„ฐ๋กœ ๊ตฌํ˜„ (data, link ํ•„๋“œ๋กœ ๊ตฌ์„ฑ)

& ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์ข…๋ฅ˜
1. ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
2. ์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
3. ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

๐Ÿ‘‘ ๋‹จ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

  • ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ NULL ๊ฐ€๋ฆฌํ‚ด (๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ link field ๊ฐ’: NULL)
  • ๋…ธ๋“œ ์ƒ์„ฑ: malloc
  • ๋…ธ๋“œ ์‚ญ์ œ: free

& ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์—ฐ์‚ฐ

  • insert_first(): ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ํ•ญ๋ชฉ ์‚ฝ์ž…
  • insert(): ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„ ๋ถ€๋ถ„์— ํ•ญ๋ชฉ ์‚ฝ์ž…
  • delete_first(): ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ ์‚ญ์ œ
  • delete(): ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„ ํ•ญ๋ชฉ ์‚ญ์ œ
  • print_list(): ๋ฆฌ์ŠคํŠธ ๋ฐฉ๋ฌธํ•˜์—ฌ ๋ชจ๋“  ํ•ญ๋ชฉ ์ถœ๋ ฅ
#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;
}

โญ• TIL (Today I learned)

& ๋‹จ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ ์ฝ”๋“œ ์–ด๋ ค์› ์Œ. ์‚ฝ์ž…, ์‚ญ์ œ ์ƒ์ƒํ•˜๋ฉด์„œ ์ฝ”๋“œ ๋ณด๊ธฐ.
๋˜, ListNode* pํ•˜๋ฉด p์ž์ฒด๊ฐ€ ํฌ์ธํ„ฐ์ž„!!!! head๋„ ์ž์ฒด๋กœ ํฌ์ธํ„ฐ์ž„!!!! ๊ทธ๊ฑธ ๊ฐ€๋ฆฌํ‚ค๋ฉด, ์ฝ”๋“œ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š์Œ.
๋‹จ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ์ฒซ ๋ฒˆ์งธ์— ์‚ฝ์ž…, ์‚ญ์ œ/ ์ค‘๊ฐ„์— ์‚ฝ์ž…, ์‚ญ์ œ ํ•˜๋Š” ๊ฒƒ ๋‘ ์ข…๋ฅ˜ ์žˆ์—ˆ์Œ

profile
์ง€์ˆ˜์˜ ์ทจ์ค€, ๊ฐœ๋ฐœ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€