Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • DFS
  • Network
  • 다이나믹 프로그래밍
  • Kubernetes
  • 백트래킹
  • BFS
  • 프로그래머스
  • boj
  • docker
  • django

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/DS, algorithms

[자료구조] Single Linked List 예제 코드 #2 (C)

2021. 6. 14. 21:16

Single Linked List 구현하기 #2

 

따로 list의 크기를 입력받는 상황은 구현하지 않고 정적으로 이미 만들어진 노드에서 circular list로 만들고 검색하는 기능을 구현했다.

 

구현을 위한 메서드들

  • struct Node : list의 한 노드를 구성하는 구조체
  • struct Node* Create(int data) : list의 새 노드를 만드는 함수
  • struct Node* Insert(struct Node* current, int data) : current 라는 노드 뒤에(after라는 노드 앞에) 노드를 새로 만들고 data값을 설정 후 노드를 반환하는 함수
  • void destroy(struct Node* destroy, struct Node* head) : head 노드부터 탐색해서 destroy 노드를 찾으면 메모리를 할당 해제하고 해제된 노드의 양쪽을 이어주는 함수
  • void Search(struct Node* head, struct Node* search) : search라는 노드가 존재하는지 head 노드부터 찾아보는 함수
  • int Count(struct Node* head) : 연결된 노드의 개수를 반환하는 함수
  • void Circle(struct Node* head) : 마지막 노드가 head를 가리키게 만들어 주는 함수(circular)

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
 
struct Node* Create(int data);
void printNode(struct Node* from);
struct Node* Insert(struct Node* current, int data);
int Count(struct Node* head);
int Search(struct Node* head, struct Node* search);
void Circle(struct Node* head);
 
struct Node{
    int data;
    struct Node* nextNode;
    struct Node* prevNode;
};
 
int main()
{
    struct Node* Node1 = Create(100);
    struct Node* Node2 = Insert(Node1,200);
    struct Node* Node3 = Insert(Node2,300);
    struct Node* Node4 = Insert(Node2,250);
 
    printNode(Node1);
    printf("노드는 %d개\n",Count(Node1));
    Search(Node1, Node4);
    Circle(Node1); 
    printf("%d",(Node1->prevNode)->data);
    
    return 0;
}
 
void printNode(struct Node* from){
    int i = 1;
    while(from){
        printf("%d번째 노드 : %d\n",i,from->data);
        i++;
        from = from->nextNode;
    }
}
/* 새 노드를 만드는 함수 */
struct Node* Create(int data){
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    
    newNode->data = data;
    newNode->nextNode = '\0';
    newNode->prevNode = NULL;
    
    return newNode;
}
/* current 라는 노드 뒤에(after라는 노드 앞에) 노드를 새로 만들어 넣는 함수 */
struct Node* Insert(struct Node* current, int data){
    
    struct Node* after = current->nextNode;
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    
    newNode->data = data;
    newNode->nextNode = after;
    newNode->prevNode = current;
    current->nextNode = newNode;
    if(after!=NULL){
        after->prevNode = newNode;
    }
    
    return newNode;
}
/* 선택된 노드를 파괴하는 함수 */
void destroy(struct Node* destroy, struct Node* head){
    
    struct Node* next = head;
    
    if(destroy == head){
        free(destroy);
        return;
    }
    
    while(next){
        if(next->nextNode == destroy){
            next->nextNode = destroy->nextNode;
        }
        next = next->nextNode;
    }
    free(destroy);    
}
 
int Count(struct Node* head){
    int i=1;
    while(head->nextNode != NULL){
        i++;
        head = head->nextNode;
    }
    return i;
}
 
int Search(struct Node* head, struct Node* search){
    if(head==search){
        printf("head가 search입니다. %d\n",search->data);
    }
    while(head){
        if(head == search){
            printf("찾았습니다. %d\n",head->data);
            break;
        }
        else{
            head=head->nextNode;
            continue;
        }
    }
}
 
// 마지막 노드가 HEAD를 가리키게 만들어 준다.
void Circle(struct Node* head){
    struct Node* temp = head;
    while(temp->nextNode != NULL){
        temp = temp->nextNode;
    }
    //temp는 마지막 노드를 가리키게 된다. 
    struct Node* tail = temp;
    tail->nextNode = head;
    head->prevNode = tail;
}
 
Colored by Color Scripter
cs

'Algorithm > DS, algorithms' 카테고리의 다른 글

오일러 서킷  (0) 2021.06.23
[자료구조] Single Linked List 예제 코드#1 (C)  (0) 2021.06.14
힙 정렬(heap sort) <priority queue> C++  (0) 2021.05.29
Minimum Spanning Tree (MST) - Kruskal  (0) 2021.05.10
Stack Permutation C++  (0) 2021.04.19
    'Algorithm/DS, algorithms' 카테고리의 다른 글
    • 오일러 서킷
    • [자료구조] Single Linked List 예제 코드#1 (C)
    • 힙 정렬(heap sort) <priority queue> C++
    • Minimum Spanning Tree (MST) - Kruskal

    티스토리툴바