DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   I need help with a linked list program (http://www.daniweb.com/forums/thread15772.html)

the b Dec 18th, 2004 11:30 am
I need help with a linked list program
 
I am writing a program that uses a singly linked list. The program is suposed to ask the user for the number of days they have temperatures for and for the corresponding temperature. I seem to be having a problem with the get_temp function. When I run the program it asks for the number of days and for the temperatures but when it comes to the point where it is supposed to display them it displays the last day, and the first temperature only.
Code:
#include <iostream.h>

struct temperature_node
{
        int day;
        float temperature;
        temperature_node *next;
};

int day_counter = 0;

temperature_node *head_ptr;
temperature_node *current_ptr;

int get_temp(int days, float &temp);
void show_temps();
void remove_temps();
void add_temp(int days, float temp);
void move_c_to_end();

int main()
{
        int days;
        float temp;

        cout << "enter the days for which you have data ";
    cin >> days;

        if(get_temp(days, temp))
        {
                head_ptr = new temperature_node;
                head_ptr->day = days;
                head_ptr->temperature = temp;
                head_ptr->next = NULL;
        while(get_temp(days, temp))
                {
                        add_temp(days, temp);
                }
        show_temps();
        remove_temps();
        }
return 0;
}

int get_temp(int days, float &temp)
{
        int keep_data = 1;

    if(day_counter == days)
        {
                keep_data = 0;
        }
        else
        {
                days = day_counter + 1;
                cout << days;
                cout << "enter the temperature for day " << day_counter + 1 << endl;
                cin >> temp;
                day_counter++;
        }
        return(keep_data);
}

void add_temp(int days, float temp)
{
        temperature_node *new_rec_ptr;
        new_rec_ptr = new temperature_node;

        new_rec_ptr->day = days;
        new_rec_ptr->temperature = temp;
        new_rec_ptr->next = NULL;

        move_c_to_end();
        current_ptr->next = new_rec_ptr;
}

void move_c_to_end()
{
        current_ptr = head_ptr;

        while(current_ptr->next != NULL)
        {
                current_ptr = current_ptr->next;
        }
}

void show_temps()
{
        current_ptr = head_ptr;

        do
        {
                cout << current_ptr->day << "        ";
                cout << current_ptr->temperature << endl;
        } while(current_ptr != NULL);
}

void remove_temps()
{
        temperature_node *temporary_ptr;

        current_ptr = head_ptr;

        do
        {
                temporary_ptr = current_ptr->next;
                delete current_ptr;
                current_ptr = temporary_ptr;
        }while(temporary_ptr != NULL);
}

vegaseat Dec 18th, 2004 1:04 pm
Re: I need help with a linked list program
 
show_temps() does not walk through the list!

Chainsaw Dec 18th, 2004 5:19 pm
Re: I need help with a linked list program
 
Ya, you need a

current_ptr = current_ptr->next;

in there...

the b Dec 19th, 2004 5:56 pm
Re: I need help with a linked list program
 
Quote:

Originally Posted by Chainsaw
Ya, you need a

current_ptr = current_ptr->next;

in there...

Thank you that helped me out alot :)


All times are GMT -4. The time now is 7:08 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC