Software Engineering 1: Laboratory Exercises
Listing 4: ptr1.c
/*
ptr_net.c
A test program illustrating structures and pointers.
B.McMullin, February 1993.
*/
#include <stdio.h>
struct bucket_s
{
struct bucket_s *black, *white;
int quantity;
};
struct bucket_s n0, n1, n2, n3;
void red_connections(void)
{
n0.black = &n1;
n0.white = &n3;
n3.black = n0.black;
n3.white = &n0;
n1.white = NULL;
n1.black = &n2;
n2.white = n3.black;
n2.black = n1.white;
}
void blue_connections(void)
{
n0.white = &n1;
n1.black = &n0;
n0.black = n0.white->black;
n1.white = n1.black->white;
n2.black = &n3;
n3.black = &n2;
n2.white = n2.black->black;
n3.white = n3.black->white;
}
void yellow_connections(void)
{
struct bucket_s *p;
p = n0.white;
n0.white = n1.white;
n1.white = n2.white;
n2.white = n3.white;
n3.white = p;
p = n0.black;
n0.black = n1.black;
n1.black = n2.black;
n2.black = n3.black;
n3.black = p;
}
void massage(void)
{
n0.black->quantity = 2 * n3.white->quantity;
n1.black->quantity = 2 * n2.white->quantity;
n0.white->quantity = 2 * n2.quantity;
}
void main(void)
{
n0.quantity = 2;
n1.quantity = -3;
n2.quantity = 4;
n3.quantity = -5;
red_connections();
massage();
blue_connections();
yellow_connections();
massage();
/*
printf("n0.quantity: %d\n", n0.quantity);
printf("n1.quantity: %d\n", n1.quantity);
printf("n2.quantity: %d\n", n2.quantity);
printf("n3.quantity: %d\n", n3.quantity);
*/
}