View Poll Results: What can I do different about my code to make it better
- Voters
- 0. You may not vote on this poll
-
December 10th, 2010, 07:50 PM
#1
C++ Linked List Issues
I am having trouble with my linked list. My teacher wants us to add the total number of nodes, the total from the user input (example: if the user enters 5, 5, ,5 she wants the results displayed as 15. As far as counting the nodes I have no idea how to implement it inside my code or the math to figure it out. If someone can help me I would greatly appreciate it! Here is my code as of now:
/*
1. Write a program that allows the user to enter a series of positive
floating-point numbers.
2. Store the numbers in a linked list.
3. Allow the user to enter numbers until a negative number is entered. (terminate when a negative # is entered).
4. Next, transverse the list to total the numbers entered, number of nodes, and average to the screen
*/
#include <iostream>
#include <iomanip>
using namespace std;
struct node
{
float value;
node *next;
};
node *head_ptr = NULL;
node *current_ptr = NULL;
int get_user_data ();
void add_node (float number);
void move_current_to_end();
void display_list();
void delete_list();
int main()
{
float numbers;
while(get_user_data());
display_list(); // display the users input and total
return 0;
}
int get_user_data ()
{
float input = 1;
if (input != 0)
{ // Display details for what temp points to
cout << "Please insert a number: " << endl;
cin >> input;
add_node(input);
cout << endl; // Blank line
}
else
{
input = 0;
}
return (input);
}
void add_node (float number)
{
node *temp, *temp2; // declare local pointer for new node
temp = new node; // allocate memory for a new node and
// initialize pointer to point to it
temp->value = number;
temp->next = NULL; //new node ptr to null (end of list)
if(head_ptr == NULL)
head_ptr = temp;
else
{
temp2 = head_ptr; //start at head of list
while (temp2->next != NULL)
{
temp2 = temp2->next; //advance to next node
}
temp2->next = temp; //found the end, append temp (the new node)
}
}
void move_current_to_end()
{
current_ptr = head_ptr; // move current_ptr to head of the list
while (current_ptr->next!= NULL)
{
// transverse list until NULL is reached
current_ptr = current_ptr->next;
}
}
void display_list()
{
current_ptr = head_ptr; // move current_ptr to head of list
cout << "Numbers" << endl;
do
{
cout.setf(ios::left);
cout << setw(5) << current_ptr->value << " - " << endl;
current_ptr = current_ptr->next; // point current_ptr to next node
} while (current_ptr != NULL); // loop until end of list
cout<<endl;
}
void delete_list()
{
node *temp_ptr; // pointer used for temporary storage
current_ptr = head_ptr; // move current_ptr to head of the list
do
{
temp_ptr = current_ptr->next; // set temp pointer to point
// to the remainder of the list
delete current_ptr; // delete current
current_ptr = temp_ptr;
} while (temp_ptr != NULL);
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|