linked lists question
Nov 8, 2011 at 10:52am UTC
i'm having trouble inserting/printing data from my linked list, i'm not sure if the data is going into the list or there is a problem with my print function...any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#ifndef ListOfDoubles_H
#define ListOfDoubles_H
class ListOfDoubles
{
private :
struct node
{
double data;
node *next;//pointer to next node
}
*head;//first node in list
public :
ListOfDoubles();
void insert(double number);
void display();
double deleteMostRecent();
int count();
//~ListOfDoubles();
};
#endif;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "ListOfDoubles.h"
#include<iostream>
using namespace std;
ListOfDoubles::ListOfDoubles()
{
head = NULL;
}
void ListOfDoubles::insert(double num)
{
node *tempNode, *newNode;
if (head == NULL)//if there's no nodes
{
newNode = new node;
newNode->data = num;
newNode->next = NULL;
}
else
{
tempNode = head;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
newNode = new node;
newNode->data = num;
newNode->next = NULL;
tempNode->next = newNode;
}
}
void ListOfDoubles::display()
{
node *tempNode = head;
while (tempNode != NULL)
{
cout << tempNode->data;
tempNode = tempNode->next;
}
/* a differant print function i tried
node *tempNode;
for(tempNode = head; tempNode != NULL; tempNode = tempNode->next)
{
cout << tempNode->data << endl;
}
*/
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "ListOfDoubles.h"
#include<iostream>
using namespace std;
int main()
{
ListOfDoubles list;
list.insert(3);
list.insert(4);
list.insert(5);
list.insert(6);
list.display();
system("pause" );
return 0;
}
Nov 8, 2011 at 1:10pm UTC
You never update 'head'
Nov 9, 2011 at 11:34am UTC
thanks.
Topic archived. No new replies allowed.