hi everyone
the program below is a function that should give the number of items greater than a number entered by a user.(unsorted list ADT)
ex:
input 3
unsorted list contains: 2 6 8 1 5 9 8
output should be 5

here is my try by there are alot of errors..could u plz help?

int GreaterThanItem(ItemType givenItem)
{
Node *ptr;
Ptr=head;
While(ptr!=node*)NULL)
{
If(ptr->info==item)
{
int Count_Max(node_ptr&q,int x)
{
    node_ptr p;
    p=q;
    int count=0;
    while(p!=NULL)
    {
        if(p->num > x)
        count++;
        p=p->next;
    }
    return count;
}
    }
Ptr=ptr->next;
}
Return NULL;
}

Dani AI

Generated

Quick practical fix for (and a note to helpers): the main problems in the posted code are a function declared inside another function, mixed/misspelled C++ keywords (e.g. While), inconsistent pointer names, and returning NULL from a function that should return an int. correctly flagged the nested-function issue and echoed that; 's pointer to a basic C/C++ refresher is useful for syntax and style.

Write one standalone function that takes the list head and the threshold, walks the list and returns an integer count. Prefer const on the input pointer if you don't modify the list, use nullptr (C++11+) or NULL for older compilers, and avoid malloc/free in idiomatic C++ (prefer STL containers or new/delete if you must manage memory yourself).

Here is a small, clean example you can drop into your program:

int countGreaterThan(const node* head, int threshold) {
    int count = 0;
    for (const node* cur = head; cur != nullptr; cur = cur->next) {
        if (cur->num > threshold) ++count;
    }
    return count;
}

/* Example: list 2 6 8 1 5 9 8
   countGreaterThan(head, 3) returns 5 */

Quick troubleshooting checklist:

  • Don’t declare a function inside another function. Move it to global scope or a class.
  • Match return types: an int function must return an integer, not NULL.
  • Initialize head before traversing; advance the pointer each loop iteration.
  • Fix keyword capitalization and missing semicolons/braces (these cause most compile errors).
  • If you can change the design, consider std::vector + std::count_if to simplify testing.

Use the snippet above to test with a small list, print the result, then apply it to your ADT.

Recommended Answers

All 7 Replies

can anybody plz help?

What you're trying to do here:

If(ptr->info==item)
{
int Count_Max(node_ptr&q,int x)
{

is wrong. You can't declare a function inside another function.

Are your really using a linked list or did someone just give you that code?

i was trying..but the thing is that im weak at programming..
could u plz point me to my mistakes and help me remove the errors?

i was trying..but the thing is that im weak at programming..
could u plz point me to my mistakes and help me remove the errors?

He already has. You can't declare a function inside a function.

how can i solve this problem?
i should remove the function?
and then do what with the code that does the searching?

can you please help me write the main to the function so i can compile and run the program?

struct node{
int num;
node * next;
};
typedef node *node_ptr;

void firstNode(node_ptr &first,int n,char name[10])
{
if(first==NULL)
{


first=( node* )malloc(sizeof(node));
first->num=n;
first->next=NULL;
}
else
{
cout<<"Error:";
}
}

void append (node_ptr &first,int n,char name[10])
{

node_ptr p,q;

p=first;
while(p->next!=NULL)
{

p=p->next;
}
q=(node *)malloc(sizeof(node));
q->num=n;
q->next=NULL;
p->next =q;


}

void Display(node_ptr&q)
{
node_ptr p;
p=q;
while(p!=NULL)
{
p=p->next;
}
}

int Count_Max(node_ptr&q,int x)
{
node_ptr p;
p=q;
int count=0;
while(p!=NULL)
{
if(p->num > x)
count++;
p=p->next;
}
return count;
}

Please read this, so we can follow your code. It will also show you some problems you can fix yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.