-
Notifications
You must be signed in to change notification settings - Fork 6
/
memAllocAndGarbage.c
165 lines (160 loc) · 3.56 KB
/
memAllocAndGarbage.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
typedef struct node{
struct node* prev;
int value;
int size;
struct node* next;
}node;
node* create(){
node* t = malloc(sizeof(node));
printf("Enter the value : ");
scanf("%d",&(t->value));
printf("Enter the size : ");
scanf("%d",&(t->size));
t->next=NULL;
t->prev=NULL;
return t;
}
node* insert(node* t,node* H){ // inserting at front
if(H==NULL){
H=t;
}
else{
node* p = H;
while(p->next){
p=p->next;
}
p->next=t;
t->prev=p;
}
return H;
}
node* insertGarbage(int p,node* H){ // inserting garbage
node* t= malloc(sizeof(node));
t->next=NULL;
t->prev=NULL;
t->size=p;
t->value=INT_MIN;
if(H==NULL){
H=t;
}
else{
node* p = H;
while(p->next){
p=p->next;
}
p->next=t;
t->prev=p;
}
return H;
}
void display(node * H){
printf("\nThe current list is : ");
node* p = H;
if(p){
while(p->next){
if(p->value!=INT_MIN){
printf("%d -> ",p->value);
}
else{
printf("# -> ");
}
p=p->next;
}
if(p->value!=INT_MIN){
printf("%d -> ",p->value);
}
else{
printf("# -> ");
}
}
printf("\nThe corresponding sizes are : ");
p = H;
if(p){
while(p->next){
printf("%d -> ",p->size);
p=p->next;
}
printf("%d -> ",p->size);
}
printf("\n");
}
void displayGarbage(node * HG){
printf("\nThe garbage values is : ");
node* p = HG;
if(p){
while(p->next){
printf("%d -> ",p->size);
p=p->next;
}
printf("%d -> ",p->size);
}
}
node* delete(node* H,node* HG){
int pos;
printf("Enter the pos to delete : ");
scanf("%d",&pos);
node* p = H;
for(int i =2;i<=pos;i++){
p=p->next;
}
if(p->value!=INT_MIN){
p->value=INT_MIN;
HG= insertGarbage(p->size,HG);
}
display(H);
return HG;
}
void garbageCollection(node* H,node* HG){
node * p =H;
int garbage = 0;
if(p){
while(p->next){
if(p->value==INT_MIN){
garbage += p->size;
}
p=p->next;
}
if(p->value==INT_MIN){
garbage += p->size;
}
}
printf("The amount of garbage collected is : %d",garbage);
displayGarbage(HG);
}
int main(){
node* HL = NULL;
node* HG = NULL;
int n,pos;
while (1){
printf("\n\n============================");
printf("\n1.Display\n2.Insert\n3.Delete\n4.Collect Garbage\n5.Exit");
printf("\n============================");
printf("\nEnter Choice : ");
fflush(stdin);
scanf("%d", &n);
node* t;
switch (n) {
case 1:
display(HL);
break;
case 2:
t=create();
HL=insert(t,HL);
display(HL);
break;
case 3:
HG = delete(HL,HG);
break;
case 4:
garbageCollection(HL,HG);
break;
case 5:
exit(0);
default :
printf("Wrong option selected");
}
}
}