COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
build_list_backward.cpp
Go to the documentation of this file.
1
#include <iostream>
2
3
using namespace
std;
4
5
struct
Node
6
{
7
int
info
;
8
Node
*
next
;
9
};
10
11
void
print_list
(
Node
* head)
12
{
13
Node
* current = head;
14
while
(current != NULL)
15
{
16
// process the current node, e.g., print the content
17
cout << current->
info
<<
" -> "
;
18
current = current->
next
;
19
}
20
cout <<
"NULL\n"
;
21
}
22
23
void
head_insert
(
Node
* & head,
int
num)
24
{
25
Node
* p =
new
Node
;
26
p->
info
= num;
27
p->
next
= head;
28
head = p;
29
}
30
31
32
int
main
()
33
{
34
Node
* head = NULL;
35
int
num = 0;
36
37
// build linked list backward
38
cout <<
"input integers (-999 to end): "
;
39
cin >> num;
40
while
( num != -999 ) {
41
head_insert
(head, num);
42
cin >> num;
43
}
44
45
// print the items in the linked list
46
print_list
(head);
47
48
return
0;
49
}
head_insert
void head_insert(Node *&head, int num)
Definition
build_list_backward.cpp:23
main
int main()
Definition
build_list_backward.cpp:32
print_list
void print_list(Node *head)
Definition
build_list_backward.cpp:11
Node
Definition
3.cpp:6
Node::info
int info
Definition
ex4ex5.cpp:7
Node::next
Node * next
Definition
ex4ex5.cpp:8
Module8
Sample Code
Linked Lists
build_list_backward.cpp
Generated on Sun Sep 29 2024 16:39:53 for COMP2113 by
1.12.0