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
|
//Solved Example 3:
//Implementing Push And Pop Functions:
function[y,sta1]=empty(sta)
y=0;
sta1=0;
if(sta.top==0)
y=0;
else
y=1;
end
sta1=sta
endfunction
function[sta]=push(stac,ele)
sta=0;
if(empty(stac)==0)
stac.a=ele;
stac.top=stac.top+1;
else
stac.a=[stac.a(:,:) ele]
stac.top=stac.top+1;
end
disp(stac);
sta=stac;
endfunction
function[ele,sta]=pop(stack)
ele='-1';
if(empty(stack)==0)
disp("Stack Underflow");
break;
else
ele=stack.a(stack.top);
stack.top=stack.top-1;
if(stack.top~=0)
b=stack.a(1);
for i2=2:stack.top
b=[b(:,:) stack.a(i2)];
end
stack.a=b;
else
stack.a='0';
end
end
disp(stack);
sta=stack;
endfunction
global stack
//Calling Routine:
stack=struct('a',0,'top',0);
stack=push(stack,4);
stack=push(stack,55);
stack=push(stack,199);
stack=push(stack,363);
[ele,stack]=pop(stack);
disp(stack,"After the above operations stack is:");
|