[์ž๋ฃŒ๊ตฌ์กฐ] Linked List ๐ŸŽˆ

LEESEUNGYEOLยท2022๋…„ 1์›” 20์ผ
0
post-thumbnail

Linked List(์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ)

List๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•

Array

  • ๊ตฌํ˜„์ด ์‰ฝ๊ณ  ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค.
  • random access๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ๊ฒฝ์šฐ ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ฒจ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.-reallocation ํ•„์š”
  • ๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—ฐ์†์ ์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

Linked List

  • ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์˜ ์ด๋™์—†์ด ์ค‘๊ฐ„์— ์‚ฝ์ž…์ด๋‚˜ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ,
  • ํฌ๊ธฐ์˜ ์ œํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.
  • random access๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜์—ฌ ํ•„์š”ํ•œ ๋…ธ๋“œ์— ์ ‘๊ทผ์ด ๋ถˆํŽธํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—ฐ์†์ ์œผ๋กœ ์ €์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ ์—ฐ๊ฒฐ๋œ ๋‹ค์Œ ์ฃผ์†Œ๊ฐ€ ํ•จ๊ป˜ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค

๋ฉ”๋ชจ๋ฆฌ ์ €์žฅ ๋ฐฉ์‹์˜ ์ฐจ์ด

  • Array๋Š” ๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—ฐ์†์ ์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • Linked List๋Š” ๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—ฐ์†์ ์œผ๋กœ ์ €์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Linked List ๊ตฌํ˜„ํ•˜๊ธฐ

  • ๋ฉ”์ธ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์˜ ๋ฐ์ดํ„ฐ๋“ค์€ ํฉ์–ด์ ธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ž๋ฃŒ์—์„œ ๋‹ค์Œ ์ž๋ฃŒ๋ฅผ ์‰ฝ๊ฒŒ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ ํ•„๋“œ์™€ ํฌ์ธํ„ฐ์ธ ๋งํฌ ํ•„๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
   int data;
   struct node* next; //์ž๊ธฐ ์ฐธ์กฐ ๊ตฌ์กฐ์ฒด
} NODE;

create

  • ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋…ธ๋“œ์˜ ๋นˆ ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค
NODE* create(int data) {
	NODE* new_node = (NODE*)malloc(sizeof(NODE));
	new_node->next = NULL;
	new_node->data = data;
}

insert

  • ๋จผ์ € ์ ‘๊ทผ์„ ์ƒ๊ฐํ•˜๊ณ  ์‚ฝ์ž…์„ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ
  • ๊ฐ€์žฅ ์•ž์— ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ
  • ๊ฐ€์žฅ ๋’ค์— ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ
  • ์ค‘๊ฐ„์— ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ
//ํ•ด๋‹น ํ•จ์ˆ˜๋Š” data ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๋“ค์— ์ ์ ˆํ•œ ์œ„์น˜์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
//๋ฐ์ดํ„ฐ๋ฅผ ๋“ค์–ด๋†“์€ data ๋งŒ๋“ค๊ธฐ
	//๋…ธ๋“œ ์ค‘๊ฐ„ ์‚ฝ์ž…์„ ํ•  ๊ฒฝ์šฐ ์ปค์„œ ๋‘๊ฐœ ๋งŒ๋“œ๋Š” ์ด์œ 
	//๊ณผ์ • -> ์‚ฝ์ž… ์ด์ „๋…ธ๋“œ์˜ ์œ„์น˜์™€ ์‚ฝ์ž… ์ดํ›„ ๊ทธ๋‹ค์Œ ๋…ธ๋“œ์˜ ์œ„์น˜๋„ ์•Œ์•„์•ผ ํ•œ๋‹ค
	//1.์‚ฝ์ž…ํ•˜๋ ค๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ƒ์„ฑ
	//2.์‚ฝ์ž…ํ•˜๋ ค๋Š” ์ด์ „ ๋…ธ๋“œ๊นŒ์ง€ ์ด๋™
	//3.์‚ฝ์ž… ๋…ธ๋“œ์˜ link๋ฅผ ์ด์ „ ๋…ธ๋“œ์˜ link๋กœ ์ €์žฅ
	//4.์ด์ „ ๋…ธ๋“œ์˜ link๋ฅผ ์‚ฝ์ž… ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋กœ ์ €์žฅ
NODE* insert(NODE* head, int data) {
	NODE* node, * p, * q;
	node = create(data);
	
	p = head;
	q = head;
	//์ปค์„œ =์›ํ•˜๋Š” ์œ„์น˜๋กœ ์˜ฎ๊ฒจ์ฃผ๊ธฐ ์ˆœ์„œ: q new data p
	while (p != NULL) {
		//p data๊ฐ€ ์ฒ˜์Œ์œผ๋กœ data๋ณด๋‹ค ํฌ๋ฉด ํƒˆ์ถœ
		if (p->data > data) { //์ ์ ˆํ•œ ์œ„์น˜ ์ฐพ๊ธฐ
			break;
		}
		//ํ•œ์นธ์”ฉ ์ด๋™
		q = p;
		p = p->next;
	}
	//p๊ฐ€ ๋งจ ์•ž์— ์žˆ๋‹ค๋ฉด ์ฆ‰ head ์•ž์— ์žˆ๋‹ค๋ฉด
	//๋งŒ์•ฝ head์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค ํ•ด๋„ ์ด if๋ฌธ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค
	if (p == head) {
		node->next = head;//node๊ฐ€ head๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•œ๋‹ค
		head = node;//head๋Š” ๋งจ์•ž์— ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋–„๋ฌธ์— head๋ฅผ ๋งจ ์•ž์œผ๋กœ ์ด๋™
	}
	//์ œ์ผ ์•ž์— ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด
	else {
		node->next = p;
		q->next = node;
	}
	return head;
}

delete

  • ๋จผ์ € ์ ‘๊ทผ์„ ํ•˜๊ณ  ์‚ญ์ œ๋ฅผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ
  • ๊ฐ€์žฅ ์ฒซ๋ฒˆ์จฐ์— ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ
  • ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ
  • ์ค‘๊ฐ„ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ
  • ์‚ญ์ œํ•  ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
//ํ•ด๋‹น ํ•จ์ˆ˜๋Š” data ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
NODE* deleteNode(NODE* head, int data) {
	NODE* p, * q;
	p = head;
	q = head;
	//์ ‘๊ทผ
	while (p != NULL) {
		if (p->data == data) { //์‚ญ์ œํ•  ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ๊ฒฝ์šฐ
			break;
		}
		q = p;
		p = p->next;
	}
	if (p == NULL) { //์ปค์„œ๊ฐ€ ๋๊นŒ์ง€ ์ด๋™ํ•œ ๊ฒƒ์€ ์‚ญ์ œํ•  ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
		return head;
	}
	else if (p == head) { //์‚ญ์ œ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์žฅ ์•ž์— ์žˆ์„ ๊ฒฝ์šฐ
		head = head->next;
		free(p);
		return head;
	}
	else { // ์‚ญ์ œ ๋…ธ๋“œ๊ฐ€ ์ค‘๊ฐ„์— ์žˆ๋Š” ๊ฒฝ์šฐ
		q->next = p->next; //์‚ญ์ œ๋…ธ๋“œ ์ด์ „๋…ธ๋“œ์™€ ์‚ญ์ œ๋…ธ๋“œ ๋‹ค์Œ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
		free(p);
		return head;
	}
}

์‚ฌ์ง„ ์ถœ์ฒ˜
http://dorson23.blogspot.com/2018/02/c-1-linkedlist.html
https://server-engineer.tistory.com/130

profile
๐Ÿข

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