hi
I have been working in this code for a while and I need to know what's wrong with it ASAP.
here:
#include<iostream> using namespace std; class Node { public: int value; Node * next; Node(); Node(int); }; Node::Node():value(0),next(NULL){} Node::Node(int v):value(v),next(NULL){} bool AllPositive(next * h) { bool test = false; Node* tmp; while(head) { if (head > 0) { test = true; } else { test = false; exit if; } tmp = head; head = head->next; return test; } head = NULL; } int Length(Node * h) { Node * current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; } bool Greater(Node * h1, Node * h2) { Node* current = head; while (h1 != NULL && h2 != NULL) { h1 = h1 -> next; h2 = h2 -> next; } int main() { Node * h = NULL; bool positive = false; Node* one = new Node(1); Node* two = new Node(2); Node* three = new Node(3); h=one; one->next=two; two->next=three; positive = AllPositive(h); if (positive = true) { cout<<"all are positive"; } else { cout<<"one or more negative"; } Length( node* h); Greater(Node * h1, Node * h2); return 0; }
The questions I'm trying to answer are those:
Q1: Write a function ‘AllPositive’ that takes the head of a linked list and returns true if all of the nodes in the linked list are positive, otherwise it returns false.
bool AllPositive(Node * h);
Q2: Write a function ‘Length’ that takes the head of a linked list as argument and returns the length of the list. Do NOT use recursion.
int Length(Node * h);
Q3: Write a function ‘Greater’ that takes the heads of two linked lists as arguments and returns true if the first linked list is greater than the second linked list. Otherwise it returns false. A linked list h1 is greater than the linked list h2 if h1 has more number of nodes than h2.
bool Greater(Node * h1, Node * h2);
Any help would be appreciated.
Edited 5 Years Ago by Nick Evan: Added CODE-tags