Jun 6, 2017 at 3:30pm UTC
It seems to work, but I think it would be more consistent if you moved the functions that modify the internal state of your structs inside the structs themselves.
Just to give you a general idea:
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <limits>
struct Node {
int data;
Node *p_next;
};
struct List {
void addNodeToTail(Node* p);
void timMax();
Node* p_head {nullptr };
Node* p_tail {nullptr };
};
void List::addNodeToTail(Node* to_be_added)
{
if (p_head == nullptr ) {
to_be_added->p_next = nullptr ;
p_head = p_tail = to_be_added;
} else {
p_tail->p_next = to_be_added;
p_tail = to_be_added;
}
}
Node* createNodeFromData(int x)
{
Node *tmp = new Node;
tmp->data = x;
tmp->p_next = nullptr ;
return tmp;
}
void input(List& l)
{
std::cout << "Nhap so Node: " ;
int n;
std::cin >> n;
std::cin.ignore(1);
for (int i = 0; i < n; i++) {
std::cout << "Please give me data for next node: " ;
int x;
std::cin >> x;
std::cin.ignore(1);
Node* tmp = createNodeFromData(x);
l.addNodeToTail(tmp);
}
}
void List::timMax()
{
int x = p_head->data;
for (Node* p = p_head; p != nullptr ; p = p->p_next){
if (p->data > x) {
x = p->data;
}
}
std::cout << "\nMax : " << x;
}
void waitForEnter();
int main()
{
List l;
input(l);
l.timMax();
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
However this is a forum to ask for help, not to have our code evaluated.
Good luck with your project!
Last edited on Jun 6, 2017 at 3:34pm UTC