| | |
Linked Lists Help C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
Hi -- I am writing a program taht is supposed to input 12 integers into a program, sort them as they are entered, and then Print out the Sorted list. Second step is compute the average. I have done all that.
THIRD is to delete all the integers that are less than the average. I am having trouble with that part. The entire program is below and it runs great when I take out the "Delete less than " information in Int Main. I only get 2 errors, but still won't run. Can someone point me in the right direction? I am a beginner on Linked Lists.
Thanks
**************************************************
THIRD is to delete all the integers that are less than the average. I am having trouble with that part. The entire program is below and it runs great when I take out the "Delete less than " information in Int Main. I only get 2 errors, but still won't run. Can someone point me in the right direction? I am a beginner on Linked Lists.
Thanks
**************************************************
#include <iostream> #include <fstream> usingnamespace std; //Functions void GetData (int& number); constint nil=0; class node_type // Declaration of Class { public: int num; // Numbers that we are entering node_type *next; }; int main () { node_type *first, *p, *q, *newnode, *w; // Introducing the Nodes int i; int number; int sum, average; sum = 0; first = new node_type; p=first; GetData(number); (*first).num = number; (*first).next = nil; for (i=1; i<=11; i++) { GetData (number); // Input Numbers newnode = new node_type; (*newnode).num=number; (*newnode).next=nil; (*p).next = newnode; // Links previous Node to new Node // p=newnode; } bool sw=false; while(!sw)//you do this till there are no adjacent numbers in reverse order { sw=true; if (first->num>first->next->num) { q=first->next; first->next=q->next; q->next=first; first=q; sw=false; } q=first; while (q->next->next) { p=q->next; w=p->next; if (p->num>w->num) { q->next=w; p->next=w->next; w->next=p; sw=false; } q=q->next; } } q=first; cout<<" The ordered numbers are: \n"; while (q != nil) { cout << (*q).num << "\n "; sum = sum + (*q).num; q = (*q).next; }//endwhile //Compute Average average = sum/12; cout << "The Average of the Numbers entered is " <<average<< "\n"; //Delete Nodes from List if ((*first).num >=average) { cout << "Nothing to Delete \n" ; } else { q=first; p=(*q).next; while (((*p).next != nil)&&((*p).next < average)) { q=p; p=(*p).next; } //endwhile if ((*p).num > average) { first = p; } else { first = nil; } } //end if //end if //end if return 0; } void GetData(int& number) { cout<< "Enter an integer \n"; cin>>number; cout<< number << "\n"; }
Last edited by ~s.o.s~; Mar 17th, 2007 at 11:57 pm. Reason: Added code tags, learn to use them.
>
You're comparing a pointer with an integer. It's exactly what the my compiler said about that line. You should compare it with the value of 'num' contained in that node that you're currently comparing it with...
Secondly, you don't have to always use the syntax (*pointer).member, instead there's a shorthand version ->. let me show you an example:
while (((*p).next != nil)&&((*p).next < average))You're comparing a pointer with an integer. It's exactly what the my compiler said about that line. You should compare it with the value of 'num' contained in that node that you're currently comparing it with...
Secondly, you don't have to always use the syntax (*pointer).member, instead there's a shorthand version ->. let me show you an example:
C++ Syntax (Toggle Plain Text)
p->next = blahblah; // same as (*p).next except it's easier on the eyes
Last edited by John A; Mar 17th, 2007 at 8:59 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
>while (((*p).next != nil)&&((*p).next < average))
You're comparing a pointer with an integer. It's exactly what the my compiler said about that line. You should compare it with the value of 'num' contained in that node that you're currently comparing it with...
Secondly, you don't have to always use the syntax (*pointer).member, instead there's a shorthand version ->. let me show you an example:
C++ Syntax (Toggle Plain Text)
p->next = blahblah; // same as (*p).next except it's easier on the eyes
******************************************
I guess I am confused as to how I would compare the average i just computed that is an integer to one of the integers that is in the info field of the nodes that are sorted. Would I have to insert that average into a node in the list? Am I missing something? I understand the concept and the flow, but for some reason just don't know how to compare the two. Thanks for the help.
What I'm saying is that you can't just compare p->next, because that's a pointer. You would want to compare the average to p->next->num, which is the actual value contained in the node.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
Wow -- thanks I did not even realize I was pulling from the next feild instead of "info" field on that one...lol...
My new code is here. Only prob is it is not printing the new list after the numbers are deleted that are below average.
My new code is here. Only prob is it is not printing the new list after the numbers are deleted that are below average.
//Delete Nodes from List if ((*first).num >=average) { cout << "Nothing to Delete \n" ; } else { q=first; p=(*q).next; while (((*p).next!= nil)&&(p->next->num < average)) { q=p; p=(*p).next; } //endwhile if ((*p).num > average) { first = p; } else { first = nil; } } //end if //end if //end if q=first; cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n"; while (q != nil) { cout << (*q).num << "\n "; sum = sum + (*q).num; q = (*q).next; } //endwhile return 0; }
Last edited by ~s.o.s~; Mar 18th, 2007 at 12:00 am. Reason: Learn to use code tags. Last warning.
>Only prob is it is not printing the new list after the numbers are deleted that are below average.
You've got a few little bugs below...
You've got a few little bugs below...
C++ Syntax (Toggle Plain Text)
q=first; p=(*q).next; // why set this to the next node? // do you realize that you will skip the first node? // by comparing the 'next' node, you skip another node, so the first 2 nodes are skipped (from the first bug) while (((*p).next!= nil)&&(p->next->num < average)) { q=p; p=(*p).next; } //endwhile // because you were comparing the 'next' node, // 'p' will be one node behind and this statement will always fail if ((*p).num > average) { first = p; } else { first = nil; }
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
Thank you so much. Basically with your help you reminded me to draw it out and walk through what I was doing on my program. It was very helpful. Here is my new program - it runs good but if you see any more errors that may interfere with my next few steps I have to write, please let me know. Thanks again.
//Delete Nodes from List if ((*first).num >=average) { cout << "Nothing to Delete \n" ; } else { q=first; p=q; while (((*p).next!= nil)&&(p->num<average)) { first= (*p).next; p=(*p).next; q=p; } //endwhile if ((*p).num >= average) { first = p; } else { first = nil; } } //end if //end if //end if q=first; cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n"; while (q != nil) { cout << (*q).num << "\n "; sum = sum + (*q).num; q = (*q).next; } //endwhile
Last edited by ~s.o.s~; Mar 18th, 2007 at 1:12 pm. Reason: Added code tags.
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
Okay so one last question! The next part of the problem was to input a second set of integers (6 this time) just like the first round and sort as I input them. I copied the first part of the problem's program, but it is coming out different. Any ideas? Here is the entire code, if you compile you will see. Thanks again.
#include <iostream> #include <fstream> usingnamespace std; //Functions void GetData (int& number); constint nil=0; class node_type // Declaration of Class { public: int num; // Numbers that we are entering node_type *next; }; int main () { node_type *first, *p, *q, *newnode, *w; // Introducing the Nodes int i; int number; int sum, average; sum = 0; first = new node_type; p=first; GetData(number); (*first).num = number; (*first).next = nil; // Inputting first list of Numbers (12 Integers)and Sorting them for Output for (i=1; i<=11; i++) { GetData (number); // Input Numbers newnode = new node_type; (*newnode).num=number; (*newnode).next=nil; (*p).next = newnode; // Links previous Node to new Node // p=newnode; } bool sw=false; while(!sw)//you do this till there are no adjacent numbers in reverse order { sw=true; if (first->num>first->next->num) { q=first->next; first->next=q->next; q->next=first; first=q; sw=false; } q=first; while (q->next->next) { p=q->next; w=p->next; if (p->num>w->num) { q->next=w; p->next=w->next; w->next=p; sw=false; } q=q->next; } } q=first; cout<<" The First List of ordered numbers are: \n"; while (q != nil) { cout << (*q).num << "\n "; sum = sum + (*q).num; q = (*q).next; }//endwhile //Compute Average of First List of Numbers average = sum/12; cout << "The Average of the Numbers entered is " <<average<< "\n"; //Delete Nodes from First List of Numbers if ((*first).num >=average) { cout << "Nothing to Delete \n" ; } else { q=first; p=q; while (((*p).next!= nil)&&(p->num<average)) //Goes through List finding those less than average and Deleting less than { first= (*p).next; p=(*p).next; q=p; } //endwhile if ((*p).num >= average) // Once Get through the sorted list of Integers that are less than average, reposition First Pointer to stay. // This will start the new output list { first = p; } else { first = nil; } } //end if //end if //end if q=first; cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n"; while (q != nil) { cout << (*q).num << " \n "; sum = sum + (*q).num; q = (*q).next; } //endwhile cout<<"\n "; //Inputting Second List of Numbers (6 Integers) sorting as the List is Created for (i=1; i<=6; i++) { GetData (number); // Input Numbers newnode = new node_type; (*newnode).num=number; (*newnode).next=nil; (*p).next = newnode; // Links previous Node to new Node // p=newnode; } while(!sw)//you do this till there are no adjacent numbers in reverse order { sw=true; if (first->num>first->next->num) { q=first->next; first->next=q->next; q->next=first; first=q; sw=false; } q=first; while (q->next->next) { p=q->next; w=p->next; if (p->num>w->num) { q->next=w; p->next=w->next; w->next=p; sw=false; } q=q->next; } } q=first; cout<<" The Second List List of sorted numbers are: \n "; while (q != nil) { cout << (*q).num << "\n "; sum = sum + (*q).num; q = (*q).next; }//endwhile cout<<"\n"; return 0; } void GetData(int& number) { cout<< "Enter an integer \n"; cin>>number; cout<< number << "\n"; }
Last edited by ~s.o.s~; Mar 18th, 2007 at 1:14 pm. Reason: Added code tags with a warning.
>I copied the first part of the problem's program, but it is coming out different.
Copying and pasting is a BAD idea, and should be avoided whenever possible.
>Any ideas?
Well, you could start by taking your original program that works, and splitting the parts of code into functions. Then once you have that working, all you have to do is call the functions from the main() function. It's also a lot easier to see your mistakes when you've split up the code.
And by the way, it's very annoying when you don't use code tags, so if you started using them, it would be much-appreciated. Thanks.
Copying and pasting is a BAD idea, and should be avoided whenever possible.
>Any ideas?
Well, you could start by taking your original program that works, and splitting the parts of code into functions. Then once you have that working, all you have to do is call the functions from the main() function. It's also a lot easier to see your mistakes when you've split up the code.
And by the way, it's very annoying when you don't use code tags, so if you started using them, it would be much-appreciated. Thanks.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- strcpy and linked lists (C++)
- Concordance, Linked Lists and classes (C++)
- help with linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: Including stdafx.h in other .cpp files
- Next Thread: variables holding operators?
Views: 1702 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






