Okay everyone I managed to do a linked list I did a couple of comparisons to get a maximum and a minimum out of the code observe
Now my question is does anybody know a better way to sort through a linked list to get a minimumCode:#include <iostream> #include <cstring> using namespace std; //linked list first example struct node { int info; node *next; //a pointer named node of size node(the structure in which it is contained) }; int main() { int d=0, max, min, counter=0,a[5]; node *list; //a pointer called list of size node node *p; //a pointer called p of size node list = new(node); //have list point to a new address p=list; //make both of those adresses the same for(int i=0; i<5; ++i) { cout<<"Enter the value of this node: "<<endl; cin>>p->info; //enter a value for the first d=d+p->info; a[i]=p->info; counter++; if(p->info > max) { max=p->info; } p->next=new(node); p=p->next; } min=list->info; //set the first node as the min if(list->next->info < list->info) //compare 2nd node //if smaller than first node it is min min=list->next->info; if(list->next->next->info <list->info && list->next->next->info < list->next->info) //compare 3rd node if smaller than 1st or 2nd node //3rd node becomes the min min=list->next->next->info; if(list->next->next->next->info < list->info && list->next->next->next->info < list->next->next->info //compare 4th node if smaller than than previous && list->next->next->next->info < list->next->info) //nodes 4th node becomes the min min=list->next->next->next->info; if(list->next->next->next->next->info < list->info && list->next->next->next->next->info < list->next->next->next->info //compare 5th node if smaller than all && list->next->next->next->next->info < list->next->next->info //5th node becomes the min && list->next->next->next->next->info < list->next->info) min=list->next->next->next->next->info; ; cout<<"THIS IS THE MAXIMUM NUMBER "<<max<<endl; cout<<"THE AVERAGE IS: "<<d/counter<<endl; cout<<"THE NUMBER OF VARIABLES IN THIS LINKED LIST IS "<<counter<<endl; cout<<"THE MINIMUM IS "<<min<<endl; cout<<"THE FIVE NODES ARE "<<list->info <<" "<<list->next->info <<" "<<list->next->next->info <<" "<<list->next->next->next->info <<" "<<list->next->next->next->next->info<<" "<<endl; return 0; }



LinkBack URL
About LinkBacks


