Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ Linked List Problems?

Any help would be appreciated!!! Thanks

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

Ok so now I have some numbers coming up at least, but now I need to transverse the list to total the numbers, count the number of

nodes, and average to the screen.

I asked this question on here already but this is in C++ and we cant use <list>, <iterator>, or anything like that. We have to Build it from scratch. Here is my code right 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

Ok so now I have some numbers coming up at least, but now I need to transverse the list to total the numbers, count the number of

nodes, and average to the screen. Here is my revised code.

*/

#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 numbers);

void move_current_to_end();

int CountNodes( );

void display_list();

void delete_list();

int main()

{

while(get_user_data());

display_list(); // display the countries and population

return 0;

}

int get_user_data () // Get data from user

{

float input = 1;

if (input != 0)

{

cout << "Please insert a number: " << endl;

cin >> input;

add_node(input);

cout << endl; // Blank line

}

else

{

input = 0;

}

return (input);

}

void add_node (float numbers)

{

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 = numbers;

temp->next = NULL; //new node ptr to null (end of list)

if(head_ptr == NULL)

head_ptr = temp;

else

{

//traverse list and find the end

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 << endl;

current_ptr = current_ptr->next; // point current_ptr to next node

}while (current_ptr != NULL); // loop until end of list

// Begin Node Calculation

cout << "Number of Nodes: ";

cout << endl << endl;

cout << CountNodes(); // calling function to print # of nodes

cout << endl << endl;

}

// function for calculating the amount of nodes

int CountNodes( )

{

int numberOfNodes = 0;

current_ptr = head_ptr;

if(head_ptr == NULL)

return 0;

while ( current_ptr != NULL )

{

numberOfNodes ++; // increment total of nodes

current_ptr = current_ptr->next;

}

return numberOfNodes;

}

void delete_list()

{

node *temp_ptr; // pointer used for temporary storage

current_ptr = head_ptr; // move current_ptr to head of the list

do // transverse list

{

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);

}

Update:

That would be amazing if you emailed it to me! Thank you so much!

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Your program does not run properly.. its accepting negative numbers (you should stop taking input when a negative number is encountered )..your program stops taking input when 0 is the input.

    But it prints the right number of nodes.. to print the total create a double sum =0 ; and add all the numbers to it while traversing. to print average..divide the sum by total number of nodes. something like :

    node *temp = head;

    while(temp!=NULL)

    {

    sum+=temp->value;

    temp=temp->next;

    }

    so ur total is sum...average is sum divided by the number of nodes which you already have.

    and to stop taking input when a negative number is entered..you can use a while loop and have a cin statement inside it

    I have edited your code and it works perfectly fine (as to what i understand of the problem)..but for some reason i cant paste the code here because its exceeding the character limit.. if you want i can email it to you!

  • Anonymous
    5 years ago

    Your debugger is mendacity to you, or you're no longer expertise what that's showing you. You *DO* circulate into the else area (you could desire to. You the two circulate into the if area or the else area. There are no longer the different alternatives). yet once you get into the else area, your for loop won't execute. Your debugger could tutor you combating on the for loop, then skipping over the physique of it. Why does it pass over? Your for loop attempt is: temp->next != NULL yet temp is pointing to the 1st node interior the appropriate checklist. the subsequent pointer ought to be NULL, there is not any 2nd node interior the checklist at this factor. keep in mind, C/C++ tests the difficulty (temp->next != NULL) formerly it executes the physique of the for loop. What you attempt to do interior the for loop is locate the final node interior the appropriate checklist. this is the node with a next pointer of NULL. yet you will no longer be conscious of which you have got here upon it till you walk by way of all of the nodes. That comes AFTER the for loop, no longer interior of it. think of roughly your good judgment right here.

Still have questions? Get your answers by asking now.