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

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

๐Ÿ‘‘ ์›ํ˜• ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

: ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋งํฌ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ ์ฃผ์†Œ ๊ฐ€๋ฆฌํ‚ด

  • head๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ!!!
  • ์ฒ˜์Œ์— ์‚ฝ์ž…, ๋งˆ์ง€๋ง‰์— ์‚ฝ์ž… 2๊ฐ€์ง€
#include <stdio.h>
#include <stdlib.h>

typedef int element;
typedef struct ListNode {
	element data;
	struct ListNode* link;
}ListNode;

//์ถœ๋ ฅ ํ•จ์ˆ˜
void print_list(ListNode* head) {
	ListNode* p;
	if (head == NULL) return;
	p = head->link;
	do {
		printf("%d->", p->data);
		p = p->link;
	} while (p!=head);
	printf("%d->", p->data);
}

//์ฒ˜์Œ ์‚ฝ์ž… ํ•จ์ˆ˜
ListNode* insert_first(ListNode* head, element data) {
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = data;
	if (head == NULL) { //๊ณต๋ฐฑ์ƒํƒœ์ด๋ฉด, head๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด ๊ทธ ๋…ธ๋“œ๊ฐ€ ๋˜๊ฒŒ
		head = node;
		node->link = head;
	}
	else {
		node->link = head->link;
		head->link = node;
	}
	return head;
}

//๋งˆ์ง€๋ง‰ ์‚ฝ์ž… ํ•จ์ˆ˜
ListNode* insert_last(ListNode* head, element data) {
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = data;
	if (head == NULL) {
		head = node;
		node -> link = head;
	}
	else {
		node->link = head->link;
		head->link = node;
		head = node;
	}
	return head;
}

int main(void)
{
	ListNode* head = NULL;
	
	head = insert_last(head, 20);
	head = insert_last(head, 30);
	head = insert_last(head, 40);
	head = insert_first(head, 10);
	print_list(head);

	return 0;
}

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

  • ์„ ํ–‰๋…ธ๋“œ์™€ ํ›„์†๋…ธ๋“œ ์กด์žฌ(llink, rlink)
  • ์‚ฌ์šฉ ์ „์— ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•จ
  • ํ—ค๋“œ ๋…ธ๋“œ ์กด์žฌ (head ํฌ์ธํ„ฐ์™€๋Š” ๋‹ค๋ฆ„. ์•„๋ฌด ๊ฐ’ ์—†๋Š” ๋…ธ๋“œ)
  • llink: ๋ฐ”๋กœ ์„ ํ–‰ ๋…ธ๋“œ ๊ฐ€๋ฆฌํ‚ด, rlink: ๋ฐ”๋กœ ํ›„์† ๋…ธ๋“œ ๊ฐ€๋ฆฌํ‚ด
  • ์ค‘๊ฐ„ ์‚ฝ์ž… ์‹œ, ์ฝ”๋“œ ์ˆœ์„œ: โ†– -> โ†— -> โ†™ -> โ†˜
  • ์‚ญ์ œ๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ ๊ฒƒ๋ถ€ํ„ฐ ์‚ญ์ œ๋จ!!
#include <stdio.h>
#include <stdlib.h>

typedef int element;
typedef struct DListNode {
	element data;
	struct DListNode* llink;
	struct DListNode* rlink;
}DListNode;

//์ดˆ๊ธฐํ™” ํ•จ์ˆ˜
void init(DListNode* phead) {
	phead->llink = phead;
	phead->rlink = phead;
}

//๋…ธ๋“œ ์ถœ๋ ฅ ํ•จ์ˆ˜
void print_dlist(DListNode* phead) {
	DListNode* p;
	for (p = phead->rlink;p != phead;p = p->rlink) {
		printf("<-| | %d| |-> ", p->data);
	}
	printf("\n");
}

//before์˜ ์˜ค๋ฅธ์ชฝ์— ์‚ฝ์ž…ํ•˜๋Š” ํ•จ์ˆ˜
void dinsert(DListNode* before, element data) {
	DListNode* newnode = (DListNode*)malloc(sizeof(DListNode));
	newnode->data = data;
	newnode->llink = before;
	newnode->rlink = before->rlink;
	before->rlink->llink = newnode;
	before->rlink = newnode;
}

//๋…ธ๋“œ๋ฅผ removeํ•˜๋Š” ํ•จ์ˆ˜ 
void ddelete(DListNode* head, DListNode* removed) {
	if (removed == head) return;
	removed->llink->rlink = removed->rlink;
	removed->rlink->llink = removed->llink;
	free(removed);
}

int main(void)
{
	DListNode* head = (DListNode*)malloc(sizeof(DListNode));
	init(head);
	printf("์ถ”๊ฐ€ ๋‹จ๊ณ„\n");
	for (int i = 0;i < 5;i++) {
		dinsert(head, i);
		print_dlist(head);
	}
	printf("์‚ญ์ œ ๋‹จ๊ณ„\n");
	for (int i = 0;i < 5;i++) {
		print_dlist(head);
		ddelete(head, head->rlink);
	}
	free(head);

	return 0;
}

๐Ÿ‘‘ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ์Šคํƒ

  • ๋‹จ์ˆœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ๋งจ ์•ž์— ์š”์†Œ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Œ
  • head ํฌ์ธํ„ฐ ๋Œ€์‹  top ํฌ์ธํ„ฐ
  • ์ดˆ๊ธฐํ™”: top==NULL (๋ฐฐ์—ด์—์„œ๋Š” top==-1)
  • ๊ณต๋ฐฑ ์ƒํƒœ (top์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด NULL, ํฌํ™”๋Š” ์•ˆ๋จ-๋™์ ํ• ๋‹น)
#include <stdio.h>
#include <malloc.h>

typedef int element;
typedef struct StackNode {
	element data;
	struct StackNode* link;
}StackNode;

typedef struct {
	StackNode* top;
}LinkedStackType;

//์ดˆ๊ธฐํ™” ํ•จ์ˆ˜
void init(LinkedStackType* s) {
	s->top == NULL;
}

//๊ณต๋ฐฑ ์ƒํƒœ ํ•จ์ˆ˜ (top์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด NULL์ธ์ง€)
int is_empty(LinkedStackType* s) {  
	return (s->top == NULL);
}

//ํฌํ™” ์ƒํƒœ ํ•จ์ˆ˜ (ํฌํ™”์ƒํƒœ๋Š” ๊ฑฐ์˜ ์—†์Œ. ๋ฉ”๋ชจ๋ฆฌ ๋™์  ํ• ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—)
int is_full(LinkedStackType*s) {
	return 0;  //ํ•ญ์ƒ ํฌํ™”์ƒํƒœ ์•„๋‹˜
}

//์‚ฝ์ž… ํ•จ์ˆ˜
void push(LinkedStackType* s, element item) {
	StackNode* temp = (StackNode*)malloc(sizeof(StackNode));
	temp->data = item;
	temp->link = s->top;
	s->top = temp;
}

//์ถœ๋ ฅ ํ•จ์ˆ˜
void print_stack(LinkedStackType* s) {
	for (StackNode* p = s->top;p != NULL;p = p->link) {
		printf("%d->", p->data);
	}
	printf("NULL \n");
}

//์‚ญ์ œ ํ•จ์ˆ˜
element pop(LinkedStackType* s) {
	if (is_empty(s)) {
		fprintf(stderr, "์Šคํƒ์ด ๋น„์–ด์žˆ์Œ\n");
		exit(1);
	}
	else {
		StackNode* temp = s->top;
		int data = temp->data;
		s->top = s->top->link;
		free(temp);
		return data;
	}
}

//ํ”ผํฌ ํ•จ์ˆ˜
element peek(LinkedStackType* s) {
	if (is_empty(s)) {
		fprintf(stderr, "์Šคํƒ์ด ๋น„์–ด์žˆ์Œ\n");
		exit(1);
	}
	else {
		return s->top->data;
	}
}

int main(void)
{
	LinkedStackType* s;
	init(&s);
	push(&s, 1); print_stack(&s);
	push(&s, 2); print_stack(&s);
	push(&s, 3); print_stack(&s);
	pop(&s); print_list(&s);
	pop(&s); print_list(&s);
	pop(&s); print_list(&s);

	return 0;
}

๐Ÿ‘‘ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ํ

  • ์‚ฝ์ž… (๋งจ ๋’ค rear), ์‚ญ์ œ (๋งจ ์•ž front)
  • ์ดˆ๊ธฐํ™”: front์™€ rear์ด 0 (์„ ํ˜• ํ ๋ฐฐ์—ด์—์„œ๋Š” -1)
  • ๊ณต๋ฐฑ ์ƒํƒœ: front์ด NULL (์„ ํ˜• ํ ๋ฐฐ์—ด์—์„œ๋Š” front==rear์ธ ๊ฒฝ์šฐ)
#include <stdio.h>
#include <stdlib.h>

typedef int element;
typedef struct QueueNode {
	element data;
	struct QueueNode* link;
}QueueNode;

typedef struct {
	QueueNode* front, *rear;
}LinkedQueueType;

//์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ (front์™€ rear์ด NULL)
void init(LinkedQueueType* q) {
	q->front = q->rear = 0;
}

//๊ณต๋ฐฑ ์ƒํƒœ ํ•จ์ˆ˜
int is_empty(LinkedQueueType* q) {
	return (q->front = NULL);
}

//ํฌํ™” ์ƒํƒœ ํ•จ์ˆ˜
int is_full(LinkedQueueType* q) {
	return 0;
}

//์‚ฝ์ž… ํ•จ์ˆ˜
void enqueue(LinkedQueueType* q, element data) {
	QueueNode* temp = (QueueNode*)malloc(sizeof(QueueNode));
	temp->data = data;
	temp->link = NULL;
	if (is_empty(q)) {
		q->front = temp;
		q->rear = temp;
	}
	else {
		q->rear->link = temp;
		q->rear = temp;
	}
}

//์‚ญ์ œ ํ•จ์ˆ˜
element dequeue(LinkedQueueType* q) {
	QueueNode* temp = q->front;
	element data;
	if (is_empty(q)) {
		fprintf(stderr, "์Šคํƒ์ด ๋น„์–ด์žˆ์Œ\n");
		exit(1);
	}
	else {
		data = temp->data;
		q->front = q->front->link;
		if (q->front == NULL) { //๋งˆ์ง€๋ง‰ ๋‚จ์€ ๋…ธ๋“œ ์‚ญ์ œ ํ–ˆ์„ ๊ฒฝ์šฐ(front์™€ rear๋ชจ๋‘ NULL)
			q->rear = NULL;
		}
		free(temp);
		return data;
	}
}

//์ถœ๋ ฅ ํ•จ์ˆ˜
void print_queue(LinkedQueueType* q) {
	QueueNode* p;
	for (p = q->front;p != NULL;p = p->link) {
		printf("%d->", p->data);
	}
	printf("NULL \n");
}

int main(void)
{
	LinkedQueueType queue;
	init(&queue);

	enqueue(&queue, 1); print_queue(&queue);
	enqueue(&queue, 2); print_queue(&queue);
	enqueue(&queue, 3); print_queue(&queue);
	dequeue(&queue); print_queue(&queue);
	dequeue(&queue); print_queue(&queue);
	dequeue(&queue); print_queue(&queue);
	return 0;
}

โญ• TIL (Today I Learned)

& ์ด์ค‘ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ llink์™€ rlink๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ž„! ํŠน์ • ๋งํฌ๊ฐ€ ์•„๋‹ˆ๋ผ!!!
์ด์ค‘ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฝ์ž… ์ˆœ์„œ ์ž˜ ๊ธฐ์–ตํ•˜๊ธฐ!!! (1-2-3-4)
์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฝ์ž… ํ•  ๋•Œ์—๋Š” ๋ณดํ†ต ์„ ์—ฐ๊ฒฐ ํ›„ ์ด๋™ (ํฌ์ธํ„ฐ์˜ ์ด๋™)!!

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” ์–ด์จŒํŠผ ๋ฐฉํ–ฅ์€ ๋‹ค '->' ์ชฝ์ด๋ผ๊ณ  ์ƒ์ƒํ•˜๋ฉด์„œ ์ฝ”๋“œ ๋ถ„์„ํ•˜๊ธฐ!! ์Šคํƒ๊ณผ ํ์—์„œ๋Š” ๋ฐฐ์—ด๊ณผ์˜ ์ฐจ์ด์ ์„ ์ƒ๊ฐํ•˜๋ฉด์„œ!

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

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