I am trying to write an algorithm to determine the average of a linked list of real numbers with the first node pointed to by first...

ptr = first;
while (ptr != null_value)
{
//not sure what to put in here
}


Can someone take a look of this short algorithm and help me to determine to finish this off as I have never really worked with this before?

Recommended Answers

All 11 Replies

First you need to declare a float variable and set it to 0. Then set your pointer to your head node like you did. Then in your loop, do your float variable += ptr->number. And iterate it by saying ptr = ptr->link(or whatever you named it).
Then when the loop is done, divide your float variable by how many nodes there are by using an int counter variable that you ++ for each iteration of the loop.

Apologize if this really makes me sound like an idiot but this is what I got and just want to make sure I did this right

float num = 0;
int number;
ptr = first;
while (ptr != null_value)
{
	num += ptr >> number;
number++;
}
	Average = float / number;

Line 6, make it num += ptr->data. What this does, is adds the number being held in the data variable in the node to your num variable. Data being the value stored in the node that you want to tally up. line 7 is fine.

Then on line 8, before you close off the loop, type in ptr = ptr->link. Link being the pointer in your node that points to the next node. This is how you point to the next node in the list.

Then line 9 outside of the loop, your going to type num = num / number. I would change your number variable to counter. and indent line 7. Proper naming conventions etc can make code much easier to work with.

Edit: Are you sure your working with a linked list? Can you post all your source?

On a side not you should never use the increment operator on a number that hasn't been initialized. line 2 should be int number = 0;

k thanks I appreciate it. I am going to make a lot of posts as I am stuck on several of this problems so I got this so it should work now...

float num = 0;
int number;
ptr = first;
while (ptr != null_value)
{
    num += ptr -> data;
number++;
ptr = ptr->link
}
num = num / number;
    Average = float / number;

Ya, you got it. Accept you dont need your last line there. the line with num = num / number is your average. If you want to set it to another variable, just say average = num.

I am working on another linked list problem...really been struggling with this chapter dealing with lists...Here is the instruction for this example...

Write an algorithm to search a linked list with the first node pointed to by first for a given item and, if the item is found, return a pointer to the predecessor of the node containing the item...

I guess my major problem is I do not know how to perform a search and just want to see if I am on the right track...

float num = 0;
    int number = 0;
    ptr = first;
    while (ptr! = null_value)
    {
        //perform the search
    }
    return ptr;
Node* findSomething(Node* listToSearch, int whatToFind){
  while(listToSearch->next)
    if(listToSearch->next->data==whatToFind)
      return listToSearch;
  return NULL;
}

wow I was way off thanks

oh crap
i forgot to add this:

Node* findSomething(Node* listToSearch, int whatToFind){
        while(listToSearch->next)
          if(listToSearch->next->data==whatToFind)
            return listToSearch;
          else                                     //these two lines
            listToSearch=listToSearch->next;       //have to be added
        return NULL;
      }

what i posted before is an infinite loop

thank you....you have been a great help

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.