summaryrefslogtreecommitdiff
path: root/1034/CH9/EX9.1/s1.sce
diff options
context:
space:
mode:
authorpriyanka2015-06-24 15:03:17 +0530
committerpriyanka2015-06-24 15:03:17 +0530
commitb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (patch)
treeab291cffc65280e58ac82470ba63fbcca7805165 /1034/CH9/EX9.1/s1.sce
downloadScilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.gz
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.bz2
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.zip
initial commit / add all books
Diffstat (limited to '1034/CH9/EX9.1/s1.sce')
-rwxr-xr-x1034/CH9/EX9.1/s1.sce122
1 files changed, 122 insertions, 0 deletions
diff --git a/1034/CH9/EX9.1/s1.sce b/1034/CH9/EX9.1/s1.sce
new file mode 100755
index 000000000..04950f702
--- /dev/null
+++ b/1034/CH9/EX9.1/s1.sce
@@ -0,0 +1,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");
+ \ No newline at end of file