Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,578 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,917 people online right now. Registration is fast and FREE... Join Now!




Odd and Even Function

 
Reply to this topicStart new topic

Odd and Even Function

shampaynes
25 Sep, 2008 - 07:47 PM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 5

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?
User is offlineProfile CardPM
+Quote Post

Soura
RE: Odd And Even Function
25 Sep, 2008 - 08:10 PM
Post #2

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 27


My Contributions

QUOTE(shampaynes @ 25 Sep, 2008 - 08:47 PM) *



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?

The Program is-


CODE
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

main()
{
      int a[10], i, n=1;
      for(i=1;i<=10;i++)
      {printf("Enter The NUMBER %d=",n);
      scanf("%d",&a[i]);
      n+=1;}
      printf("Odd Numbers\tEven Numbers\n");
      for(i=1;i<=10;i++)
      {if(a[i]%2==0)
      {printf("\t\t%d\n", a[i]);}
      else
      {printf("%d \n", a[i]);}
      }
      getch();
}
/*End Of Main*/

It works only with 10 No.s.
Please try this.

This post has been edited by Soura: 25 Sep, 2008 - 08:11 PM
User is offlineProfile CardPM
+Quote Post

shampaynes
RE: Odd And Even Function
26 Sep, 2008 - 12:25 AM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 5

Thanks I know how to do that it the way you suggested. I was just having difficulting doing it the structure way..I realized that I couldn't convert the structure into an integer. Also, I couldn't return anything in void. I think I fixed it I have to execute and make sure...thanks a lot tho


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month