COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
print_list_reverse.cpp File Reference
#include <iostream>
Include dependency graph for print_list_reverse.cpp:

Go to the source code of this file.

Classes

struct  Node
 

Functions

int main ()
 
void print_list (Node *head)
 
void print_list_reverse (Node *head)
 
void tail_insert (Node *&head, Node *&tail, int num)
 

Function Documentation

◆ main()

int main ( void )

Definition at line 52 of file print_list_reverse.cpp.

53{
54 Node * head = NULL, * tail = NULL;
55 int num = 0;
56
57 // build linked list backward
58 cout << "input integers (-999 to end): ";
59 cin >> num;
60 while ( num != -999 ) {
61 tail_insert(head, tail, num);
62 cin >> num;
63 }
64
65 // print the items in the linked list
66 cout << "print list in order: " << endl;
67 print_list(head);
68
69 // print the items in the linked list in reverse
70 cout << "print list in reverse: " << endl;
72
73 return 0;
74}
void tail_insert(Node *&head, Node *&tail, int num)
void print_list_reverse(Node *head)
void print_list(Node *head)
Definition 3.cpp:6

References print_list(), print_list_reverse(), and tail_insert().

Here is the call graph for this function:

◆ print_list()

void print_list ( Node * head)

Definition at line 21 of file print_list_reverse.cpp.

22{
23 Node * current = head;
24 while (current != NULL)
25 {
26 // process the current node, e.g., print the content
27 cout << current->info << " -> ";
28 current = current->next;
29 }
30 cout << "NULL\n";
31}
int info
Definition ex4ex5.cpp:7
Node * next
Definition ex4ex5.cpp:8

References Node::info, and Node::next.

Referenced by main().

Here is the caller graph for this function:

◆ print_list_reverse()

void print_list_reverse ( Node * head)

Definition at line 11 of file print_list_reverse.cpp.

12{
13 if (head == NULL)
14 cout << "NULL";
15 else {
16 print_list_reverse( head->next );
17 cout << " <- " << head->info;
18 }
19}

References Node::info, Node::next, and print_list_reverse().

Referenced by main(), and print_list_reverse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tail_insert()

void tail_insert ( Node *& head,
Node *& tail,
int num )

Definition at line 33 of file print_list_reverse.cpp.

35{
36 Node * p = new Node;
37 p->info = num;
38 p->next = NULL;
39
40 if (head == NULL) {
41 head = p;
42 tail = p;
43 }
44 else {
45 tail->next = p;
46 tail = p;
47 }
48}

References Node::info, and Node::next.

Referenced by main().

Here is the caller graph for this function: