-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct Node Insert, Delete.c
50 lines (46 loc) · 1.01 KB
/
struct Node Insert, Delete.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
#include<stdio.h>
#include<windows.h>
int Search(NodePointer ptr, int value);
void Insert(NodePointer pre, int value);
int Delete(NodePointer del, NodePointer tra);
typedef struct NODE* NodePointer;
typedef struct NODE {
int data;
NodePointer link;
}NODE;
NodePointer head = NULL;
int main() {
Insert(head, 1);
}
int Search(int key) {
int count = 0;
for (NodePointer ptr = head; ptr->data != key; ptr = ptr->link) {
if (ptr->link == NULL) return -1;
count++;
}
return count;
}
//노드 삽입함수
void Insert(NodePointer pre, int value) {
NodePointer temp = (NodePointer)malloc(sizeof(NODE));
if (!head) { // 리스트가 공백인 상태
temp->data = value;
temp->link = NULL;
head = temp;
}
else { // 리스트가 공백이 아닌 상태
temp->data = value;
temp->link = pre->link;
pre->link = temp;
}
}
//노드 삭제함수
int Delete(NodePointer del, NodePointer tra) {
if (!head) return printf("List is Empty!\n");
else {
NodePointer temp = del;
if (tra) tra->link = del->link;
else head = head->link;
free(temp);
}
}