-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.c
More file actions
51 lines (45 loc) · 1.08 KB
/
insert.c
File metadata and controls
51 lines (45 loc) · 1.08 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
#include<stdio.h>
#include<stdlib.h>
#include "dll.h"
#include "compare.h"
doublyLL insert(int key, doublyLL dl)
{
int i, temp;
if(freeList.size == 0)
{
printf("\nFAILURE: MEMORY NOT AVAILABLE\n");
return dl;
}
if(compare(arr[dl.head], key) == GREATER)
{
temp = freeList.head;
freeList.head = arr[freeList.head+1];
arr[temp] = key;
arr[dl.head+2] = temp;
arr[temp+1] = dl.head;
arr[temp+2] = -1;
dl.head = temp;
dl.size++;
freeList.size--;
printf("\nSUCCESS\n");
return dl;
}
for(i = dl.head; arr[i+1] != -1; i = arr[i+1])
{
if(compare(arr[arr[i+1]], key) == GREATER)
break;
}
//if(compare(arr[arr[i+1]], key) == GREATER)
temp = freeList.head;
freeList.head = arr[freeList.head+1];
arr[temp+1] = arr[i+1];
arr[temp+2] = i;
if(arr[i+1] != -1)
arr[arr[i+1]+2] = temp;
arr[i+1] = temp;
arr[temp] = key;
dl.size++;
freeList.size--;
printf("\nSUCCESS\n");
return dl;
}