944,081 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1967
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 17th, 2007
0

Linked Lists Help C++

Expand Post »
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
**************************************************
#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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007
Mar 17th, 2007
0

Re: Linked Lists Help C++

>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)
  1. 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 17th, 2007
0

Re: Linked Lists Help C++

>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)
  1. p->next = blahblah; // same as (*p).next except it's easier on the eyes
ter
******************************************
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007
Mar 17th, 2007
1

Re: Linked Lists Help C++

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 17th, 2007
0

Re: Linked Lists Help C++

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.
//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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007
Mar 18th, 2007
0

Re: Linked Lists Help C++

>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...
C++ Syntax (Toggle Plain Text)
  1. q=first;
  2. p=(*q).next; // why set this to the next node?
  3. // do you realize that you will skip the first node?
  4.  
  5. // by comparing the 'next' node, you skip another node, so the first 2 nodes are skipped (from the first bug)
  6. while (((*p).next!= nil)&&(p->next->num < average))
  7. {
  8. q=p;
  9. p=(*p).next;
  10. }
  11. //endwhile
  12.  
  13. // because you were comparing the 'next' node,
  14. // 'p' will be one node behind and this statement will always fail
  15. if ((*p).num > average)
  16. {
  17. first = p;
  18. }
  19. else
  20. {
  21. first = nil;
  22. }
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 18th, 2007
0

Re: Linked Lists Help C++

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007
Mar 18th, 2007
0

Re: Linked Lists Help C++

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007
Mar 18th, 2007
0

Re: Linked Lists Help C++

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 18th, 2007
0

Re: Linked Lists Help C++

Thank -- not sure why you are being snotty for I think that for a first time Linked Lister, I am not doing too bad of a job.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notfornothing21 is offline Offline
10 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Including stdafx.h in other .cpp files
Next Thread in C++ Forum Timeline: variables holding operators?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC