ooooooooooops...........sorry.......forgot the code tags....
Code:
// Compiler: Microsoft Visual C++ (ver. 6.0)
//
// Task: Write a program to do the following:
// The user will be asked to enter a list of integer values.
// Each value will be stored in a struct data structure.
// The structs will be put into a linked list.
// Show the linked list after it has been loaded.
// The user will be asked to choose one value to remove from the list
// Show the linked list after the value has been removed
// Show the average of the integers in the list.
//
// Format of output
//
// enter integer values; use 0 to quit
//
// 1 2 3 4 5 6 7 8 9
//
// HEAD->1->2->3->4->5->6->7->8->9->NULL
//
// average is: 5
//
// which value do you want to delete? 8
//
// HEAD->1->2->3->4->5->6->7->9->NULL
//
// average is: 4.625
//
// Last Updated: 11/22/2000
//
/////////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------------
// Preprocessor directives
#include<iostream>
using namespace std;
//---------------------------------------------------------------------------------------
// Function prototypes
void create_list();
void show_list();
void remove_value(int);
void average();
//---------------------------------------------------------------------------------------
// Declaration of a node
struct node
{
int value;
node *next; // pointer to next node in the list...
};
//---------------------------------------------------------------------------------------
// Global variables
node *head = NULL; // global pointer to the first node in the list
//---------------------------------------------------------------------------------------
// Driver function
int main()
{
create_list();
int remove;
cout << "Which value do you want to remove from the list? ";
cin >> remove;
cout << endl;
remove_value(remove); // will delete variable (remove) from the list
return 0;
}
//---------------------------------------------------------------------------------------
// create_list(): creates a linked list
//
// Parameters: none
// Return Value: void
void create_list()
{
node *cur, *new_node;
int temp; // temporary variable for (int) value of node
new_node = new node; // creates a node on the heap
head = new_node; // head will point to the first node in
// the list
cur = head;
cout << "Please enter an integer, use 0 (zero) to quit: \n\n";
cin >> temp;
if(temp != 0)
{
cur->value = temp;
}
while(temp != 0)
{
new_node = new node; // keeps on adding nodes to the list
cur->next = new_node; // as long as the user won't enter 0 (zero)
cur = new_node;
cin >> temp;
if(temp != 0)
{
cur->value = temp;
}
}
cur->next = NULL; // last's nodes pointer (next) must point
// to NULL
show_list(); // will display contents of a list to
// the screen
average(); // will display average of all values onto
// the screen...
cur = NULL;
new_node = NULL;
return;
}
//---------------------------------------------------------------------------------------
// show_list(): will output the contents of a linked lists to the screen
//
// Parameters: none
// Return Value: void
void show_list()
{
cout << endl;
node *cur;
cur = head;
cout << "HEAD->";
while(cur->next != NULL)
{
cout << cur->value << "->";
cur = cur->next;
}
cout << "NULL \n";
return;
}
//---------------------------------------------------------------------------------------
// remove_value(): will remove the desired value from the linked list
//
// Parameters: integer (value whose node will be removed from the list)
// Return Value: void
void remove_value(int remove)
{
node *cur;
node *prev;
cur = head;
if(cur->value == remove) // if the desired (int) to remove is the
{ // the first item in the list...
cur = head;
head = head->next;
delete cur; // ** special case **
cur = NULL;
}
else
{
while( (cur->value != remove) && (cur->next != NULL) )
{
prev = cur;
cur = cur->next;
if(cur->value == remove)
{
prev->next = cur->next; // will exit the loop at first occurence
delete cur; // of desired (user-inputed) variable
cur = NULL; // called (remove)...
prev = NULL;
break;
}
}
}
show_list(); // diplays contents of a new node
average(); // diplays average of all values
return;
}
//---------------------------------------------------------------------------------------
// average(): displays the average of all integers that linked list contains
//
// Parameters: none
// Retur Value: void
void average()
{
cout << endl;
node *cur;
cur = head;
double average;
int sum = 0;
int counter = 0;
while(cur->next != NULL) // traverses the entire linked list
{
sum = sum + cur->value;
counter++;
cur = cur->next;
}
average = ( (static_cast<double>(sum)) / counter);
cout << "The average of values is: " << average << "\n\n";
return;
}
//---------------------------------------------------------------------------------------