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);
void get_memory(NODE **new_node);
void fill_node(NODE *new_node);
void create_list(NODE **first, NODE *new_node);
void add_node(NODE **first, NODE *new_node);
void delete_node(NODE **first, int value);
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);
fill_node(new_node);
if (is_empty(first) == TRUE)
create_list(&first, new_node);
else
add_node(&first, new_node);
break;
case DELETE: printf("\nNot implemented yet.\n");
break;
case PRINT: print_list(first);
break;
case QUIT: printf("\nQuitting program.\n\n");
break;
default: printf("\nInvalid entry for option - try again\n");
}
}
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 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 */
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;
}
}
}