void removeElement(int remValue)
{// to remove an element, we go through the list, find the value given // if we find it, stop // to remove, disconnect the link // relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
LinkedList* current = head;
LinkedList* next = current;
while(current != NULL)
{
if(current->value == remValue)
{ // if match
break; // break out of while
}
else
{
cout << "Value " << current->value << " does not match " << remValue << ".\n";
next = current; // save in case
current = current->pNextValue;
// go to next value
}
}
// end while
if(current == NULL)
{
// if we reached end of list
cout << "Can't remove value: no match found.\n"; // no match, cant remove
}
else
{ // found match
cout << "Deleting: " << current << "\n";
delete current;
current = next->pNextValue; // current is updated
}
}
#include <ctime> // time
#include <cstdlib> // rand/NULL
#include <iostream> // cout/cin
usingnamespace std;
// our linked list structure, has one integer
struct LinkedList {
int info; // our value
LinkedList *link; // pointer to the next value
};
LinkedList *first = NULL; // global variable head (start of list)
// add value
LinkedList *addValue(int v) {
LinkedList *newNode = new LinkedList; // allocate memory
newNode->info = v; // set pointer to the random number
newNode-> link = first;// point to previous first element in list
first = newNode; // update so that our newest value is at the beginning of list
return newNode; // return our list
}
// traverse through list and display
void displayList() {
LinkedList *current = first; // setting current to first element in list
int i = 1; // keeps track
while(current != NULL) {
cout << "Value #" << i << ": " << current->info << "\n";
current = current->link; // go to next value
i++; // increment i
}
}
// remove an element from the linked list
void removeElement(int remValue) {
// to remove an element, we go through the list, find the value given
// if we find it, stop
// to remove, disconnect the link
// relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
LinkedList *current = first;
LinkedList *link = current;
while(current != NULL) {
if(current-> info == remValue) { // if match
break; // break out of while
}
else {
cout << "Value " << current->info << " does not match " << remValue << "\n";
link = current; // save in case
current = current->link; // go to next value
}
} // end while
if(current == NULL) { // if we reached end of list
cout << "Can't remove value: no match found.\n"; // no match, cant remove
}
else
{ // found match
cout << "Deleting: " << current << "\n";
delete current;
current = link->link; // current is updated
}
}
// main
int main()
{
LinkedList *listValue = addValue(3); // add a value to our linked list
cout << "In memory, our list value is at: " << listValue << "\n"; // mem address
cout << "Our actual value should be: " << listValue->info << "\n"; // actual value there
cout << "Adding more values.\n";
listValue = addValue(5);
listValue = addValue(10);
listValue = addValue(15);
listValue = addValue(100);
listValue = addValue(1732);
cout << "Mem address for " << listValue->info << ": " << listValue << "\n";
displayList();
removeElement(5);
displayList();
cout << "\n";
}