linked list, help needed please
Nov 12, 2011 at 9:35pm UTC
i've to code my own linked list of doubles with insert, deleteMostRecent, display and count functions. i've been following lecturer notes but am getting some errors and can't get the program to run.
1) it's saying i have 'head' as undeclared. i have it declared though in DoubleList.h ??
2) error C4430: missing type specifier - int assumed. Note: C++ does not support default-int - another issue with 'head' in DoubleList.h
here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef DoubleListNode_H
#define DoubleListNode_H
class DoubleListNode
{
friend class DoubleList;
public :
DoubleListNode();
private :
double data;
DoubleListNode *next;
};
#endif;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef DoubleList_H
#define DoubleList_H
class DoubleList
{
public :
DoubleList();
//~DoubleList();
void insert(double num);
void display();
double deleteMostRecent();
int count();
private :
DoubleListNode *head;
};
#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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include"DoubleList.h"
#include"DoubleListNode.h"
#include<iostream>
using namespace std;
DoubleList::DoubleList()
{
head = NULL;
}
void DoubleList::insert(double num)
{
DoubleListNode *tempNode, *newNode;
if (head == NULL)
{
newNode = new DoubleListNode;
newNode->data = num;
newNode->next = NULL;
head = newNode;
}
else
{
tempNode = new DoubleListNode;
tempNode = head;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
newNode = new DoubleListNode;
newNode->data = num;
newNode->next = NULL;
tempNode->next = newNode;
}
}
void DoubleList::display()
{
DoubleListNode *tempNode;
tempNode = new DoubleListNode;
tempNode = head;
while (tempNode != NULL)
{
cout<<tempNode->data<<endl;
tempNode = tempNode->next;
}
}
double DoubleList::deleteMostRecent()
{
DoubleListNode *leadPtr, *trailPtr;
leadPtr = new DoubleListNode;
leadPtr = head;
trailPtr = new DoubleListNode;
trailPtr = NULL;
if (head == NULL)
{
return NULL;
}
else if (leadPtr->next == NULL)
{
double value = leadPtr->data;
delete leadPtr;
head = NULL;
return value;
}
else
{
while (leadPtr->next != NULL)
{
leadPtr = leadPtr->next;
}
trailPtr = leadPtr->next;
double value = leadPtr->data;
delete leadPtr;
trailPtr->next = NULL;
return value;
}
}
int DoubleList::count()
{
DoubleListNode *tempNode;
tempNode = new DoubleListNode;
int count = 0;
for (tempNode = head; tempNode != NULL; tempNode = tempNode->next)
count++;
return count;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include"DoubleList.h"
#include"DoubleListNode.h"
#include<iostream>
using namespace std;
int main()
{
DoubleList list;
cout<<"Num items: " <<list.count()<<endl;
list.insert(1);
list.insert(2);
list.insert(3);
cout<<"Num items: " <<list.count()<<endl;
system("pause" );
return (0);
}
could someone with a good knowledge of linked list please tell me where i'm going wrong? would be much appreciated, thanks.
Nov 13, 2011 at 1:01pm UTC
can anyone give a bit of help??? please!
Topic archived. No new replies allowed.