Making your Own linked list

Hey guys, I have to design my own linked link class that will hold a series of figures and this is my program:

# include < iostream>
using namespace std;


class LinkedList
{
private:
struct ListNode
{
double value;
struct ListNode *next;
};

ListNode * head;

public:
LinkedList()
{
head = NULL;
}

~LinkedList();

void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;

};


void LinkedList::appendNode(double num)
{
ListNode *newNode;
ListNode *nodePtr;

newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;

if (!head)
{
head = newNode;
}

else
{
nodePtr = head;

while (nodePtr->next)
nodePtr = nodePtr->next;

nodePtr->next = newNode;
}

}


void LinkedList::displayList() const
{
ListNode *nodePtr;

nodePtr = head;

while (nodePtr)
{
cout << nodePtr->value << endl;

nodePtr = nodePtr->next;
}
}


void LinkedList::insertNode(double num)
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode = NULL;

newNode = new ListNode;
newNode->value = num;

if (!head)
{
head = newNode;
newNode->next = NULL;

}

else
{
nodePtr = head;

previousNode = NULL;

while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}

else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}

}
}


void LinkedList::deleteNode(double num)
{
ListNode *nodePtr;
ListNode *previousNode;

if (!head)
return;

if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}

else
{
nodePtr = head;

while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}


LinkedList::~LinkedList()
{
ListNode *nodePtr;
ListNode *nextNode;

nodePtr = head;

while (nodePtr != NULL)
{
nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;
}
}


int main()
{
LinkedList list;

list.appendNode(2);
list.appendNode(4);
list.appendNode(6);
list.appendNode(8);
list.appendNode(10);

cout << " Here are the values displayed!" << endl;
list.displayList();
cout << endl;

cout << " Now I am going to insert 5 into the values" << endl;
list.insertNode(5);

cout << " Here is the values now" << endl;
list.displayList();
cout << endl;

cout << " I am now deleteing the 6 value" << endl;
list.deleteNode(6);

cout << " Here are the remaining nodes" << endl;
list.displayList();

cout << " That is the Program! " << endl;

system("pause");
return 0;


}

Now when I try running it this is the error I get:

* error C4703: potentially uninitialized local pointer variable 'previousNode' used

Can anyone see where this error is coming from and How can I fix it?

Thanks guys
The only instance of a potentially uninitialized local pointer is in LinkedList::deleteNode(double). If nodePtr != NULL but nodePtr->value == num, the loop won't be run, the if statement will run, and previousNode will not have been assigned a value.

Please please please in the future use code tags. You can get them by clicking the little <> button to the right of the text box you make your posts in, and putting your code in between them.

Happy coding.

-Albatross

hmm..How can I fix it than? I'm pretty sure I wrote it correctly but I'm not sure
Here's your deleteNode code with code tags, indented and slightly reformatted to save space. See comments below.
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
void LinkedList::deleteNode(double num)
{
  ListNode *nodePtr;
  ListNode *previousNode;

  if (!head)
    return;

  if (head->value == num) {
    nodePtr = head->next;
    delete head;
    head = nodePtr;
  } else {
    nodePtr = head;

    while (nodePtr != NULL && nodePtr->value != num) {
      previousNode = nodePtr;
      nodePtr = nodePtr->next;
    }

    if (nodePtr) {
      previousNode->next = nodePtr->next;
      delete nodePtr;
    }
  }
}

Just initialize previousNode to NULL at line 4.

I think TheRabbitologist is right with his explanation of why the compiler thinks the pointer can be NULL, but a slightly deeper look shows that the compiler is wrong. Lines 10-12 handle deleting the first node so line 17 will always execute.

You can shrink all of this code enormously with a trick. Instead of using a pointer to the previous node, use a pointer to the pointer to the node. In other words, a pointer to head, or the previous node's next pointer:
1
2
3
4
5
6
7
8
9
10
11
12
void LinkedList::deleteNode(double num)
{
  ListNode *nodePtr;
  ListNode **pp;

  for (pp = &head; nodePtr = *pp; pp = &nodePtr->next) {
    if (nodePtr->value == num) {
      *pp = nodePtr->next;
      delete nodePtr;
      break;
  }
}

The same trick can simplify some of the other code too.
That made much more sense.. Thanks guys! Helped a lot!
Topic archived. No new replies allowed.