aboutsummaryrefslogtreecommitdiffstats
path: root/LinkedLists.cpp
blob: 55dc09026426643972a051ba71a2f66cd2c260fa (plain)
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
#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;
}