UMBC CS 201, Fall 05
 UMBC CMSC 201 Fall '05 CSEE | 201 | 201 F'05 | lectures | news | help Search Notes:

## The linked list implementation of a list

A linked list is a linear collection of self-referential structures, called nodes, connected by pointer links. A linked list is accessed by keeping a pointer to the first node of the list. This pointer to the first node of a list is typically named head. Subsequent nodes are accessed via a link pointer member that is stored in each node.

The operations on linked lists include :

• creation of a node
• putting information into the node
• inserting a node into the list
• deleting a node from the list
• is the list empty
• print the list
• etc.

A node is a structure that has whatever members are needed to hold the data, and a member that is pointer to a structure of the same type, the link. Since this pointer typically is a pointer to the next item in the list, it is traditionally called next.

One possible interface for a linked list

```/********************************************************\
* Author:    Sue Bogar                                   *
* Written:   11/17/98                                    *
* Last Modification: 9/27/05                             *
* Section:   201staff                                    *
* Email:     bogar@cs.umbc.edu                           *
*                                                        *
* This file contains the structure definition of a node, *
* and typedefs of the types NODE and NODEPTR.  It also   *
* contains the function prototypes for the functions     *
* implementation of a list abstract data type.           *
\********************************************************/

/* NODEPTR is defined as an alias for
** "struct node *" so it can be used within
** the structure before it is completely defined
*/

typedef struct node * NODEPTR;

/* A NODE is a structure that allows us to build a linked
** list since it has both a data portion and a NODEPTR as
** its members.  The NODEPTR called next is known as the
*/
typedef struct node
{
int     data;
NODEPTR next;    /* OR struct node *next; */
}NODE;

/**************************************
** Function: CreateNode
** Input:    none
** Output:  memory for a node is malloced
**           and a pointer (nodePtr) to the memory
**           is returned to the user
** Note:
**      CreateNode initializes the members
**      CreateNode exits if there is insufficient
**      memory
**************************************/
NODEPTR CreateNode (void);

/**************************************
** Function: SetData
** Input:    a pointer to a node (NODEPTR), and
**           the value to be placed into the node
** Output:   the data member of the node pointed to
**           is populated with the value passed in
**
**************************************/
void    SetData    (NODEPTR temp, int data);

/**************************************
* Function: Insert
* Input:    a pointer to a pointer to the head of the
*           a pointer to the node (NODEPTR) to be
*             inserted
* Output:   the node is inserted at the end of the
*           there is no return value
**************************************/
void    Insert     (NODEPTR* headPtr, NODEPTR temp);

/**************************************
* Function: Delete
* Input:    a pointer to a pointer to the head
*           an integer (target) containing the value
*             of the data in the node to be deleted
* Output:   if found, the first node with the corresponding
*             value (target) is deleted from the
*           if the node is deleted, the value found
*             in the deleted node (NOT target) is
*             returned
*           if no node is deleted, an error is
*             printed and -1 is returned
**************************************/
int     Delete     (NODEPTR* headPtr, int data);

/**************************************
* Function: IsEmpty
* Output:   returns 1 (true) if the list is empty
*           returns 0 (false) if the list is not empty
*********************************************/

/**************************************
* Function: PrintList
* Input:    a pointer to the head of the list
* Ouput:    each node in the list is printed according
*             to the format specified in this code
**************************************/