Problem with recursion function
Mar 28, 2016 at 7:50pm UTC
The recursion function int LinkedList::recurDist(Node* curr) on line 71 is supposed to calculate the sum of the distances from LGA to the airport code typed in by the user, but it always returns 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#include <iostream>
#include <string>
#include <unistd.h> //sleep(),
using namespace std;
int sum = 0; //declaration of sum variable to hold sum, init to 0.
class Node
{
private :
string airportCd;
int distance;
Node* next;
Node* prev;
friend class LinkedList;
};
class LinkedList
{
private :
Node* header;
Node* trailer;
Node *curr;
public :
LinkedList(); //constructor
void insertNode(Node*, const string&, const int &); //insert new node
void printList(); //function to print the linked list
void menu(); //menu for user selection
int recurDist(Node*); //calculate distance from LGA to selected airport using recursion
void iterDist(const string&); //calculate distance from LGA to selected airport using iteratrion
};
//*********************//
//LINKED LIST FUNCTIONS//
//*********************//
LinkedList::LinkedList() //constructor
{
header = new Node;
header->airportCd = "LGA" ;
header->distance = 0;
trailer = new Node;
header->next = trailer;
trailer->prev = header;
}
void LinkedList::insertNode(Node* nextloc, const string& e, const int & f) //insert new node
{
Node* t = new Node;
t->airportCd = e;
t->distance = f;
t->next = nextloc;
t->prev = nextloc->prev;
nextloc->prev->next = t;
nextloc->prev = t;
cout << endl;
}
void LinkedList::printList() //print linked list
{
curr = header;
while (curr->next != trailer)
{
cout << curr->airportCd << " " ;
cout << curr->distance << " miles." << endl;
curr = curr->next;
}
}
int LinkedList::recurDist(Node* curr) //calculate distance from LGA to selected airport using recursion
{
if (curr->airportCd == header->airportCd)
{
return 0;
}
else return sum + recurDist(curr->prev);
}
void LinkedList::iterDist(const string& e) //calculate distance from LGA to selected airport using iteration
{
curr = header;
while (curr->airportCd != e) //while the current pointer is not pointing to the chosen airport cd's node
{
sum = sum + curr->distance;
curr = curr->next;
}
if (curr->airportCd == e) //if the current ptr is pointing at the chosen airport cd's node
{
sum = sum + curr->distance;
}
cout << sum << " miles.\n\n" ;
}
void LinkedList::menu() //menu
{
int distance;
string airPCd;
int choice;
do {
cout << "Menu: Select choice(1-3). Enter 4 to quit.\n" ;
cout << "-------------------------------------------------------------\n\n" ;
cout << "1)Enter new airport information.[Starting airport is LGA.]\n\n" ;
cout << "2)Specify an existing airport code and calculate the distance\nfrom LGA to the selected airport using recursion.\n\n" ;
cout << "3)Specify an existing airport code and calculate the distance\nfrom LGA to the selected airport by looping thru " ;
cout << "the nodes in\nstandard fashion.\n\n" ;
cin >> choice;
switch (choice)
{
case 1:
curr = trailer->prev;
cout << "Enter airport code.\n" ;
cin >> airPCd;
if (curr == header) //if header points to trailer
{
cout << "Enter distance from " << header->airportCd << ".[In miles]\n" ; //from "LGA"
}
else //else
{
cout << "Enter distance from " << curr->airportCd << ".[In miles]\n" ; //from prev airportCd
}
cin >> distance;
insertNode(trailer, airPCd, distance);
break ;
case 2:
cout << "Enter an airport code.\n" ;
cin >> airPCd;
curr = header;
while (curr->airportCd != airPCd)
{
curr = curr->next;
}
cout << recurDist(curr) << " miles.\n\n" ; //call function to sum distance from LGA to chosen airport code
break ;
case 3:
cout << "Enter and airport code.\n" ;
cin >> airPCd;
iterDist(airPCd);
break ;
case 4:
return ;
default :
cout << "YOU HAVE ENTERED AN INVALID NUMBER!!!\n\n" ;
}
}while (choice != 4);
}
int main()
{
LinkedList passenger;
passenger.menu();
for (int i = 0; i < 3; i++)
{
cout << "." ;
cout << flush; //flush out buffer
sleep(1); //prints 1 dot every second
}
cout << "Finito.\n" ;
return 0;
}
Last edited on Mar 28, 2016 at 7:51pm UTC
Mar 28, 2016 at 8:00pm UTC
line 6: What makes you think using a global here is a good idea?
Consider if you loop through menu more than once, you never reset sum.
Mar 28, 2016 at 8:03pm UTC
I thought that if I declared it in the recursion function, it would also reset to 0 when it recursively calls itself, so I made it global.
Mar 28, 2016 at 9:36pm UTC
Consider if you loop through menu more than once, you never reset sum.
I don't think that matters since, with the current logic in the program, sum will never be anything but 0.
I thought that if I declared it in the recursion function, it would also reset to 0 when it recursively calls itself, so I made it global.
If a recursive function needs to keep track of something like that, you generally make it a parameter to the function.
Let me say that a linked list of airports is a particularly shoddy representation of flight paths. This
should be implemented with a graph.
I suspect your recursive function should look something like this:
1 2 3 4 5 6 7 8 9
int LinkedList::recurDist(Node* curr) //calculate distance from LGA to selected airport using recursion
{
if (curr->airportCd == header->airportCd)
{
return 0;
}
else
return curr->distance + recurDist(curr->prev);
}
Although it doesn't take into account the direction in which the list needs to be iterated and could use a sanity check or two.
Mar 28, 2016 at 10:58pm UTC
Haha yeah linked List was the way my professor told us to do it. We didn't cover graphs yet. Thanks, it works now. I don't quite get why though it wouldn't work with sum. Can you explain why?
Mar 29, 2016 at 1:09pm UTC
Your function returned sum + recurDist(curr->prev)
, but sum was zero. The only other value returned from the function was... also 0. So you either returned 0 or 0+0.
Topic archived. No new replies allowed.