So I want to implement three different functions into my header file. I am working on the first one but I am unsure where to begin. Here is my current header file:
#ifndef LINKLIST_H
#define LINKLIST_H
class NumberList
{
private:
//Declare a structure for the list
struct ListNode
{
int value; //The value in this node
struct ListNode *next; //The values in the next node
};
ListNode *head;
public:
//Constructor
NumberList()
{
head = NULL;
}
//Destructor
~NumberList();
//Link list functions
void appendNode(int);
void insertNode(int);
void deleteNode(int);
void displayList() const;
void deleteNodeAll(int);
bool listSorted() const;
void deleteList();
void listAddEvenMultiplyOdd(); //Implement this function
void listOddPositions(); //Implement this function
void listDivisibleBy5or10(); //Implement this function
};
NumberList::~NumberList()
{
ListNode *nodePtr; //to traverse the list
ListNode *nextNode; // to point to the next node
//Position nodePtr at the head of the list
nodePtr = head;
//while the node pointer is not at the end of the list
while(nodePtr != NULL)
{
//save a pointer to the next node
nextNode = nodePtr -> next;
// Delete the current Node
delete nodePtr;
//position nodePtr at the next node
nodePtr = nextNode;
}
}
void NumberList::appendNode(int num)
{
ListNode *newNode; //To point to a new node
ListNode *nodePtr; //move through the list
//Allocate a new node and store num there
newNode = new ListNode;
newNode -> value = num;
newNode -> next = NULL;
//If there are no nodes in the list
//make newNode the first node.
if(!head)
head = newNode;
else //insert newNode at the end
{
// Intialize NodePtr to head of the list
nodePtr = head;
//Find the last node in the list
while ( nodePtr->next)
nodePtr= nodePtr -> next;
//insert newNode as the last node
nodePtr -> next= newNode;
}
}
void NumberList::displayList() const
{
ListNode *nodePtr; //To move through the list
//Position nodePtr at the head of the list
nodePtr = head;
//while nodePtr points to a node, traverse the list
while (nodePtr)
{
//Display the value in this node
std::cout<< nodePtr->value <<" ";
//Move to the next node
nodePtr = nodePtr -> next;
}
std::cout<< std::endl;
}
void NumberList::deleteNode(int num)
{
ListNode *nodePtr; //To traverse the list
ListNode *previousNode; //To point to the previous node
//If the list is empty, do nothing
if(!head)
return;
//Determine if the first node is the one to be deleted
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
//Intialize nodePtr to head of list
nodePtr = head;
//Skip all nodes whose value member is not equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If nodePtr is not at the end of the list, link the previous
// node to the node after nodePtr, then delete nodePtr
if(nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
void NumberList::insertNode(int num)
{
ListNode *newNode; //A new Node
ListNode *nodePtr; //To traverse the list
ListNode *previousNode = NULL; //The previous node
//Allocate a new node and store num there
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
//If there are no nodes in the list make newNode the first node
if(!head)
{
head = newNode;
}
else //otherwise, insert newNode
{
//Position modePtr at the head of the list
nodePtr = head;
//Intialize previousNode to NULL
previousNode = NULL;
//Skip all nodes whose value is less than num
while(nodePtr != NULL && nodePtr->value <num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If the new node is to be 1st in the list, Insert it before all other nodes.
if(previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void NumberList::deleteNodeAll(int num)
{
ListNode *nodePtr; //To traverse the list
ListNode *previousNode; //To point to the previous node
//If the list is empty, do nothing
if(!head)
return;
//Determine if the first node is the one to be deleted
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
//Intialize nodePtr to head of list
nodePtr = head;
while(nodePtr !=NULL)
{
//Skip all nodes whose value member is not equal to num
while (nodePtr !=NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If nodePtr is not at the end of the list, link the previous
// node to the node after nodePtr, then delete nodePtr
if(nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
nodePtr= previousNode->next;
}
}
}
bool NumberList::listSorted() const
{
ListNode *nodePtr; //To traverse the list
ListNode *previousNode; //To point to the previous node
//If the list is empty, do nothing
if(!head)
returntrue;
if(head->next ==NULL)
returntrue;
//Intialize nodePtr to head of list
nodePtr = head->next;
previousNode = head;
//Skip all nodes whose value member is not equal to num
while (nodePtr !=NULL && previousNode->value < nodePtr->value )
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If nodePtr is not at the end of the list, link the previous
// node to the node after nodePtr, then delete nodePtr
if(nodePtr)
{
returnfalse;
}
elsereturntrue;
}
void NumberList::deleteList()
{
ListNode *nodePtr; //To traverse the list
ListNode *nextNode; //To point to the previous node
//If the list is empty, do nothing
if(!head)
return;
if(head->next ==NULL)
{
delete head;
head = NULL;
return;
}
//Intialize nodePtr to head of list
nodePtr = head;
//Skip all nodes whose value member is not equal to num
while (nodePtr !=NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
head=NULL;
return;
}
How do I actually begin getting somewhere with the first of the three functions?
How do I actually begin getting somewhere with the first of the three functions?
listAddEvenMultiplyOdd
Perhaps you could start by telling us exactly what a function with the name listAddEvenMultiplyOdd()
is supposed to do ... and with what.
Remote guess ... add up the even numbers and multiply together the odd? Well you have written a function void NumberList::displayList() const to traverse the list (haven't you?). If my wild guess is correct you could modify that to add up the even numbers and multiply the odd numbers as you went along ... if, of course, that was what you intended to do.
BTW, your routines aren't templated, so there's no reason for them to be in a header file.
If you're not doing homework, you shouldn't be writing your own linked list; no one does that in C++. Use the one from the standard library. In this case it can be:
If you're not doing homework, you shouldn't be writing your own linked list; no one does that in C++. Use the one from the standard library. In this case it can be:
I made a mistake. My professor uploaded this like that. I just have to add three different functions to it. I am working on the first function right now. This is what I have so far but it doesn't work with the source file:
void listAddEvenMultiplyOdd()
{
int ListNode *currentNode; //Pointer for current value in the link list
int ListNode *nextNode; // Pointer for the next value in the link list
int listOdd =0;
int listEven = 0;
if(isEmpty()) // checks for an empty link list
{
std::cout<<"The Link list is empty \n"; // tells user that the link list is empty
}
else
{
currentNode = top;
while(currentNode!= NULL)
{
if(currentNode->value%2 == 0)
{
listEven += 4;
}
if(currentNode->value%2 == 1)
{
listOdd *= 5;
}
currentNode = currentNode->next; // shows that the node
// after the current node is next
}
}
std::cout<<listOdd <<"\n";
std::cout<< listEven <<"\n";
}
Anyway, what do you think multiplying 0 by 5 a bunch of times is going to accomplish?
You said you wanted to add 4 to even values and multiply odd values by 5.
Obviously you aren't doing that.
That's what he wrote and that's the tasks he assigned. He wants us to add four to the even values and multiply the odd values by 5. I don't know what my code is doing currently. That's why I am asking for guidance.
You don't need listEven or listOdd at all. You are adding 4 to, or multiplying by 5, the variable currentNode->value. Simply traverse the list (roughly in the way you are doing) and change each currentNode->value as you get to it. No idea why you wrote "top": it's called "head".
BTW, where were 4 and 5 in your original question? You sent several of us completely the wrong way by failing to state that and leaving us to guess from the name of the function.
Make sure that you address all of doug4's points (except possibly his number 2, now you have actually explained the task).
@lastchance:
I just got that 4 and 5 clarified because I was a little confused myself.
@doug4:
My professor wants it in a header file that's why they're in a header file and not a .cpp file. It is automatically a fail if I put it in a .cpp file. Thank you. I will try to figure out the function.
void NumberList::listOddPositions(struct ListNode *head)
{
if(!head) // checks for an empty link list
{
std::cout<<"The list is empty \n"; // tells user that the list is empty
}
struct ListNode *temp=head;
while(temp!=NULL)
{
temp->data = temp->data*10; //multiply values at odd position by 10
temp = temp->next;
//skip odd position values
if(temp!= NULL)
temp = temp->next;
}
}
If you see the time. I edited it at 8:13 and your response came at 8:14 so I don't know where the confusion came in at. If you don't want to help then fine. Hopefully someone else will be willing to help on a forum that people are supposed to be able to come to for help.
If you see the time. I edited it at 8:13 and your response came at 8:14 so I don't know where the confusion came in at.
Because @Furry Guy probably started writing his response at 8:12. You ninja'd him by changing your code while he was responding. Now his response doesn't make any sense.