GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Sep-2008, 22:09
shampaynes shampaynes is offline
New Member
 
Join Date: Feb 2008
Posts: 4
shampaynes is on a distinguished road

Odd and Even Function


CPP / C++ / C Code:
#include <iostream>
using namespace std;


struct TNode
	{
	int Data;
	TNode *Next;
	};


void PrintList (TNode *list);
TNode *AddData (TNode *list, int data);
void FreeList (TNode *list);



///////////////////////////////////////////////////////
void PrintList (TNode *list)
	{
	TNode *p;
	for (p=list; p!=0; p=p->Next)
		{
		cout<<p->Data<<endl;
		}
	}



//////////////////////////////////////////////////////
TNode *AddData (TNode *list, int data)
	{
	TNode *newnode;
	newnode= new TNode;
	if (newnode ==0)
		{
		cout<<"Memory Failure"<<endl;
		}

	newnode->Next =list;
	newnode->Data =data;
	return newnode;
	}


///////////////////////////////////////////////////////
int GetMax(TNode *list)
	{
	if (list==0)
		{
		return 0;
		}

	int max;
	max= list-> Data;
    
	
	TNode *p;
	for (p=list;p!=0; p=p->Next)
		{
		if (max < p->Data)
			{
			max=p->Data;
			}
		}

	return max;
	}

///////////////////////////////////////////////////////////
TNode *ReverseList (TNode *list)
	{
	if (list==0)
		{
		return 0;
		}

	TNode *next;
	TNode *newlist=0;
	TNode *p;

	for(p=list->Next; p!=0; p=next)
		{
		next= p->Next;
		p->Next=newlist;
		newlist=p;
		}

	return newlist;
	}





////////////////////////////////////////////////////
void FreeList (TNode *list)
	{
	TNode *p;
	TNode *Next;
	for (p=list; p!=0; p=Next)
		{
		Next= p->Next;
		delete p;
		}
	}



/////////////////////////////////////////////////////////////////
void GetOddEven (TNode *list, TNode*&oddlist, TNode*&evenlist)
	{

        TNOde *finallist;
	TNode *p;
	TNode *Next;
	for (p=list; p!=0;p=Next)
		{
		Next =p->Next;
		p->Next= finallist;
                finallist = p;

                if (finallist %2==1)
			{
		         oddlist=finallist;
                         return oddlist;

			}
                else if (finallist %2==0)
			{
	                evenlist=finallist;
                        return evenlist;
			}
		}
	}

	
//////////////////////////////////////////////////////


int main (void)
	{
	TNode *list;
	list= 0;
	for (; ;)
		{
		int val;
		cout<<"Enter #";
		cin>>val;
		if(val<=0)
			{
			break;
			}
		list = AddData (list, val);
		}


	PrintList(list);
	cout<<"Max="<<GetMax(list)<<endl;

	list=ReverseList(list);

	PrintList(list);
    
	TNode *oddlist;
	TNode *evenlist;

             cout<<GetOddEven(list,oddlist,evenlist)<<endl;
	PrintList(list);


	FreeList(list);

	return 0;

	}
		

Okay there are several things going on in this program of mine. First I let numbers be entered then print out it as a list. Then I print out the largest number entered. After that I reverse the entered list. Now I'm trying to split the list into odd and even list. But I keep getting errors with my odd and even function. Can anyone help me?
  #2  
Old 25-Sep-2008, 23:09
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Odd and Even Function


Quote:
Originally Posted by shampaynes
Can anyone help me?
Numerous comments:
  • Your formating style is, er, um, unique.
  • Function AddData() checks for errant memory allocation, & then proceeds as if nothing wrong happens.
  • You need to rethink your for-loop within function ReverseList(). Consider what happens is the list coming into this function consists of only one node.
  • You need to rethink your for-loop within function FreeList(). Consider what happens is the list coming into this function consists of only one node.
  • You have a syntax error in function GetOddEven(). TNode is misspelled.
  • The logic within function GetOddEven() needs to be overhauled:
    • Decide whether the original list is to be left intact or destroyed.
    • Traverse the original list once. Check each node, & if even create a new node & add to the even list, or if odd, create a new node & add to the odd list. If the original list is to be destroyed, move each node instead of creating a new one.
  • At the end of execution in main(), you potentially have three lists. Destroy each one of them.
One of the standard techniques used in troubleshooting is to reduce the code down to the simplest case which generates the error, fix the error, & gradually add more code. Generally, it is best to debug as you proceed so the task isn't quite as daunting in the end.
  #3  
Old 26-Sep-2008, 03:16
shampaynes shampaynes is offline
New Member
 
Join Date: Feb 2008
Posts: 4
shampaynes is on a distinguished road

Re: Odd and Even Function


thanks for the advice...but my teacher told me not to modify the functions..only create the odd even function..I saw my syntax error. But I realized it was the return that I made in the void function and my error was trying to convert a structure to an int..I fixd it and it works.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.