-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
134 lines (111 loc) · 3.2 KB
/
linked_list.c
File metadata and controls
134 lines (111 loc) · 3.2 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 예제용 구조체 정의
typedef struct {
int studentID;
char name[50];
int age;
} Student;
// 연결 리스트의 노드 구조체
typedef struct ListNode {
Student data;
struct ListNode* next;
} ListNode;
// 연결 리스트의 헤드 노드를 가리키는 포인터
ListNode* head = NULL;
// 새로운 노드 생성
ListNode* createNode(Student student) {
ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
new_node->data = student;
new_node->next = NULL;
return new_node;
}
// 연결 리스트에 노드 추가
void insertNode(Student student) {
ListNode* new_node = createNode(student);
new_node->next = head;
head = new_node;
}
// 연결 리스트에서 노드 삭제
void deleteNode(int studentID) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
ListNode* current = head;
ListNode* previous = NULL;
while (current != NULL) {
if (current->data.studentID == studentID) {
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("Node with Student ID %d deleted.\n", studentID);
return;
}
previous = current;
current = current->next;
}
printf("Node with Student ID %d not found.\n", studentID);
}
// 연결 리스트 중간에 노드 추가
void insertAfter(int studentID, Student newStudent) {
ListNode* current = head;
while (current != NULL) {
if (current->data.studentID == studentID) {
ListNode* new_node = createNode(newStudent);
new_node->next = current->next;
current->next = new_node;
printf("Node inserted after Student ID %d.\n", studentID);
return;
}
current = current->next;
}
printf("Node with Student ID %d not found.\n", studentID);
}
// 연결 리스트 출력
void printList() {
ListNode* current = head;
while (current != NULL) {
printf("Student ID: %d, Name: %s, Age: %d\n", current->data.studentID, current->data.name, current->data.age);
current = current->next;
}
}
// 연결 리스트 해제
void freeList() {
ListNode* current = head;
while (current != NULL) {
ListNode* temp = current;
current = current->next;
free(temp);
}
}
int main() {
// 학생 정보를 추가하여 연결 리스트 생성
Student student1 = {101, "Alice", 20};
Student student2 = {102, "Bob", 22};
Student student3 = {103, "John", 21};
insertNode(student1);
insertNode(student2);
insertNode(student3);
// 연결 리스트 출력
printf("Original Student List:\n");
printList();
// 노드 삭제
deleteNode(102);
// 연결 리스트 출력
printf("\nStudent List after Deletion:\n");
printList();
// 노드 추가
Student newStudent = {104, "Eve", 23};
insertAfter(101, newStudent);
// 연결 리스트 출력
printf("\nStudent List after Insertion:\n");
printList();
// 연결 리스트 해제
freeList();
return 0;
}