diff options
Diffstat (limited to 'C++/LinkedLists.cpp')
-rw-r--r-- | C++/LinkedLists.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/C++/LinkedLists.cpp b/C++/LinkedLists.cpp new file mode 100644 index 0000000..55dc090 --- /dev/null +++ b/C++/LinkedLists.cpp @@ -0,0 +1,41 @@ +#include <iostream> + +using namespace std; + +struct intList { + int val; + intList* next_el; +}; + +// add functions to add elements to a list +// create a list and be able to order them +// make ordered linked list + +int main() { + intList* ilist = NULL; + int el, n; + + cout << "How many elements do you want to add to the list?" << endl; + cin >> n; + cout << "Now please enter the " << n << " integers: " << endl; + for(int i = 0; i < n; ++i) { + cin >> el; + intList* tmp = new intList; + tmp->val = el; + tmp->next_el = ilist; + ilist = tmp; + } + cout << endl; + intList* lit = ilist; + while(lit != NULL) { + cout << lit->val << endl; + lit = lit->next_el; + } + + while(ilist != NULL) { + intList* tmpilist = ilist->next_el; + delete ilist; + ilist = tmpilist; + } + return 0; +} |