Linked List - First Part


/** NOTE: I used nothing but * notation in this program.  I expected that
you would be using pointers (->) and since the book no longer has the *
notation, I decided to give you an example.  I also added a couple of
simple functions to this program such as is_empty() which make the code a
bit more readable.  I also added several defines to enhance readability.
**/

/*****
*  Brenda Sonderegger       CS210    Fall, 1994
*
**** Linked list example ***/

#include  <stdio.h>
#include  "myio_c.h"

/**  syntatic sugar  **/

#define   TRUE    1
#define   FALSE   0
#define   INSERT  1
#define   DELETE  2
#define   PRINT   3
#define   QUIT    4

/** the structure definition and the prototypes for the functions using the
    structure all belong in a linked.h file
**/

typedef struct node_record
{
   int  info;
   struct node_record  *link;
} NODE;

/** I left variable names in to help explain what was being sent **/
/** new is a keyword in C++ and should NOT be used as a variable name if
    you are using a C++ compiler **/


int is_empty(NODE *first);
  /** is_empty() is a nice utility - does the job, is readable, nicely named
      etc.  an is_full() is also useful but not as much for a linked list
      implementation as for an array implementation.  Still wouldn't be
      bad to have one.
  **/
void get_memory(NODE **new_node);
void fill_node(NODE *new_node);
  /** It would be just as good to combine create_list() and add_node()
      and only have insert_node() as the combined subprogram
  **/
void create_list(NODE **first, NODE *new_node);
void add_node(NODE **first, NODE *new_node);

void print_node(NODE *node);
void print_list(NODE *first);

/** print_menu() and main() should all be in a driver.c file **/
void print_menu(void);

void main(void)
{
  NODE *first, *new_node;
  int flag, option, value;

  first = new_node = NULL;
  do
  {
    print_menu();
    printf("\nEnter your choice now ");
    get_int_bound(&option, 1, 4);
    switch(option)
    {
      case INSERT: get_memory(&new_node);
                      /** it is "safer" to check to see if memory was
                          allocated here instead of assuming it was done
                          in the subprogram. 
                      **/
                   if (new_node != NULL)
                   {
                     fill_node(new_node);
                     if (is_empty(first) == TRUE)
                       create_list(&first, new_node);
                     else
                       add_node(&first, new_node);
                   }
                   else
                   {
                     printf("\nNo memory available - exit()\n\n");
                     exit(1);
                   }
                   break;
      case DELETE: printf("\nNot implemented yet.\n");
                   break;
      case PRINT: if (is_empty(first) == FALSE)
                     print_list(first);
                  else
                     printf("\nList is empty\n");
                  break;
      case QUIT: printf("\nQuitting program.\n\n");
                 break;
    }
  }
  while (option != QUIT);
}



void print_menu(void)
{

   printf("\nEnter your choice \n");
   printf("\n1 - Insert a new node");
   printf("\n2 - Delete a node");
   printf("\n3 - Print the list");
   printf("\n4 - Quit the program\n");
}


/*****all functions from here down belong in linked.c file*****/
void add_node(NODE **first, NODE *new_node)
{
  /*   following declarations not needed for this part but are used 
       for insert for next part of the assignment.
  */

  NODE *current, *previous; 

    /*insert at front */

  (*new_node).link = *first;
  *first = new_node;
}

/* optional could simply have a decision statement in the add_node()
   if (*front == NULL)
      *first = new_node;
   else
     etc.
*/
void create_list(NODE **first, NODE *new_node)
{
  *first = new_node;
}


void get_memory(NODE **new_node)
{
   *new_node = (NODE *)malloc(sizeof(NODE));
   if (*new_node == NULL)
   {
     printf("\n\nOut of memory - terminating.\n\n");
     exit(1);
   }
}




void fill_node(NODE *new_node)
{
  printf("\nEnter an integer ");
  get_int(&(*new_node).info);

  (*new_node).link = NULL;
}

/* useful but optional */

int is_empty(NODE *first)
{
  if (first == NULL)
    return(TRUE);
  else
    return(FALSE);
}


void print_node(NODE *node)
{
  printf("%d  ", (*node).info);
}
 
void print_list(NODE *start)
{
  if (is_empty(start) == TRUE)
  {
    printf("\nList is empty.\n");
  }
  else
  {
    printf("\n");
    while(start != NULL)
    {
      print_node(start);
      start = (*start).link;
    }
  }
}