카테고리 없음

자료구조-노드사용[c언어]

리그캣 2018. 1. 21. 16:45

노드사용하기


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct ListNode{  
    char data[10];
    struct ListNode* link; 
} listNode;
 
typedef struct{   
   listNode* head;
} linkedList_h;
 
linkedList_h* createLinkedList_h(void);
void freeLinkedList_h(linkedList_h*);
void addLastNode(linkedList_h*char*);
void reverse(linkedList_h*);
void deleteLastNode(linkedList_h*);
void printList(linkedList_h*);
void addFirstNode(linkedList_h*);
 
linkedList_h* createLinkedList_h(void){ 
   linkedList_h* L;
   L = (linkedList_h*)malloc(sizeof(linkedList_h));
   L -> head = NULL
   return L;
}
 
void addLastNode(linkedList_h* L, char* x){ 
    listNode* newNode;
    listNode* p;
    newNode = (listNode*)malloc(sizeof(listNode)); 
    strcpy(newNode->data, x);
 
    newNode->link= NULL;
    if (L->head == NULL){  
       L->head = newNode; 
       return;
   }
   p = L->head;  
   while (p->link != NULL) p = p->link; 
   p ->link = newNode; 
}
 
 void addFirstNode(linkedList_h* L, char* x)
 {
 
     listNode* FirstNode;
     FirstNode = (listNode*)malloc(sizeof(listNode)); 
     strcpy(FirstNode->data, x);
     FirstNode->link=L->head;
     L->head=FirstNode;
    
 }
 
void reverse(linkedList_h * L){ 
      listNode* p; 
    listNode* q;
    listNode* r;
 
    p = L->head;
    q=NULL;
    r=NULL;
 
    while (p!= NULL){
      r = q;
      q = p;
      p = p->link;
      q->link = r;
    }
    L->head = q;
 
   }
 
   void deleteLastNode(linkedList_h * L){
    listNode* previous;
    listNode* current;
    if (L->head == NULLreturn;
 
    if (L->head->link == NULL) {  
       free(L->head);          
       L->head = NULL;      
       return;
    }
    else {                       
       previous = L->head;       
       current = L->head->link;
       while(current ->link != NULL){
       previous = current;
       current = current->link;
       }
       free(current);
       previous->link = NULL;
    }
   }
 
   void freeLinkedList_h(linkedList_h* L){
    listNode* p;
    while(L->head != NULL){
       p = L->head;
       L->head = L->head->link;
       free(p);
       p=NULL;
    }
   }
 
   void printList(linkedList_h* L){   
    listNode* p;
    printf("L = (");
    p= L->head;
    while(p != NULL){
       printf("%s", p->data);
       p = p->link;
 
       if(p != NULLprintf(", ");
    }
    printf(") \n");
   }
 
 int main(){
    linkedList_h* L;
    L = createLinkedList_h();
    printf("(1) 공백 리스트 생성하기! \n");
    printList(L); getchar();
 
    printf("(2) 리스트에 3개의 노드 추가하기! \n");
    addLastNode(L, "월");
    addLastNode(L, "수");
    addLastNode(L, "금");
    printList(L); getchar();
 
    printf("(3) 리스트 처음에 노드 한개 추가하기! \n");
    addFirstNode(L, "일");
    printList(L); getchar();
 
    printf("(4) 마지막 노드 삭제하기! \n");
 
    deleteLastNode(L);
    printList(L); getchar();
 
    printf("(5) 리스트 원소를 역순으로 변환하기! \n");
    reverse(L);
    printList(L); getchar();
 
    printf("(6) 리스트 공간을 해제하여, 공백 리스트 상태로 만들기! \n");
    freeLinkedList_h(L);
    printList(L);
 
   getchar();
 
   return 0;
}
cs