: ๋ง์ง๋ง ๋ ธ๋์ ๋งํฌ๊ฐ ์ฒซ ๋ฒ์งธ ๋ ธ๋ ์ฃผ์ ๊ฐ๋ฆฌํด
#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;
}
#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;
}
#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;
}
#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;
}
& ์ด์ค ์ฐ๊ฒฐ๋ฆฌ์คํธ์์ llink์ rlink๋ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ์! ํน์ ๋งํฌ๊ฐ ์๋๋ผ!!!
์ด์ค ์ฐ๊ฒฐ๋ฆฌ์คํธ์์ ์ฝ์
์์ ์ ๊ธฐ์ตํ๊ธฐ!!! (1-2-3-4)
์ฐ๊ฒฐ๋ฆฌ์คํธ์์ ์ฝ์
ํ ๋์๋ ๋ณดํต ์ ์ฐ๊ฒฐ ํ ์ด๋ (ํฌ์ธํฐ์ ์ด๋)!!
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ์ด์จํผ ๋ฐฉํฅ์ ๋ค '->' ์ชฝ์ด๋ผ๊ณ ์์ํ๋ฉด์ ์ฝ๋ ๋ถ์ํ๊ธฐ!! ์คํ๊ณผ ํ์์๋ ๋ฐฐ์ด๊ณผ์ ์ฐจ์ด์ ์ ์๊ฐํ๋ฉด์!