-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_linked_list.c
119 lines (106 loc) · 2.49 KB
/
doubly_linked_list.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int x;
struct node *next;
struct node *prev;
}NODE;
void printList(NODE *head);
void siraliEkle(NODE **head,int data);
void elemanSil(NODE **head,int data);
NODE* getNode(int data);
int main(){
NODE *head = NULL;
siraliEkle(&head,400);
printList(head);
siraliEkle(&head,40);
printList(head);
siraliEkle(&head,4);
printList(head);
siraliEkle(&head,450);
printList(head);
siraliEkle(&head,50);
printList(head);
elemanSil(&head,50);
printList(head);
elemanSil(&head,999);
printList(head);
elemanSil(&head,4);
printList(head);
elemanSil(&head,450);
printList(head);
return 0;
}
void printList(NODE *head){
NODE *tmp = head;
while(tmp != NULL){
printf("%d ",tmp->x);
tmp = tmp->next;
}
printf("\n");
}
NODE* getNode(int data){
NODE *node = (NODE*)malloc(sizeof(NODE));
node->x = data;
node->next = NULL;
node->prev = NULL;
return node;
}
void siraliEkle(NODE **head, int data){
NODE *newNode = getNode(data);
NODE *tmp;
if((*head) == NULL){
(*head) = newNode;
(*head)->prev = NULL;
}
else if((*head)->x > data){
newNode->prev = NULL;
newNode->next = (*head);
(*head)->prev = newNode;
(*head) = newNode;
}
else{
tmp = (*head);
while(tmp->next != NULL && tmp->next->x < data){
tmp = tmp->next;
}
newNode->next = tmp->next;
tmp->next = newNode;
newNode->prev = tmp;
if(newNode->next != NULL){
newNode->next->prev = newNode;
}
}
}
void elemanSil(NODE **head,int data){
NODE *tmp;
NODE *c;
if((*head)==NULL){
printf("Linked List bos, silinecek eleman yok");
}
else if((*head)->x == data){
tmp = (*head);
(*head) = (*head)->next;
printf("%d silindi..\n",data);
free(tmp);
(*head)->prev = NULL;
}
else{
tmp = (*head);
while(tmp->next != NULL && tmp->next->x != data){
tmp = tmp->next;
}
if(tmp->next != NULL){
c = tmp->next;
tmp->next = tmp->next->next;
if(tmp->next != NULL){
tmp->next->prev = tmp;
}
printf("%d silindi..\n",data);
free(c);
}
else{
printf("Silinecek eleman bulunamadi..\n");
}
}
}