From 29ec0fb6df306b5e226a08ad511099480aff2a1a Mon Sep 17 00:00:00 2001 From: Doni Pracner Date: Sat, 7 May 2016 16:08:01 +0200 Subject: [PATCH] new sample and tests: linkedlist --- samples/linkedlist.mj | 85 ++++++++++++++++++++++++++++++++++++++++++ tests/linkedlist.1.txt | 1 + tests/linkedlist.2.txt | 7 ++++ tests/linkedlist.3.txt | 7 ++++ 4 files changed, 100 insertions(+) create mode 100644 samples/linkedlist.mj create mode 100644 tests/linkedlist.1.txt create mode 100644 tests/linkedlist.2.txt create mode 100644 tests/linkedlist.3.txt diff --git a/samples/linkedlist.mj b/samples/linkedlist.mj new file mode 100644 index 0000000..6d04746 --- /dev/null +++ b/samples/linkedlist.mj @@ -0,0 +1,85 @@ +program linkedlist + class Element { + int info; + Element next; + } + Element first; +{ + void add(int i) + Element newone; + { + newone = new Element; + newone.info = i; + newone.next = first; + first = newone; + } + + void printlist() + Element cur; + { + cur = first; + while (cur != null) { + print(cur.info); + cur = cur.next; + } + } + + int exists(int i) Element cur; { + cur = first; + while (cur != null && cur.info != i) + cur = cur.next; + if (cur != null) + return 1; + else + return 0; + } + + int count(int i) Element cur; int c;{ + cur = first; + c = 0; + while (cur != null) { + if (cur.info == i) + c++; + cur = cur.next; + } + return c; + } + + void main() + int num; int in; int c; { + print('l'); + print('?'); + read(num); + // demo + if (num<0) { + add(1); + add(2); + add(3); + add(2); + printlist(); + print(exists(7)); + print(count(2)); + } else { + c = 0; + while (c