Created
October 2, 2017 16:10
-
-
Save hckim16/72d4a9f9376220a90244b183391f826a to your computer and use it in GitHub Desktop.
Singly linked list with traverse and search functions, credit and help cited
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//courseID:CIS277-001 | |
//name: Hyo Chang Kim | |
//Prof. Eliscu | |
//Homework Assignment#4: Singly Linked List | |
//Due by 2/21/2017 | |
/* | |
I had a few challenges on this assignment. I wrote the code for creating a singly linked list that accepted the airport codes and miles as values. I wrote it as a do-while loop after setting the initial node to LGA and zero miles. The do-while then adds as many nodes as inputted to the last position. I wrote a second do-while loop with switch menu to give the user the option of displaying the entire list or to search for a specific code. | |
*/ | |
/* | |
I needed help from Arielle Patrice to write the search function that also created a new list which placed the search airport code at the head and automatically populated the follow on codes in the order entered. Arielle also helped me greatly in showing me how to debug a program and talking me through the concepts how nodes are created, inserted, traversed, etc. and reinforcing the lecture notes. | |
*/ | |
#include<iostream> | |
#include<string> | |
using namespace std; | |
class StringNode { // establish class StringNode for Linked List | |
private: | |
StringNode *next; // points to next node in the list | |
string elem; // string variable for airport code | |
double miles; // double to hold distance from LGA in miles | |
public: | |
friend class StringLinkedList; // gives StringLinkedList access to StringNode private members | |
}; | |
class StringLinkedList { | |
private: | |
StringNode *head; // points to head of the list | |
StringNode *priorloc; // points to prior node location on list | |
StringNode *nextloc; // points to next node location on list | |
public: | |
StringLinkedList(); // empty list constructor | |
~StringLinkedList(); // destructor | |
bool empty() const; // function to test for empty list | |
void addNode(const string& e, const double& m); // add nodes to end | |
void removeFront(); // remove nodes as part of destructor | |
void display(); // display list | |
void search(const string&); // finds searched value in list | |
}; | |
StringLinkedList::StringLinkedList() // constructor parameters | |
{ | |
head = NULL; // initialize head to NULL | |
priorloc = NULL; // initialize priorloc to NULL | |
nextloc = NULL; // initialize nextloc to NULL | |
} | |
StringLinkedList::~StringLinkedList() // destructor | |
{ while (!empty()) removeFront();} | |
bool StringLinkedList::empty() const // checks for empty list | |
{ return head == NULL;} | |
int main () | |
{ | |
StringLinkedList list = StringLinkedList(); // define list as an object of StringLinkedList | |
string value; // hold airport code | |
double dist; // hold distance in miles to LGA | |
char again; // do-while loop option | |
list.addNode("LGA", 0); | |
do // input data | |
{ | |
cout << "Enter the 3 letter airport code: " << endl; | |
cin >> value; | |
cout << "Enter the miles to the airport from La Guardia Airport: " << endl; | |
cin >> dist; | |
list.addNode(value, dist); | |
cout << endl; | |
cout << "Do you want to add another airport location and distance? (Y/N): "; | |
cin >> again; | |
cout << endl; | |
} while (again == 'Y' ||again == 'y'); // user option to adds as many as wanted | |
int choice; // variable for second do-while loop | |
do // user input menu option | |
{ | |
cout << "The following are your choices.\n"; | |
cout << "Please enter the number for the selection you want: \n"; | |
cout << endl; | |
cout << "1. Print listing of all airport codes and miles from LGA\n"; | |
cout << "2. Search for airport and print\n"; | |
cout << "3. Quit Program\n"; | |
cin >> choice; | |
while (choice < 1 || choice > 3) // input validation | |
{ | |
cout << "The valid choices are 1 through 3. Please\n" | |
<< "select one of those." << endl; | |
cin >> choice; | |
} | |
switch (choice) | |
{ | |
case 1: // display function described below | |
list.display(); | |
cout << endl; | |
break; | |
case 2: // search function described below | |
{string searchValue; | |
cout << "Which airport are you looking for: "; | |
cin >> searchValue; | |
list.search(searchValue); | |
cout << endl; | |
break;} | |
case 3: // ends program | |
cout << "Thank you for using this program.\n"; | |
cout << "Have a nice Day.\n"; | |
cout << endl; | |
} | |
}while (choice != 3); | |
return 0; | |
} | |
/* | |
Function creates and adds node to end of list. Populates with two variables in data portion of node. Called in the the first do-while loop. | |
*/ | |
void StringLinkedList::addNode(const string& e, const double& m) | |
{ | |
StringNode* v = new StringNode; | |
v-> elem = e; | |
v-> miles = m; | |
v-> next = NULL; | |
if ( priorloc == NULL ) | |
{ | |
head = v; | |
} | |
else | |
{ | |
priorloc->next = v; | |
} | |
priorloc = v; | |
} | |
/* | |
Function displays linked list. | |
*/ | |
void StringLinkedList::display() | |
{ | |
StringNode* temp = head; | |
while (temp != NULL) | |
{ | |
cout << temp-> elem << " " << temp-> miles << " miles" << endl; | |
temp = temp-> next; | |
} | |
delete(temp); | |
} | |
/* | |
Function searches for inputed value. Once value is found, it creates a new linked list. It creates and populates another nodes with values that follow the found value on the initial list. It displays the new list as a separate command to the display function. If not found, it so informs the user. | |
*/ | |
void StringLinkedList::search(const string& e) | |
{ | |
StringNode *temp = head; // create temp node | |
bool found = false; | |
while (temp != NULL) // loops until end of list | |
{ | |
if (temp-> elem == e) // if search value is found | |
{ | |
found = true; | |
StringLinkedList newList = StringLinkedList(); // point to this node | |
newList.addNode(temp-> elem, temp-> miles); // create a new list, | |
StringNode *current = temp-> next; // point to this list | |
while (current != NULL) // until end of this list | |
{ | |
newList.addNode(current->elem,current->miles); // add node to new list and populates with existing data | |
current = current-> next; | |
} | |
newList.display(); // display new list | |
} | |
temp = temp-> next; // otherwise keep searching | |
} | |
if (!found){ | |
cout << "There is no airport code that matches what you entered." << endl; | |
cout << "Please select again." << endl; | |
cout << endl; | |
} | |
} | |
// remove function to support destructor command | |
void StringLinkedList::removeFront() | |
{ | |
StringNode* old = head; | |
head = old-> next; | |
delete old; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//courseID:CIS277-001 | |
//name: Hyo Chang Kim | |
//Prof. Eliscu | |
//Homework Assignment#4: Singly Linked List | |
//Due by 2/21/2017 | |
/* | |
I had a few challenges on this assignment. I wrote the code for creating a singly linked list that accepted the airport codes and miles as values. I wrote it as a do-while loop after setting the initial node to LGA and zero miles. The do-while then adds as many nodes as inputted to the last position. I wrote a second do-while loop with switch menu to give the user the option of displaying the entire list or to search for a specific code. | |
*/ | |
/* | |
I needed help from Arielle Patrice to write the search function that also created a new list which placed the search airport code at the head and automatically populated the follow on codes in the order entered. Arielle also helped me greatly in showing me how to debug a program and talking me through the concepts how nodes are created, inserted, traversed, etc. and reinforcing the lecture notes. | |
*/ | |
#include<iostream> | |
#include<string> | |
using namespace std; | |
class StringNode { // establish class StringNode for Linked List | |
private: | |
StringNode *next; // points to next node in the list | |
string elem; // string variable for airport code | |
double miles; // double to hold distance from LGA in miles | |
public: | |
friend class StringLinkedList; // gives StringLinkedList access to StringNode private members | |
}; | |
class StringLinkedList { | |
private: | |
StringNode *head; // points to head of the list | |
StringNode *priorloc; // points to prior node location on list | |
StringNode *nextloc; // points to next node location on list | |
public: | |
StringLinkedList(); // empty list constructor | |
~StringLinkedList(); // destructor | |
bool empty() const; // function to test for empty list | |
void addNode(const string& e, const double& m); // add nodes to end | |
void removeFront(); // remove nodes as part of destructor | |
void display(); // display list | |
void search(const string&); // finds searched value in list | |
}; | |
StringLinkedList::StringLinkedList() // constructor parameters | |
{ | |
head = NULL; // initialize head to NULL | |
priorloc = NULL; // initialize priorloc to NULL | |
nextloc = NULL; // initialize nextloc to NULL | |
} | |
StringLinkedList::~StringLinkedList() // destructor | |
{ while (!empty()) removeFront();} | |
bool StringLinkedList::empty() const // checks for empty list | |
{ return head == NULL;} | |
int main () | |
{ | |
StringLinkedList list = StringLinkedList(); // define list as an object of StringLinkedList | |
string value; // hold airport code | |
double dist; // hold distance in miles to LGA | |
char again; // do-while loop option | |
list.addNode("LGA", 0); | |
do // input data | |
{ | |
cout << "Enter the 3 letter airport code: " << endl; | |
cin >> value; | |
cout << "Enter the miles to the airport from La Guardia Airport: " << endl; | |
cin >> dist; | |
list.addNode(value, dist); | |
cout << endl; | |
cout << "Do you want to add another airport location and distance? (Y/N): "; | |
cin >> again; | |
cout << endl; | |
} while (again == 'Y' ||again == 'y'); // user option to adds as many as wanted | |
int choice; // variable for second do-while loop | |
do // user input menu option | |
{ | |
cout << "The following are your choices.\n"; | |
cout << "Please enter the number for the selection you want: \n"; | |
cout << endl; | |
cout << "1. Print listing of all airport codes and miles from LGA\n"; | |
cout << "2. Search for airport and print\n"; | |
cout << "3. Quit Program\n"; | |
cin >> choice; | |
while (choice < 1 || choice > 3) // input validation | |
{ | |
cout << "The valid choices are 1 through 3. Please\n" | |
<< "select one of those." << endl; | |
cin >> choice; | |
} | |
switch (choice) | |
{ | |
case 1: // display function described below | |
list.display(); | |
cout << endl; | |
break; | |
case 2: // search function described below | |
{string searchValue; | |
cout << "Which airport are you looking for: "; | |
cin >> searchValue; | |
list.search(searchValue); | |
cout << endl; | |
break;} | |
case 3: // ends program | |
cout << "Thank you for using this program.\n"; | |
cout << "Have a nice Day.\n"; | |
cout << endl; | |
} | |
}while (choice != 3); | |
return 0; | |
} | |
/* | |
Function creates and adds node to end of list. Populates with two variables in data portion of node. Called in the the first do-while loop. | |
*/ | |
void StringLinkedList::addNode(const string& e, const double& m) | |
{ | |
StringNode* v = new StringNode; | |
v-> elem = e; | |
v-> miles = m; | |
v-> next = NULL; | |
if ( priorloc == NULL ) | |
{ | |
head = v; | |
} | |
else | |
{ | |
priorloc->next = v; | |
} | |
priorloc = v; | |
} | |
/* | |
Function displays linked list. | |
*/ | |
void StringLinkedList::display() | |
{ | |
StringNode* temp = head; | |
while (temp != NULL) | |
{ | |
cout << temp-> elem << " " << temp-> miles << " miles" << endl; | |
temp = temp-> next; | |
} | |
delete(temp); | |
} | |
/* | |
Function searches for inputed value. Once value is found, it creates a new linked list. It creates and populates another nodes with values that follow the found value on the initial list. It displays the new list as a separate command to the display function. If not found, it so informs the user. | |
*/ | |
void StringLinkedList::search(const string& e) | |
{ | |
StringNode *temp = head; // create temp node | |
bool found = false; | |
while (temp != NULL) // loops until end of list | |
{ | |
if (temp-> elem == e) // if search value is found | |
{ | |
found = true; | |
StringLinkedList newList = StringLinkedList(); // point to this node | |
newList.addNode(temp-> elem, temp-> miles); // create a new list, | |
StringNode *current = temp-> next; // point to this list | |
while (current != NULL) // until end of this list | |
{ | |
newList.addNode(current->elem,current->miles); // add node to new list and populates with existing data | |
current = current-> next; | |
} | |
newList.display(); // display new list | |
} | |
temp = temp-> next; // otherwise keep searching | |
} | |
if (!found){ | |
cout << "There is no airport code that matches what you entered." << endl; | |
cout << "Please select again." << endl; | |
cout << endl; | |
} | |
} | |
// remove function to support destructor command | |
void StringLinkedList::removeFront() | |
{ | |
StringNode* old = head; | |
head = old-> next; | |
delete old; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment