source: CIVL/examples/languageFeatures/linkedList.cvl@ 90dd7d7

1.23 2.0 main test-branch
Last change on this file since 90dd7d7 was be48e64, checked in by Tim Zirkel <zirkeltk@…>, 13 years ago

Fixed bug in linkedList.cvl. The variable head was getting a value from a struct pointer before all the fields in that struct were set. The fields changed, but head already had the old value. Changed head to be a pointer, so updates are saved.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@186 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 649 bytes
Line 
1#include<civlc.h>
2
3$input int N;
4$assume N > 0 && N <= 5;
5
6typedef struct Node {
7 double value;
8 struct Node* next;
9} Node;
10
11void main() {
12 $heap h;
13 Node* head;
14 Node* current = (Node *) $malloc(&h, sizeof(Node));
15 Node final;
16
17 head = current;
18 head->value = 0.0;
19 for (int i = 0; i < N-1; i++) {
20 Node* newNode = (Node *) $malloc(&h, sizeof(Node));
21
22 newNode->value = (i+1)*2.71828;
23 current->next = newNode;
24 current = newNode;
25 }
26 current->next = NULL;
27 final = *head;
28 while (final.next != NULL) {
29 final = *(final.next);
30 }
31 $assert final.value == (N-1)*2.71828;
32 $assert final.value == current->value;
33}
Note: See TracBrowser for help on using the repository browser.