티스토리 뷰

개인 서재..*/알고리즘

[C언어] Singly Linked List

낭만붕어빵 2017. 4. 14. 03:33
반응형

1. LinkedList.h


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
//링크드 리스트의 기본 연산을 수행하는 함수가 선언되어있는 LinkedList.h// 
 
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
 
#include <stdio.h>
#include <stdlib.h>
 
typedef int ElementType;
 
 
typedef struct tagNode{
    ElementType Data;
    struct tagNode* NextNode;
}Node;
 
/*함수원형 선언*/
Node* SLL_CreateNode(ElementType NewData); //노드생성
void SLL_DestroyNode(Node* Node);//노드소멸
void SLL_DestroyAllNodes(Node** Head);
void SLL_AppendNode(Node** Head,Node* NewNode);//노드추가 
void SLL_InsertAfter(Node* Current,Node* NewNode);//노드삽입
void SLL_InsertBefore(Node** Head,Node* Current,Node* NewNode);
void SLL_InsertNewHead(Node** Head,Node* NewHead);
void SLL_RemoveNode(Node** Head,Node* Remove);//노드삭제
Node* SLL_GetNodeAt(Node* Head,int Location);
int SLL_GetNodeCount(Node* Head);
 
 
#endif
cs


2. LinkedList.c


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
//헤더파일에 있는 함수들을 구현하는 .c 파일// 
 
#include "LinkedList.h"
 
/*노드 생성*/
Node* SLL_CreateNode(ElementType NewData){
    Node* NewNode=(Node*)malloc(sizeof(Node));
    NewNode->Data=NewData;
    NewNode->NextNode=NULL;
 
    return NewNode;
}
 
/*노드 소멸*/
void SLL_DestroyNode(Node* Node){
    free(Node);
}
 
/*노드 추가*/
void SLL_AppendNode(Node** Head,Node* NewNode){
 
    Node* Tail;
 
    if(*Head==NULL){
        *Head=NewNode;
    }
    else{
        
        Tail=*Head;
        while(Tail->NextNode!=NULL){
            Tail=Tail->NextNode;
        }
 
        Tail->NextNode=NewNode;
    }
}
 
/*노드 삽입*/
void SLL_InsertAfter(Node* Current,Node* NewNode){
    NewNode->NextNode=Current->NextNode;
    Current->NextNode=NewNode;
}
 
void SLL_InsertBefore(Node** Head,Node* Current,Node* NewNode){
    Node* Index;
    if(*Head==Current){
        printf("오류\n");
    }
    else{
        Index=*Head;
        while(Index->NextNode!=Current && Index!=NULL){
            Index=Index->NextNode;
        }
        if(Index!=NULL){
            Index->NextNode=NewNode;
            NewNode->NextNode=Current;
        }
    }
}
 
void SLL_InsertNewHead(Node** Head,Node* NewHead){
    if(*Head==NULL){
        (*Head)=NewHead;
    }
    else{
        NewHead->NextNode=*Head;
        *Head=NewHead;
    }
}
 
/*노드제거*/
void SLL_RemoveNode(Node** Head,Node* Remove){
    if(*Head==Remove){
        *Head=Remove->NextNode;
    }
    else{
        Node* Current=*Head;
        while(Current!=NULL && Current->NextNode!=Remove){
            Current=Current->NextNode;
        }
        if(Current!=NULL){
            Current->NextNode=Remove->NextNode;
        }
    }
}
 
void SLL_DestroyAllNodes(Node** Head){
    int i,count=SLL_GetNodeCount(*Head);
    Node* Current;
    for(i=0;i<count;i++){
        Current=SLL_GetNodeAt(*Head,0);
        SLL_RemoveNode(Head,Current);
        SLL_DestroyNode(Current);
    }
}
        
 
/*노드탐색*/
 
Node* SLL_GetNodeAt(Node* Head,int Location){
    Node* Current = Head;
 
    while(Current!=NULL && (--Location)>=0){
        
        Current=Current->NextNode;
    }
 
    return Current;
}
 
/*노드수 세기*/
int SLL_GetNodeCount(Node* Head){
    int Count=0;
    Node* Current=Head;
    while(Current!=NULL){
        Current=Current->NextNode;
        Count++;
    }
 
    return Count;
}
cs


3. TestCode.c


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
//테스트용  c코드//
 
#include "LinkedList.h"
 
 
int main(){
    int i=0,Count=0;
    Node* List=NULL;
    Node* NewNode=NULL;
    Node* Current=NULL;
 
    /*노드 5개 추가*/
    for(i=0;i<5;i++){
        NewNode=SLL_CreateNode(i);
        SLL_AppendNode(&List,NewNode);
    }
 
    NewNode=SLL_CreateNode(-1);
    SLL_InsertNewHead(&List,NewNode);
 
    NewNode=SLL_CreateNode(-2);
    SLL_InsertNewHead(&List,NewNode);
 
    /*리스트 출력*/
    Count=SLL_GetNodeCount(List);
 
    for(i=0;i<Count;i++){
        Current=SLL_GetNodeAt(List,i);
        printf("List[%d]: %d\n",i,Current->Data);
    }
 
    /*리스트의 세번째 노드 뒤에 새 노드 삽입 */
    printf("\nInserting 3000 After [2]...\n\n");
 
    Current=SLL_GetNodeAt(List,2);
    NewNode=SLL_CreateNode(3000);
 
    SLL_InsertAfter(Current,NewNode);
 
    /*vitamin Quiz*/
 
    Current=SLL_GetNodeAt(List,2);
    NewNode=SLL_CreateNode(1000);
    SLL_InsertBefore(&List,Current,NewNode);
 
    /*리스트 재출력*/
    Count=SLL_GetNodeCount(List);
    for(i=0;i<Count;i++){
        Current=SLL_GetNodeAt(List,i);
        printf("List[%d] : %d\n",i,Current->Data);
    }
 
    /*모든 노드를 메모리에서 제거*/
    printf("\nDestroying List...\n");
 
    /* 책 본문
    for(i=0;i<Count;i++){
        Current=SLL_GetNodeAt(List,0);
        if(Current!=NULL){
            SLL_RemoveNode(&List,Current);
            SLL_DestroyNode(Current);
        }
    }*/
 
    SLL_DestroyAllNodes(&List); //비타민퀴즈
 
    return 0;
}
cs
d



실행결과



반응형

'개인 서재..* > 알고리즘' 카테고리의 다른 글

[C언어] 링크드 큐  (0) 2017.04.23
[C언어] 스택(feat.링크드 리스트)  (0) 2017.04.20
[C언어] 스택(feat.배열)  (0) 2017.04.14
[C언어] Circular Doubly Linked List  (0) 2017.04.14
[C언어] Doubly Linked List  (0) 2017.04.14
댓글