Created
October 20, 2023 15:51
-
-
Save jtpaasch/4e6285a90878d3e20847f7809f71ebd6 to your computer and use it in GitHub Desktop.
Working example of the running example on p. 15 of Chang et al, "Shape Analysis," in Trends and Foundations of Programming Languages vol 6, no 1-2, pp. 1-158.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
typedef struct list { | |
struct list * n; | |
int d; | |
} list; | |
void insert(list * l, int d) { | |
list * t = l; | |
while (t != NULL) { | |
if (t->d == d) { | |
list * fresh = (list *) malloc(sizeof(list)); | |
fresh->n = t->n; | |
fresh->d = d; | |
t->n = fresh; | |
break; | |
} else { | |
t = t->n; | |
} | |
} | |
} | |
void print_list(list l) { | |
cout << "["; | |
cout << l.d; | |
while (l.n != NULL) { | |
l = *l.n; | |
cout << ", " << l.d; | |
} | |
cout << "]" << "\n"; | |
} | |
int main() | |
{ | |
cout << "Running program..." << "\n\n"; | |
list l1; | |
l1.n = 0; | |
l1.d = 3; | |
cout << "List 1:" << "\n"; | |
print_list(l1); | |
cout << "\n"; | |
cout << "List 2:" << "\n"; | |
list l2; | |
l2.n = &l1; | |
l2.d = 4; | |
print_list(l2); | |
cout << "\n"; | |
cout << "Inserting an extra 3:" << "\n"; | |
insert(&l2, 3); | |
print_list(l2); | |
cout << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment