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
|
clear;
clc;
//Implementing Priority Queue Using Lists
funcprot(0)
function[link2]=insert_pri(ele,link1)
link2=list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0);
if(link1(1)(1).add==0)
link1(1)(1).data=ele;
link1(1)(1).add=1;
link1(1)(1).nexadd=1;
link2(1)=link1(1)(1);
else
if(link1(1)(1).nexadd==link1(1)(1).add)
if(ele>=link1(1)(1).data)
t=ele;
p=link1(1)(1).data;
else
t=link1(1)(1).data;
p=ele;
end
link1(1)(1).data=t;
lin2=link1(1)(1);
lin2.data=p;
lin2.add=2;
lin2.nexadd=link1(1)(1).add;
link1(1)(1).nexadd=lin2.add;
link2(1)=link1(1)(1);
link2(2)=lin2;
else
i=1;
a=[];
while(link1(i).nexadd~=link1(1)(1).add)
a=[a(:,:) link1(i).data];
i=i+1;
end
a=[a(:,:) link1(i).data];
a=gsort(a);
j=1;
while(j<=i)
link1(j).data=a(j);
j=j+1;
end
k=1;
while(link1(k).data>=ele)
if(link1(k).nexadd==link1(1)(1).add)
break;
else
link2(k)=link1(k);
k=k+1;
end
end
if(link1(k).nexadd~=link1(1)(1).add)
lin2=link1(k);
lin2.data=ele;
lin2.add=link1(k).add;
j=k;
y=link1(1)(1).add;
while(link1(k).nexadd~=y)
link1(k).add=link1(k).add+1;
link1(k).nexadd=link1(k).nexadd+1;
k=k+1;
end
link1(k).add=link1(k).add+1;
lin2.nexadd=link1(j).add;
link2(j)=lin2;
j=j+1;
while(j<=k+1)
link2(j)=link1(j-1);
j=j+1;
end
else
lin2=link1(k);
lin2.data=ele;
lin2.nexadd=link1(1)(1).add;
lin2.add=link1(k).add+1;
link1(k).nexadd=lin2.add;
j=1;
while(j<=k)
link2(j)=link1(j);
j=j+1;
end
link2(j)=lin2;
end
end
end
endfunction
function[ele,link2]=extract_min(link1);
link2=list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0);
i=1;
ele=-1;
if(link1(1)(1).add==0)
disp("Underflow");
return;
else
if(link1(1)(1).nexadd==link1(1)(1).add)
link1(1)(1).add=0;
link1(1)(1).nexadd=0;
ele=link1(1)(1).data;
link1(1)(1).data=0;
link2(1)=link1(1)(1);
else
i=1;
while(link1(i).nexadd~=link1(1)(1).add)
link2(i)=link1(i);
i=i+1;
end
ele=link1(i).data;
link2(i-1).nexadd=link2(1).add;
end
end
endfunction
//Calling Routine:
link1=struct('data',0,'add',0,'nexadd',0);
link1=insert_pri(3,link1);
link1=insert_pri(4,link1);
link1=insert_pri(22,link1);
link1=insert_pri(21,link1);
link1=insert_pri(11,link1);
disp(link1,"List After Insertions");
[ele,link1]=extract_min(link1)
disp(ele,"Element after the min extraction");
|