// Bob Zambanini - Monday, March 14, 2011 - lab5_2.cpp
\
#include <iostream>
usingnamespace std;
constshortunsignedint NUMVALS = 10;
struct ListNode {
double Celement;
ListNode* next;
};
ListNode* alpha = NULL;
bool insertNode(char element);
void displayList(void);
int main(void)
{
char temp[30];
cout<<"input something";
cin>>temp;
char* name = temp;
shortunsignedint i;
for(i=0; i<NUMVALS; i++) { // insert the array element into the linked list
if(insertNode(temp[i])) {
cout << "Error in function insertNode!\n";
cout << "Program will halt!\n";
exit(1);
}
}
displayList();
}
bool insertNode(char element)
// This function inserts a node into our linked list.
{
ListNode* newNode;
ListNode* nodePtr = alpha;
ListNode* prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->Celement = element;
newNode->next = NULL;
if(alpha==NULL) { // check if list is currently empty
cout << "List is currently empty - " << newNode->Celement;
cout << " is part of the list's first node!\n";
alpha = newNode;
}
else {
while(nodePtr != NULL) {
prevNodePtr = nodePtr; // advance prevNodePtr up to nodePtr
nodePtr = nodePtr->next; // advance nodePtr by 1 node
}
if(prevNodePtr == NULL) {// at beginning of list
newNode->next = alpha;
alpha = newNode;
}
}
return 0;
}
void displayList(void)
// This function displays our linked list!
{
ListNode* nodePtr = alpha;
if(alpha == NULL) {
cout << "List is currently empty!\n";
}
else {
while (nodePtr != NULL) {
cout << nodePtr->Celement << endl;
nodePtr = nodePtr->next;
}
}
}
As you can see i am trying to store each char element in a link list.(by doing that i can print it backwards) however the problem is no matter what i type (e.g i type keith) it will said the list is empty program will halt. my second problem is if i sucessfully fix the first problem (which is storing each char element in each link list) how to display it backwards>?