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 78 79 80 81 82
|
using namespace std;
struct Node {
int data1;
int data2;
int data3;
Node* next = NULL;
Node* curr = NULL;
Node* prev = NULL;
};
class PolyLL {
public:
int data1, data2, data3;
Node* insertInOrder(Node* head, int first, int second, int third) {
Node* nn = new Node;
nn->data1 = first;
nn->data2 = second;
nn->data3 = third;
nn->next = NULL;
nn->prev = NULL;
Node* curr = head;
if (curr == NULL) {
head = nn;
return head;
}
if (curr->data2 > nn->data2) {
nn->next = curr;
curr->prev = nn;
head = nn;
return head;
}
while (curr->next != NULL && curr->next->data2 < nn->data2) {
curr = curr->next;
}
nn->next = curr->next;
if (curr->next != NULL)
curr->next->prev = nn;
curr->next = nn;
nn->prev = curr;
return head;
}
Node* print(Node* head)
{
Node* curr = head;
while (curr != NULL)
{
cout << curr->data1 << ", " << curr->data2<<", " << curr->data3 << " ";
curr = curr->next;
}
cout << endl;
return head;
}
private:
Node*head;
};
int main()
{
PolyLL exp1;
Node* head = NULL;
exp1.insertInOrder(head, 5, 4, 2);
exp1.insertInOrder(head, 2, 2, 3);
exp1.insertInOrder(head, -6, 3, 1);
exp1.insertInOrder(head, -7, 1, 0);
exp1.insertInOrder(head, 6, 0, 0);
exp1.print(head);
system("Pause");
return 0;
}
|