Advertisement
Guest User

LListC++

a guest
Sep 1st, 2014
2,586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. /*
  2. Chapter 15 - Project 1
  3. Directions: Write a program to remove an element from a linked list; the remove
  4. function should take just the element to be removed. Is this function easy to
  5. write and will it be fast? Could this be made easier or faster by adding
  6. and additional pointer to the list?
  7. */
  8.  
  9. #include <ctime> // time
  10. #include <cstdlib> // rand/NULL
  11. #include <iostream> // cout/cin
  12.  
  13. using namespace std;
  14.  
  15.  
  16. // our linked list structure, has one integer
  17. struct LinkedList {
  18. int value; // our value
  19. LinkedList* pNextValue; // pointer to the next value
  20. };
  21.  
  22. LinkedList* head = NULL; // global variable head (start of list)
  23.  
  24. // add value
  25. LinkedList* addValue(int v) {
  26. LinkedList* pValue = new LinkedList; // allocate memory
  27. // int randomNum = randRange(0,100); // gen random number
  28. pValue->value = v; // set pointer to the random number
  29. pValue->pNextValue = head; // point to previous first element in list
  30. head = pValue; // update so that our newest value is at the beginning of list
  31. return pValue; // return our list
  32. }
  33.  
  34. // traverse through list and display
  35. void displayList() {
  36. LinkedList* current = head; // setting current to first element in list
  37. int i = 1; // keeps track
  38. while(current != NULL) {
  39. cout << "Value #" << i << ": " << current->value << ".\n";
  40. current = current->pNextValue; // go to next value
  41. i++; // increment i
  42. }
  43. }
  44.  
  45. // remove an element from the linked list
  46. void removeElement(int remValue) {
  47. // to remove an element, we go through the list, find the value given
  48. // if we find it, stop
  49. // to remove, disconnect the link
  50. // relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
  51. LinkedList* current = head;
  52. LinkedList* next = current;
  53. while(current != NULL) {
  54. if(current->value == remValue) { // if match
  55. break; // break out of while
  56. }
  57. else {
  58. cout << "Value " << current->value << " does not match " << remValue << ".\n";
  59. next = current; // save in case
  60. current = current->pNextValue; // go to next value
  61. }
  62. } // end while
  63. if(current == NULL) { // if we reached end of list
  64. cout << "Can't remove value: no match found.\n"; // no match, cant remove
  65. } else { // found match
  66. cout << "Deleting: " << current << "\n";
  67. delete current;
  68. current = next->pNextValue; // current is updated
  69. }
  70. }
  71.  
  72. // main
  73. int main() {
  74. srand(time(NULL)); // generate a random number
  75. LinkedList* listValue = addValue(3); // add a value to our linked list
  76. cout << "In memory, our list value is at: " << listValue << "\n"; // mem address
  77. cout << "Our actual value should be: " << listValue->value << "\n"; // actual value there
  78. cout << "Adding more values.\n";
  79. listValue = addValue(5);
  80. //listValue = addValue(10);
  81. //listValue = addValue(15);
  82. cout << "Mem address for " << listValue->value << ": " << listValue << "\n.";
  83. displayList();
  84. removeElement(5);
  85. displayList();
  86. cout << "\n";
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement