/************************************************\
* Filename: linkedlist.c *
* Author: Sue Bogar *
* Date Written: 11/17/98 *
* Description: This file contains the functions *
* necessary to work with a linked list. *
* This set of functions provide the operations *
* needed including creation of a node, insertion,*
* deletion, determining if a list is empty and *
* the printing of the list. *
\************************************************/
#include
#include
#include "linkedlist.h"
/******************
* CreateNode mallocs the space needed to
* hold a struct of type node, initializes
* the members, and returns a pointer to
* the new node.
******************/
nodePtr CreateNode (void)
{
nodePtr newNode;
/* Get the space needed for the node */
newNode = (nodePtr) malloc (sizeof(node));
if (newNode == NULL)
{
fprintf (stderr, "Out of Memory - CreateNode\n");
exit (-1);
}
/* Initialize the members */
newNode -> data = 0;
newNode -> next = NULL;
return newNode;
}
/******************
* GetData gets the value of data
* from the user and puts it into the
* node pointed to by the nodePtr it
* receives as an argument.
******************/
void GetData (nodePtr temp)
{
int value;
printf ("\nEnter the value of data : ");
scanf ("%d", &value);
temp -> data = value;
}
/******************
* Insert takes a pointer to a nodePtr, the
* address of head, and a nodePtr, temp as
* arguments. temp is a pointer to the
* node to be inserted. The node is then
* inserted into the list at the end.
******************/
void Insert (nodePtr* headPtr, nodePtr temp)
{
nodePtr prev, curr;
if ( IsEmpty (*headPtr))
{
*headPtr = temp;
}
else
{
prev = NULL;
curr = *headPtr;
/* traverse the list until the end */
while (curr != NULL)
{
prev = curr;
curr = curr -> next;
}
/* insert the node, temp, at the end */
prev -> next = temp;
}
}
/******************
* Delete takes a pointer to a nodePtr as its
* first argument, which is the address of
* head. Its second argument is the value of
* data that is to be removed. This function
* traverses the list until the node containing
* the target value is found. That node is then
* deleted from the list and the value returned.
* If the target value is not in the list an
* error message is printed and -1 is returned.
******************/
int Delete (nodePtr* headPtr, int target)
{
int value;
nodePtr temp, prev, curr;
if (IsEmpty (*headPtr))
{
printf ("Can't delete from an empty list\n");
return (-1);
}
/* if the target value is the first
in the list, move head */
else if (target == (*headPtr) -> data)
{
temp = *headPtr;
value = (*headPtr) -> data;
*headPtr = (*headPtr) -> next;
free (temp);
return (value);
}
/* traverse the list until the
target value is found */
else
{
prev = *headPtr;
curr = (*headPtr) -> next;
while (curr != NULL &&
curr -> data != target)
{
prev = curr;
curr = curr -> next;
}
if(curr != NULL)
{
/* delete the node the contains
the target value */
temp = curr;
prev -> next = curr -> next;
value = curr -> data;
free(temp);
return (value);
}
else
{
printf("%d was not in the list\n", target);
return (-1);
}
}
}
/******************
* IsEmpty takes a nodePtr as its first
* argument, which is a pointer to the list,
* known as head. It determines whether the
* list is empty or not and returns 1 (true)
* if the list is empty and 0 (false) if it
* is not empty.
******************/
int IsEmpty (nodePtr head)
{
/* If the pointer to the list is
NULL then there is no list. The
list is empty, so we return true.
*/
if(head == NULL)
{
return 1;
}
else
{
return 0;
}
}
/******************
* PrintList takes a nodePtr as an argument
* which is a pointer to the list, known as
* head. The list is traversed and the value
* of the data member of each node is printed.
******************/
void PrintList (nodePtr head)
{
nodePtr curr;
if (IsEmpty (head))
{
printf ("The list is empty\n");
}
else
{
/* set the current pointer to the first
node of the list */
curr = head;
/*Until the end of the list*/
while (curr != NULL)
{
/* print the current data item */
printf("%d ", curr -> data);
/* move to the next node */
curr = curr -> next;
}
printf ("\n");
}
}
The Driver
#include
#include "linkedlist.h"
main ()
{
int num, value;
nodePtr head, temp;
head = NULL;
temp = CreateNode();
GetData (temp);
Insert (&head, temp);
PrintList (head);
temp = CreateNode();
GetData (temp);
Insert (&head, temp);
PrintList (head);
temp = CreateNode();
GetData (temp);
Insert (&head, temp);
PrintList (head);
printf("\nEnter a value to be deleted :");
scanf ("%d", &num);
value = Delete(&head, num);
printf("The value %d was returned\n", value);
PrintList (head);
printf("\nEnter a value to be deleted :");
scanf ("%d", &num);
value = Delete(&head, num);
printf("The value %d was returned\n", value);
PrintList (head);
printf("\nEnter a value to be deleted :");
scanf ("%d", &num);
value = Delete(&head, num);
printf("The value %d was returned\n", value);
PrintList (head);
printf("\nEnter a value to be deleted :");
scanf ("%d", &num);
value = Delete(&head, num);
printf("The value %d was returned\n", value);
PrintList (head);
printf("\nEnter a value to be deleted :");
scanf ("%d", &num);
value = Delete(&head, num);
printf("The value %d was returned\n", value);
PrintList (head);
}
The Output
retriever[102] a.out
Enter the value of data : 6
6
Enter the value of data : 9
6 9
Enter the value of data : 3
6 9 3
Enter a value to be deleted :9
The value 9 was returned
6 3
Enter a value to be deleted :3
The value 3 was returned
6
Enter a value to be deleted :8
8 was not in the list
The value -1 was returned
6
Enter a value to be deleted :6
The value 6 was returned
The list is empty
Enter a value to be deleted :5
Can't delete from an empty list
The value -1 was returned
The list is empty
retriever[103]