1 2 3 4 5 6 7 8 9 10

// functions and main program
NodePtr makenode ( int item )
{
NodePtr ptr;
// definition of ptr
ptr = (NodePtr) malloc ( sizeof (struct node));

if (ptr != NULL) {
ptr->data = item;
ptr->next = NULL; }
else exit (1); return ptr;

// check valid pointer

}

int main (void) {
NodePtr np = NULL;
NodePtr t;

// np start of list // t is a temp

for (i = 1; i < 5; i++) {
t = makenode (i);
// assumes t != NULL
t->next = np;
np = t;
}
for ( t = np; t != NULL; t = t->next)
printf ("%d\n", t->data);

}

What we have actually built here is a stack. A Last In First Out (LIFO) Queue

NOTE: We have simplified the programming by using a typedef in the header file to define NodePtr.

4 February, 1998

4

Copyright University of Alberta