Created
February 19, 2016 08:35
-
-
Save NickersF/fa9b23b52d70cd0f9a21 to your computer and use it in GitHub Desktop.
Linked List
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
#include <iostream> | |
using namespace std; | |
struct nodeType | |
{ | |
int info; | |
nodeType *link; | |
}; | |
nodeType* buildListForward(); | |
nodeType* buildListBackward(); | |
int main() { | |
nodeType *first; | |
first = buildListForward(); | |
while (first != nullptr) { | |
cout << first->info << " "; | |
first = first->link; | |
} | |
return 0; | |
} | |
// build a linked list forwards | |
nodeType* buildListForward() { | |
nodeType *first, *last, *newNode; | |
int num; | |
cout << "Enter a list of integers ending with -999" << endl; | |
cin >> num; | |
first = nullptr; | |
while (num != -999) { | |
newNode = new nodeType; | |
newNode->info = num; | |
newNode->link = nullptr; | |
if (first == nullptr) { | |
first = newNode; | |
last = newNode; | |
} | |
else { | |
last->link = newNode; | |
last = newNode; | |
} | |
cin >> num; | |
} | |
return first; | |
} | |
// build a linked list backwards | |
nodeType* buildListBackward() { | |
nodeType *first, *newNode; | |
int num; | |
cout << "Enter a list of integers ending with -999" << endl; | |
cin >> num; | |
first = nullptr; | |
while (num != -999) { | |
newNode = new nodeType; | |
newNode->info; | |
newNode->link = first; | |
first = newNode; | |
cin >> num; | |
} | |
return first; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment