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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
//STACK USING CIRCULAR LINKED LIST
funcprot(0)
function[link2]=append(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)
lin2=link1(1)(1);
lin2.data=ele;
lin2.add=link1(1)(1).add+1;
link1(1)(1).nexadd=lin2.add;
lin2.nexadd=link1(1)(1).add;
link2(1)=link1(1)(1);
link2(2)=lin2;
else
lin2=link1(1)(1);
i=1;
while(link1(i)(1).nexadd~=link1(1)(1).add)
i=i+1;
end
j=i;
lin2.data=ele;
lin2.add=link1(i).add+1;
lin2.nexadd=link1(1)(1).add;
link1(i).nexadd=lin2.add;
link2(1)=link1(1)(1);
i=2;
while(link1(i).nexadd~=lin2.add)
link2(i)=(link1(i));
i=i+1;
end
link2(i)=link1(i);
link2(i+1)=lin2;
end
end
endfunction
function[link2]=add(ele,pos,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;
while(i<=pos)
if(link1(i).nexadd==link1(1)(1).add)
break;
else
i=i+1;
end
end
if(link1(i).nexadd~=link1(1)(1).add)
i=i-1;
lin2.data=ele;
lin2.add=i;
j=i;
while(link1(j).nexadd~=link1(1)(1).add)
link1(j).add=link1(j).add+1;
link1(j).nexadd=link1(j).nexadd+1;
j=j+1;
end
link1(j).add=link1(j).add+1;
lin2.nexadd=link1(i).add;
link1(i-1).nexadd=lin2.add;
k=1;
while(k<i)
link2(k)=link1(k);
k=k+1;
end
link2(k)=lin2;
k=k+1;
link2(k)=link1(k-1);
k=k+1
l=k-1;
while(k~=j)
link2(k)=link1(l);
k=k+1;
l=l+1;
end
link2(j)=link1(j-1);;
link2(j+1)=link1(j);
else
if(i==pos)
k=1;
lin2.data=ele;
lin2.add=link1(i-1).add+1;
link1(i).add=link1.add+1;
lin2.nexadd=link1(i).add;
link1(i).nexadd=link1(1)(1).add;
k=1;
while(k<pos)
link2(k)=link1(k);
k=k+1;
end
link2(k)=lin2;
link2(k+1)=link1(k)
end
end
endfunction
function[link2]=delete1(pos,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;
if(link1(1)(1).add==0)
disp("Invalid");
else
if(link1(1)(1).nexadd==link1(1)(1).add)
link1(1)(1).add=0;
link1(1)(1).nexadd=0;
link1(1)(1).data=0;
link2(1)=link1(1)(1);
else
while(i<=pos)
if((link1(i).nexadd==link1(1)(1).add))
break;
else
i=i+1;
end
end
if(link1(i).nexadd~=link1(1)(1).add)
i=i-1;
j=1;
if(i==1)
j=1;
while(link1(j).nexadd~=link1(1)(1).add)
link2(j)=link1(j);
j=j+1;
end
link2(j)=link1(j);
else
link1(i-1).nexadd=link1(i+1).add;
while(link1(j).nexadd~=link1(i+1).add)
link2(j)=link1(j);
j=j+1;
end
if(j~=i-1)
link2(j)=link1(j);
link2(j+1)=link1(j+1);
k=i+1;
l=2;
else
link2(j)=link1(j);
k=i+1;
l=1;
end
while(link1(k).nexadd~=link1(1)(1).add)
link2(j+l)=link1(k);
k=k+1;
l=l+1;
end
link2(j+l)=link1(k);
end
else
if(i==pos)
j=1;
link1(i-1).nexadd=link1(1)(1).add;
while(j<=i-1)
link2(j)=link1(j);
j=j+1;
end
end
end
end
end
endfunction
function[sta]=push(ele,stack)
if(stack.top==0)
stack.a=ele;
stack.top=stack.top+1;
sta=stack;
else
i=1;
link1=struct('data',0,'add',0,'nexadd',0);
while(i<=stack.top)
link1=append(stack.a(i),link1);
i=i+1;
end
link1=append(ele,link1);
stack.top=stack.top+1;
a=[stack.a(:,:) link1(stack.top).data];
stack.a=a;
sta=stack;
end
endfunction
function[ele,sta]=pop(stack)
ele=-1;
sta=0;
if(stack.top==0)
disp("Stack Underflow");
return;
else
i=1;
link1=struct('data',0,'add',0,'nexadd',0);
while(i<=stack.top)
link1=append(stack.a(i),link1);
i=i+1;
end
ele=link1(stack.top).data;
link1=delete1(stack.top,link1);
stack.top=stack.top-1;
i=2;
a=link1(1)(1).data
while(i<=stack.top)
a=[a(:,:) link1(i).data];
i=i+1;
end
stack.a=a;
sta=stack;
end
endfunction
function[stack]=empty()
stack=struct('a',0,'top',0);
endfunction
//Calling Routine:
stack=empty()//Create an empty stack
stack=push(4,stack);
stack=push(55,stack);
stack=push(199,stack);
stack=push(363,stack);
[ele,stack]=pop(stack);
disp(stack,"After the above operations stack is:");
|