Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Chapter 15 - Project 1
- Directions: Write a program to remove an element from a linked list; the remove
- function should take just the element to be removed. Is this function easy to
- write and will it be fast? Could this be made easier or faster by adding
- and additional pointer to the list?
- */
- #include <ctime> // time
- #include <cstdlib> // rand/NULL
- #include <iostream> // cout/cin
- using namespace std;
- // our linked list structure, has one integer
- struct LinkedList {
- int value; // our value
- LinkedList* pNextValue; // pointer to the next value
- };
- LinkedList* head = NULL; // global variable head (start of list)
- // add value
- LinkedList* addValue(int v) {
- LinkedList* pValue = new LinkedList; // allocate memory
- // int randomNum = randRange(0,100); // gen random number
- pValue->value = v; // set pointer to the random number
- pValue->pNextValue = head; // point to previous first element in list
- head = pValue; // update so that our newest value is at the beginning of list
- return pValue; // return our list
- }
- // traverse through list and display
- void displayList() {
- LinkedList* current = head; // setting current to first element in list
- int i = 1; // keeps track
- while(current != NULL) {
- cout << "Value #" << i << ": " << current->value << ".\n";
- current = current->pNextValue; // 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 = 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
- }
- }
- // main
- int main() {
- srand(time(NULL)); // generate a random number
- 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->value << "\n"; // actual value there
- cout << "Adding more values.\n";
- listValue = addValue(5);
- //listValue = addValue(10);
- //listValue = addValue(15);
- cout << "Mem address for " << listValue->value << ": " << listValue << "\n.";
- displayList();
- removeElement(5);
- displayList();
- cout << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement