-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list2.c
67 lines (56 loc) · 1.35 KB
/
linked_list2.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int x;
struct node *next;
}NODE;
void printList(NODE *head);
void siraliEkle(NODE **head,int data);
NODE* getNode(int data);
int main(){
NODE *head = NULL;
siraliEkle(&head,10);
printList(head);
siraliEkle(&head,5);
printList(head);
siraliEkle(&head,15);
printList(head);
siraliEkle(&head,12);
printList(head);
siraliEkle(&head,35);
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;
return node;
}
void siraliEkle(NODE **head, int data){ //head'in degisme ihtimali de var(**) (linked list boşsa)
NODE *newNode = getNode(data);
NODE *tmp;
if((*head) == NULL){ //linkli liste boşsa, head degisir
(*head) = newNode;
}
else if((*head)->x > data){ //eklenecek node, ilk elemandan(head)'den kücükse
newNode->next = (*head);
(*head) = newNode;
}
else{
tmp = (*head);
while(tmp->next != NULL && tmp->next->x < data){
tmp = tmp->next;
}
newNode->next = tmp->next;
tmp->next = newNode;
}
}