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
3using namespace std;
4
5struct Node
6{
7 int info;
8 Node * next;
9};
10
11void 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
23void 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
32int 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}
void head_insert(Node *&head, int num)
int main()
void print_list(Node *head)
Definition 3.cpp:6
int info
Definition ex4ex5.cpp:7
Node * next
Definition ex4ex5.cpp:8