Skip to content

Instantly share code, notes, and snippets.

@NickersF
Created February 19, 2016 08:35
Show Gist options
  • Save NickersF/fa9b23b52d70cd0f9a21 to your computer and use it in GitHub Desktop.
Save NickersF/fa9b23b52d70cd0f9a21 to your computer and use it in GitHub Desktop.
Linked List
#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