summaryrefslogtreecommitdiff
path: root/1034/CH4/EX4.4/4s4.sce
blob: cf9890d009a923a80b664328e027febaf8cb96c3 (plain)
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
//Deletion from the list:
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=0;
    link1(1)(1).prevadd=0;
    link2(1)=link1(1)(1);
    else
    if(link1(1)(1).nexadd==0)
      lin2=link1(1)(1);
      lin2.data=ele;
      lin2.add=link1(1)(1).add+1;
      link1(1)(1).nexadd=lin2.add;
      lin2.nexadd=0;
      lin2.prevadd=link1(1)(1).add;
      link2(1)=link1(1)(1);
      link2(2)=lin2;
    else
      lin2=link1(1)(1);
      i=1;
      while(link1(i)(1).nexadd~=0)
        i=i+1;
      end
      j=i;
      lin2.data=ele;
      lin2.add=link1(i).add+1;
      lin2.nexadd=0;
      link1(i).nexadd=lin2.add;
      lin2.prevadd=link1(i).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==0)
       break;
     else
       i=i+1;
     end
   end
   if(link1(i).nexadd~=0)
     i=i-1;
      lin2.data=ele;
      lin2.add=i;
      j=i;
      while(link1(j).nexadd~=0)
        link1(j).prevadd=link1(j).prevadd+1;
        link1(j).add=link1(j).add+1;
        link1(j).nexadd=link1(j).nexadd+1;
        j=j+1;
      end
      link1(j).prevadd=link1(j).prevadd+1;
      link1(j).add=link1(j).add+1;
      lin2.nexadd=link1(i).add;
      link1(i).prevadd=lin2.add;
      lin2.prevadd=link1(i-1).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(i).add+1;
         lin2.nexadd=link1(i).add;
         link1(i).prevadd=lin2.add;
         lin2.prevadd=link1(i-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;
  while(i<=pos)
    if((link1(i).nexadd==0))
      break;
    else
      i=i+1;
    end
  end
  if(link1(i).nexadd~=0)
    i=i-1;
    j=1;
    if(i==1)
      j=1;
      while(link1(j).nexadd~=0)
        link2(j)=link1(j);
        j=j+1;
      end
      link2(j)=link1(j);
    else
      link1(i-1).nexadd=link1(i+1).add;
      link1(i+1).prevadd=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~=0)
      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=0;
      while(j<=i-1)
        link2(j)=link1(j);
        j=j+1;
      end
    end
  end
endfunction
//Calling Routine:
link1=struct('data',0,'add',0,'nexadd',0);
link1=append(4,link1);
link1=append(6,link1);
link1=add(10,2,link1);
link1=delete1(3,link1);
disp(link1,"After the above manipulation the list is");