πŸ“¦ 0-Chan / algorithm-in-C

πŸ“„ linked_list.c Β· 189 lines
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189/* Goal : Implementing CRUD using linked list in C
 * date : 2021. 01 . 26
 * author : Young Chan Kim (CogaPlex)
 */

#include <stdio.h>
#include <stdlib.h>
#include "linkedList.h"

void initList(LinkedList* list)
{
	list->head = NULL;
	list->size = 0;
}

Node* createNode(int item, Node* next)
{
	Node* n = malloc(sizeof(Node));
	if (n == NULL)
	{
		return 1;
	}

	n->data = item;
	n->next = next;

	return n;
}

int readList(LinkedList* list, int index)
{
	int i = 1;
	int data = 0;
	Node* tmp = list->head;

	while (tmp != NULL)
	{
		if (i == index)
		{
			data = tmp->data;
			return data;
		}

		tmp = tmp->next;
		i++;
	}
	return 1;
}

int insertNode(LinkedList* list, int index, int data)
{
	if (index < 0 || index > (list->size))
	{
		return 3;
	}
	int i = 0;
	Node* n = createNode(data, NULL);
	Node* tmp = list->head;

	if (index == 0)
	{
		n->next = list->head;
		list->head = n;
		(list->size)++;
		return 0;
	}
	else
	{
		while ((tmp != NULL) || (i == index))
		{
			if (tmp->next == NULL)
			{
				tmp->next = n;
				(list->size)++;
				return 0;
			}
			else if (i == index)
			{
				n->next = tmp->next;
				tmp->next = n;
				(list->size)++;
				return 0;
			}
			tmp = tmp->next;
			i++;
		}
	}
}

int updateNode(LinkedList* list, int index, int data)
{
	if (index < 0 || index > (list->size))
	{
		return 2;
	}

	int i = 0;
	Node* tmp = list->head;

	while (tmp != NULL)
	{
		if (i == index)
		{
			tmp->data = data;
			return 0;
		}

		tmp = tmp->next;
		i++;
	}
}

int deleteNode(LinkedList* list, int index)
{
	if (index < 0 || index > (list->size))
	{
		return 4;
	}

	int i = 0;
	Node* tmp = list->head;
	Node* n = NULL;

	if (index == 0)
	{
		list->head = tmp->next;

		free(tmp);
		(list->size)--;
		return 0;
	}
	else
	{
		while (tmp != NULL)
		{
			if (tmp->next == NULL)
			{
				n = tmp;

				tmp = tmp->next;

				free(tmp);
				(list->size)--;
				return 0;
			}
			else if (i == index - 1)
			{
				n = tmp;

				tmp = tmp->next;
				n->next = tmp->next;

				free(tmp);
				(list->size)--;
				return 0;
			}
			tmp = tmp->next;
			i++;
		}
	}
}

void errorCheck(int errorCode)
{
	switch (errorCode)
	{
	case 0:
		break;
	case 1:
		printf("createNode μ—λŸ¬ : malloc에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
		exit(1);
	case 2:
		printf("updateNode μ—λŸ¬ : λ§€κ°œλ³€μˆ˜ index 값을 확인해 μ£Όμ‹­μ‹œμ˜€.");
		exit(1);
	case 3:
		printf("insertNode μ—λŸ¬ : λ§€κ°œλ³€μˆ˜ index 값을 확인해 μ£Όμ‹­μ‹œμ˜€.");
		exit(1);
	case 4:
		printf("deleteNode μ—λŸ¬ : λ§€κ°œλ³€μˆ˜ index 값을 확인해 μ£Όμ‹­μ‹œμ˜€.");
		exit(1);
	case 5:
		printf("Pop μ—λŸ¬ : Stack underflow λ°œμƒ.");
		exit(1);
	default:
		printf("Undefined error code. Please check again.");
		break;
	}
}