Linked Lists Help C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Linked Lists Help C++

 
0
  #1
Mar 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Linked Lists Help C++

 
0
  #2
Mar 17th, 2007
>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:
  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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Re: Linked Lists Help C++

 
0
  #3
Mar 17th, 2007
Originally Posted by joeprogrammer View Post
>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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Linked Lists Help C++

 
1
  #4
Mar 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Re: Linked Lists Help C++

 
0
  #5
Mar 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Linked Lists Help C++

 
0
  #6
Mar 18th, 2007
>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...
  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. }
"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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Re: Linked Lists Help C++

 
0
  #7
Mar 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Re: Linked Lists Help C++

 
0
  #8
Mar 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Linked Lists Help C++

 
0
  #9
Mar 18th, 2007
>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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: notfornothing21 is an unknown quantity at this point 
Solved Threads: 0
notfornothing21 notfornothing21 is offline Offline
Newbie Poster

Re: Linked Lists Help C++

 
0
  #10
Mar 18th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1702 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC