-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-test2.cpp
More file actions
51 lines (40 loc) · 815 Bytes
/
list-test2.cpp
File metadata and controls
51 lines (40 loc) · 815 Bytes
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
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/* TODO: write another program, similar to the above, which reads all
* of stdin into a list *in the same order* (the above reverses the
* order). */
/* TODO: make sure you have understood the most recent additions to
* the ../whatstheoutput/ directory ({6,7}.cpp, I think). */
struct node {
int data;
node* next;
};
void insertNext(node*& L, int input) {
L->next = new node;
L = L->next;
L->data = input;
}
void printList(node*& L) {
for (node* i = L; i != NULL; i = i->next) {
cout << i->data << " ";
}
}
int main(void)
{
node* L = NULL;
node* start = NULL;
int x;
if (cin >> x) {
L = new node;
L->data = x;
start = L;
}
while(cin >> x) {
insertNext(L, x);
}
printList(start);
cout << endl;
return 0;
}