source:
CIVL/examples/languageFeatures/linkedList.cvl@
bd7a43e
| Last change on this file since bd7a43e was ea777aa, checked in by , 3 years ago | |
|---|---|
|
|
| File size: 841 bytes | |
| Rev | Line | |
|---|---|---|
| [36b5ada] | 1 | /* Commandline execution: |
| 2 | * civl verify linkedList.cvl | |
| 3 | * */ | |
| [e6b02c8] | 4 | #include<civlc.cvh> |
| [97cfc53] | 5 | |
| 6 | $input int N; | |
| [3ff27cf] | 7 | $assume(N > 0 && N <= 5); |
| [97cfc53] | 8 | |
| 9 | typedef struct Node { | |
| 10 | double value; | |
| 11 | struct Node* next; | |
| 12 | } Node; | |
| 13 | ||
| 14 | void main() { | |
| [78fdaf0] | 15 | $scope s = $here; |
| [be48e64] | 16 | Node* head; |
| [78fdaf0] | 17 | Node* current = (Node *) $malloc(s, sizeof(Node)); |
| [97cfc53] | 18 | Node final; |
| 19 | ||
| [be48e64] | 20 | head = current; |
| 21 | head->value = 0.0; | |
| [97cfc53] | 22 | for (int i = 0; i < N-1; i++) { |
| [78fdaf0] | 23 | Node* newNode = (Node *) $malloc(s, sizeof(Node)); |
| [97cfc53] | 24 | |
| 25 | newNode->value = (i+1)*2.71828; | |
| 26 | current->next = newNode; | |
| 27 | current = newNode; | |
| 28 | } | |
| 29 | current->next = NULL; | |
| [be48e64] | 30 | final = *head; |
| [97cfc53] | 31 | while (final.next != NULL) { |
| 32 | final = *(final.next); | |
| 33 | } | |
| [d980649] | 34 | $assert(final.value == (N-1)*2.71828); |
| 35 | $assert(final.value == current->value); | |
| [48bfab9] | 36 | while (head->next != NULL) { |
| 37 | Node* tmp = head; | |
| 38 | ||
| 39 | head = head->next; | |
| 40 | $free(tmp); | |
| 41 | } | |
| 42 | $free(head); | |
| [97cfc53] | 43 | } |
Note:
See TracBrowser
for help on using the repository browser.
