title: finding min, max, and average of linked list
body: <p>I have a program in which I'm supposed to build functions using linked lists to perform a variety of tasks. Currently, I am having an issue finding the min and max value of the linked list. For some reason when both come out to be the highest which digit which is 9, and when I try to find the average of the list, it still comes out as 9. </p>
  
  <p>additionally, I think it's interfering with my pop function which is supposed to delete the last item, but  when I try to work it by sections one part wont work until he previous section is running for whatever reason.</p>
  
  <p>here is my header</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using std::cout;
  using std::endl;
  
  #ifndef LINKEDLIST_H
  #define LINKEDLIST_H
  
  
  class LinkedList
  {
      private:
  
          struct Node
          {
              int data;
              Node *next;
          };
  
          int size;
          Node *head, *tail;
  
      public:
          LinkedList();
          ~LinkedList();
  
          // misc
          void display();
  
          // sorting and searching
          // reverse --&gt; sorting in descending
          int linearSearch(int key);
          void sort();
          void reverse();
  
          // various math
          int min();
          int max();
          int mean();
  
          // adding
          void append(int num);
          void insert(int num, int pos);
  
          // removing
          void pop();
          void remove(int pos);
  };
  
  #endif // LINKEDLIST_H
  </code></pre>
  
  <p>the header's source file</p>
  
  <pre><code>#include "linkedlist.h"
  
  
  
  LinkedList::LinkedList()
  {
      head = nullptr;
      tail = nullptr;
      size = 0;
  }
  
  LinkedList::~LinkedList()
  {
      if(head != nullptr)
      {
          Node *temp;
  
          while(head != nullptr)
          {
              temp = head-&gt;next;
  
              // deletes head
              delete head;
  
              // goes to next element
              head = temp;
          }
      }
  }
  
  void LinkedList::display()
  {
      Node *temp = head;
  
      for(int i = 0; i &lt; size; i++)
      {
          cout &lt;&lt; temp-&gt;data &lt;&lt; "\t";
  
          temp = temp-&gt;next;
      }
  
      cout &lt;&lt; endl;
  }
  
  void LinkedList::append(int num)
  {
      // list is empty
      if(head == nullptr)
      {
          head = new Node;
  
          head-&gt;data = num;
          head-&gt;next = nullptr;
  
          // sets tail to head
          tail = head;
      }
  
      else
      {
          // creates new node
          Node *temp = new Node;
  
          // sets new node data
          temp-&gt;data = num;
          temp-&gt;next = nullptr;
  
          // sets previous tail link to new node
          tail-&gt;next = temp;
  
          // sets this node to new tail
          tail = temp;
      }
  
      // increments size
      size++;
  }
  
  void LinkedList::pop()
  {
      if(size &gt; 1)
      {
          Node *temp = head;
  
          // loops to node before tail
          while(temp-&gt;next-&gt;next != nullptr)
          {
              temp = temp-&gt;next;
          }
  
          // deletes tail
          delete tail;
  
          // sets new tail
          tail = temp;
          tail-&gt;next = nullptr;
      }
  
      // if there's only one item
      else if(size == 1)
      {
          Node *temp = tail;
  
          // head and tail are now null
          head = nullptr;
          tail = nullptr;
  
          // deletes node
          delete temp;
      }
  
      size--;
  }
  
  
  
  void LinkedList::insert(int num, int pos)
  {
      if(pos ==0)
      {
  
          Node *temp=new Node;
          temp-&gt;data=num;
          temp-&gt;next=head;
          head=temp;
      }
  
      if(pos&gt;1)
      {
          Node *pre=new Node;
          Node *cur=new Node;
          Node *temp=new Node;
          cur=head;
          for(int i=1;i&lt;pos+1;i++)
          {
            pre=cur;
            cur=cur-&gt;next;
          }
          temp-&gt;data=num;
          pre-&gt;next=temp;
          temp-&gt;next=cur;
      }
  
  
  
      size++;
  
  }
  
  int LinkedList::linearSearch(int key)
  {
      Node *temp = head;
      for(int i = 0; i &lt; size; i++)
      {
          if(temp-&gt;data == key)
          {
              return i;
          }
  
          temp = temp-&gt;next;
      }
  
      return -1;
  }
  
  int LinkedList::max()
  {
      int max = INT_MIN;
      for(int i = 0; i &lt; size; i++)
      {
          while (head != NULL)
          {
                  if (head-&gt;data &lt; max)
                      max = head-&gt;data;
                  head = head-&gt;next;
  
          }
  
       }
  
  }
  
  int LinkedList::min()
  {
      int min = INT_MAX;
      for(int i = 0; i &lt; size; i++)
      {
          while (head != NULL)
          {
                  if (head-&gt;data &lt; min)
                      min = head-&gt;data;
                  head = head-&gt;next;
  
          }
  
       }
  }
  
  void LinkedList::reverse()
  {
      Node* temp = head;
  
      // Traverse the List
      while (temp) {
          Node* min = temp;
          Node* r = temp-&gt;next;
  
          // Traverse the unsorted sublist
          while (r)
          {
              if (min-&gt;data &lt; r-&gt;data)
                  min = r;
  
              r = r-&gt;next;
          }
  
          // Swap Data
          int x = temp-&gt;data;
          temp-&gt;data = min-&gt;data;
          min-&gt;data = x;
          temp = temp-&gt;next;
      }
  }
  
  void LinkedList::remove(int pos)
  {
      Node *temp = head;
  
      if(pos ==0)
      {
  
          head = temp-&gt;next;
          free(temp);
      }
  
      if(pos&gt;1)
      {
         for(int i=0; temp!=NULL &amp;&amp; i&lt;pos-1;i++)
         {
             temp=temp-&gt;next;
         }
  
         temp-&gt;next = temp-&gt;next-&gt;next;
         free(temp-&gt;next);
         temp-&gt;next = temp-&gt;next;
      }
  
  
  
      size--;
  }
  
  int LinkedList::mean()
  {
  
  
          int sum = 0;
          float avg = 0.0;
  
           Node *temp = head; 
          while (head != NULL)
          {
  
              sum += temp-&gt;data;
              temp = temp-&gt;next;
          }
  
          // calculate average
          avg = (double)sum / size;
  }
  
  void LinkedList::sort()
  {
      Node* temp = head;
  
      // Traverse the List
      while (temp) {
          Node* min = temp;
          Node* r = temp-&gt;next;
  
          // Traverse the unsorted sublist
          while (r) {
              if (min-&gt;data &gt; r-&gt;data)
                  min = r;
  
              r = r-&gt;next;
          }
  
          // Swap Data
          int x = temp-&gt;data;
          temp-&gt;data = min-&gt;data;
          min-&gt;data = x;
          temp = temp-&gt;next;
      }
  }
  </code></pre>
  
  <p>And the main</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include "linkedlist.h"
  
  using namespace std;
  
  int main()
  {
      LinkedList nums;
  
      // adding through append
      nums.append(8);
      nums.append(6);
      nums.append(7);
      nums.append(8);
      nums.append(0);
      nums.append(9);
  
      // displays list
      cout &lt;&lt; "List after append: " &lt;&lt; endl;
      nums.display();
      cout &lt;&lt; endl;
  
      // adding through insert
      nums.insert(1, 0);
      nums.insert(5, 4);
      nums.insert(3, 8);
  
  
      // displays list
      cout &lt;&lt; "List after inserting: " &lt;&lt; endl;
      nums.display();
      cout &lt;&lt; endl;
  
      // testing searching
      cout &lt;&lt; "Testing linear search:" &lt;&lt; endl;
  
      int pres = nums.linearSearch(7);
  
      if(pres &lt; 0)
      {
          cout &lt;&lt; "7 is not present in the list." &lt;&lt; endl;
      }
  
      else
      {
          cout &lt;&lt; "7 can be found at location " &lt;&lt; pres &lt;&lt; endl;
      }
  
      pres = nums.linearSearch(5);
  
      if(pres &lt; 0)
      {
          cout &lt;&lt; "5 is not present in the list." &lt;&lt; endl;
      }
  
      else
      {
          cout &lt;&lt; "5 can be found at location " &lt;&lt; pres &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  
      // does math
      cout &lt;&lt; "Minimum, maximum, and average before removing any items: " &lt;&lt; endl;
      cout &lt;&lt; "Min: " &lt;&lt; nums.min() &lt;&lt; endl;
      cout &lt;&lt; "Max: " &lt;&lt; nums.max() &lt;&lt; endl;
      cout &lt;&lt; "Mean: " &lt;&lt; nums.mean() &lt;&lt; endl &lt;&lt; endl;
  
      // displays items reversed
      cout &lt;&lt; "Items reversed: " &lt;&lt; endl;
      nums.reverse();
      nums.display();
      cout &lt;&lt; endl;
  
     // removing through pop
      nums.pop();
      nums.pop();
  
      // displays list
      cout &lt;&lt; "List after popping: " &lt;&lt; endl;
      nums.display();
      cout &lt;&lt; endl;
  
      // removing through remove
      nums.remove(0);
      nums.remove(2);
      nums.remove(4);
  
      // displays list
      cout &lt;&lt; "List after removing: " &lt;&lt; endl;
      nums.display();
      cout &lt;&lt; endl;
  
     // displays items sorted
      cout &lt;&lt; "Items sorted: " &lt;&lt; endl;
      nums.sort();
      nums.display();
      cout &lt;&lt; endl;
  
      // does math
      cout &lt;&lt; "Minimum, maximum, and average after removing items: " &lt;&lt; endl;
      cout &lt;&lt; "Min: " &lt;&lt; nums.min() &lt;&lt; endl;
      cout &lt;&lt; "Max: " &lt;&lt; nums.max() &lt;&lt; endl;
      cout &lt;&lt; "Mean: " &lt;&lt; nums.mean() &lt;&lt; endl &lt;&lt; endl;
  
      // testing searching
      cout &lt;&lt; "Testing linear search:" &lt;&lt; endl;
  
      pres = nums.linearSearch(7);
  
      if(pres &lt; 0)
      {
          cout &lt;&lt; "7 is not present in the list." &lt;&lt; endl;
      }
  
      else
      {
          cout &lt;&lt; "7 can be found at location " &lt;&lt; pres &lt;&lt; endl;
      }
  
      pres = nums.linearSearch(5);
  
      if(pres &lt; 0)
      {
          cout &lt;&lt; "5 is not present in the list." &lt;&lt; endl;
      }
  
      else
      {
          cout &lt;&lt; "5 can be found at location " &lt;&lt; pres &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>the only parts I'm really struggling with is the max, min, and mean along with getting my pop function to actually initiate. I know that the pop function is written correctly but ever since I made the max and min it wont work now. </p>