//Pg.288
#include <cassert>
#include <iostream>
using namespace std;
void deleteKthElement(int k, int& count);
int main()
{
struct nodeType {
int info;
nodeType *link;
};
nodeType *first, *newNode, *last, *traverse;
int num;
int count = 0;
cout << "Enter a list of integers ending with -999.\n";
cin >> num;
first = NULL;
while (num != -999)
{
newNode = new nodeType;
assert(newNode !=NULL);
newNode ->info = num;
newNode ->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last -> link = newNode;
last = newNode;
}
cin >> num;
count++; //increment the counter to keep track of no. of elements
} // end while
//print the linked list prior to deleting the 5th element
cout << "The linked list before:" << endl;
traverse = new nodeType;
assert(traverse != NULL);
traverse = first;
while (traverse != NULL)
{
cout << traverse->info << endl;
traverse = traverse->link;
}
deleteKthElement(5,count);
//print the linked list after deleting the 5th element !!
cout << "The linked list after deleting the 5th element:" << endl;
traverse = new nodeType;
assert(traverse != NULL);
traverse = first;
while (traverse != NULL)
{
cout << traverse->info << endl;
traverse = traverse->link;
}
} // end main
void deleteKthElement(int k, int& count)
{
assert(k <= count);
nodeType *current, *trailCurrent;
int i;
if (first ==NULL)
cerr << "Cannot delete from an empty list!" << endl;
else
if (k == 1)
{
current = first;
first = first->link;
if (first == NULL)
last = NULL;
delete current;
}
else
{
trailCurrent = first;
current = first->link;
i = 2;
while(i < k)
{
trailCurrent = current;
current = current->link;
i++;
}
trailCurrent->link = current->link;
if (current == last)
last = trailCurrent;
}
}
I'm struggling to get the 5th element deleted in the list
Compiler giving me a lot of errors ; eg. nodeType undeclared, etc.
Please help ?