First, your design for linked list is not impressive.
At a glance, the listNode is a class/object which each copy/instance of it would have a copy of the list's head.
And each node/object acts as a list where and what to act upon.
I would rather design a container like class that keeps track of the list's head, tail and other list operations. And the node would be a separate struct or class which would just have a data element and linking pointers prev or next.
It would look like:
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
|
struct Node {
//by default all public, in a structure
char item; // for a single char data element, i would not consider a linked list
Node *next;
Node() {}
Node(char dItem) { item = dItem; next = NULL }
};
class List {
public:
List() { head = NULL; }
bool insert(char data); // returns true or false, if inserted successfully
display(); // displays the list in-node or any other traversal
bool delete(char data);
private:
Node *head;
insertList(char data);
deleteList(char data);
displayList();
};
|
As you see it above, the List would be your driver to insert/delete/display the linked list. Just like a container allowing you to operate on and inside the class it takes care of capsulated operations.
And now coming to the data storage, you would have to give your upper/lower case checking within the addList() functionality.
For each node insertion, you would check where the element/node to be placed in the list being created.
It would be like, before you insert check the right place, other you move on to next node. This all would be in a single while loop. It would look like:
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
|
bool addList(char item)
{
// ...
if (head == NULL)
//create a new node, store it as head and return true, if success
else {
Node * newItem = new Node(item);
if (newItem == NULL) //failed allocation
return false;
// if list has currently only one item
if (upper(head->item) > upper(newItem->item))
{
newItem->next = head;
head = newItem;
return true;
}
//otherwise
Node *current = head;
while (current != NULL)
{
if (current->next == NULL && upper(newItem->item) > upper(current->item)) //current is the last in list
{
current->next = newItem;
return true;
}
if (upper(current->next->item) > upper(newItem->item) // insert right after current
{
newItem->next = current->next;
current->next = newItem;
return true;
}
//otherwise, keep checking
current = current->next;
}
}
}
|
Hope you understand the point. Check it out.
Good luck :)