gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
expanded sample size, first version
[mjc2wsl.git] / samples / alpha-mj-exp / PalindromesList.mj
1 program PalindromesList
2 class Element {
3 char info;
4 Element prev, next;
5 }
7 Element first, last;
8 {
9 void initList() {
10 first = null;
11 last = null;
12 }
14 void addToEnd(char c)
15 Element newone;
16 {
17 newone = new Element;
18 newone.info = c;
19 newone.next = null;
20 newone.prev = null;
22 if (last == null) {
23 first = newone;
24 last = newone;
25 } else {
26 newone.prev = last;
27 last.next = newone;
28 last = newone;
29 }
30 }
32 void printSubword(Element start, Element end)
33 Element curr;
34 {
35 curr = start;
36 while (curr != end) {
37 print(curr.info, 3);
38 curr = curr.next;
39 }
40 print(curr.info, 3);
41 print('\n');
42 }
44 int isPalindrome(Element start, Element end)
45 {
46 while (start != end && start.next != end) {
47 if (start.info != end.info) {
48 return 0;
49 } else {
50 start = start.next;
51 end = end.prev;
52 }
53 }
55 if (start.info == end.info) {
56 return 1;
57 }
58 return 0;
59 }
61 void printAllPalindromes()
62 Element start, end;
63 {
64 start = first;
65 while (start != null) {
66 end = last;
67 while (end != start) {
68 if (isPalindrome(start, end) == 1) {
69 printSubword(start, end);
70 }
71 end = end.prev;
72 }
73 start = start.next;
74 }
75 }
77 void printlist()
78 Element cur;
79 {
80 cur = first;
81 while (cur != null) {
82 print(cur.info);
83 cur = cur.next;
84 }
85 print('\n');
86 }
88 void load()
89 int num, c;
90 char in;
91 {
92 print('l');
93 print('?');
94 read(num);
95 c = 0;
96 while (c<num) {
97 print(c);
98 print('?');
99 read(in);
100 if (ord(in) == 10) {
101 read(in);
103 addToEnd(in);
104 c++;
106 print('l');
107 print(':');
108 printlist();
111 void main()
113 load();
114 printAllPalindromes();
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner