-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlurc_linkedlist.c
126 lines (114 loc) · 2.33 KB
/
lurc_linkedlist.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
120
121
122
123
124
125
126
/*Init Single and Double Link List from external value*/
#include <stdio.h>
#include <stdlib.h>
struct dlist {
int dat;
int id;
struct dlist *prev;
struct dlist *next;
};
struct dlist *dhead = NULL;
struct dlist *current = NULL;
struct dlist *start = NULL;
struct dlist *end = NULL;
struct dlist * insert_dlist(struct dlist *node, int val, int key)
{
struct dlist *ptr =(struct dlist *)malloc(sizeof(struct dlist));
ptr->dat= val;
ptr->id= key;
ptr->next=NULL;
ptr->prev=NULL;
if(current==NULL)
{
dhead = ptr;
start = dhead;
current = ptr;
}
else
{
ptr->prev=current;
current->next=ptr;
current =ptr;
}
return current;
}
void init_cache(void)
{
for(int i=0; i<5; i++)
{
current = insert_dlist(current,i,i);
}
end= current;
}
void tarverse_cache(struct dlist *node, int dir)
{
printf("\nid \tval\n");
while(node != NULL)
{
printf("%d \t%d\n",node->id, node->dat);
if(dir==1)
node= node->next;
else
node= node->prev;
}
}
struct dlist * find_id(struct dlist *node, int key)
{
while(node != NULL)
{
if(node->id == key)
break;
else
node= node->next;
}
return node;
}
void interchange_node(struct dlist *node, struct dlist *last)
{
struct dlist *ptr=NULL;
if(node->prev == NULL)
{
ptr = node->next;
ptr->prev = node->prev;
start = ptr;
}
else if(node->next == NULL)
{
return;
}
else
{
ptr = node->prev;
ptr->next = node->next;
ptr = node->next;
ptr->prev=node->prev;
}
node->prev = last;
last->next = node;
node->next = NULL;
end = node;
}
void main(void)
{
int id,value;
struct dlist *id_nod_loc=NULL;
init_cache();
tarverse_cache(dhead,1);
while(1)
{
printf("\nEnter Process Id= ");
scanf("%d",&id);
id_nod_loc = find_id(dhead,id);
if(id_nod_loc == NULL)
{
printf("\nEnter Value= ");
scanf("%d",&value);
start->id= id;
start->dat=value;
id_nod_loc = start;
}
interchange_node(id_nod_loc,end);
dhead =start;
tarverse_cache(dhead,1);
}
}