help with classes and member functions

This is a discussion on help with classes and member functions within the C++ Programming forums, part of the General Programming Boards category; i am just looking for help to understand this. in order to add the member functions i need to enter ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    help with classes and member functions

    i am just looking for help to understand this. in order to add the member functions i need to enter the function in public (not sure as to how) ie, bool Bsort() or int Bsort() and i am also asking if i put the actual sort code in after the urinary operator/constructer for the Bsort or does it go somewhere else. i have to compile on g++.


    1. Add two member functions to the List class performing two separate sorting operations.
    2. Use the functions in step(2) to sort the linked list of step (1).


    #include <iostream.h>
    //typedef int bool; // if your compiler doesn't support boolean type uncomment this
    typedef int ListElement;
    struct cell // the basic building block of a linked list
    {
    ListElement item; // the data in this cell
    struct cell *next; // the pointer to link to the next cell
    };
    typedef struct cell* cellptr; // define the type of the pointer to a cell
    class List
    {
    private:
    cellptr header; // the header pointer of the linked list
    public:
    List();
    ~List();
    bool IsListEmpty();
    int Length();
    bool ListInsert(ListElement);
    bool ListDelete(ListElement);
    cellptr Retrieve(ListElement);
    void PrintList();
    int SumList();
    float AverageList();
    bool Swap (cellptr p1, cellptr p2);
    bool Reverse ();
    };
    List::List():header(NULL)
    {
    };
    bool List::IsListEmpty()
    {
    return(header == NULL);
    };
    List::~List()
    {
    cellptr temp = header;
    while(temp!=NULL)
    {
    header = temp->next;
    temp->next = NULL;
    delete temp;
    temp = header;
    }
    header = NULL;
    };
    int List::Length()
    {
    cellptr temp = header;
    int counter=0;
    while(temp!=NULL)
    {
    counter++;
    temp=temp->next;
    }
    return(counter);
    };
    bool List::ListInsert(ListElement n)
    {
    cellptr temp = new struct cell;
    if(temp==NULL)
    {
    cout << "Not enough memory!" << endl;
    return(0);
    }
    else
    {
    temp->item=n;
    temp->next=header;
    header=temp;
    return(1);
    }
    }
    bool List::ListDelete(ListElement n)
    {
    cellptr temp1 = header, temp2;
    if(temp1==NULL)
    {
    cout << "Empty list!" << endl;
    return(0);
    }
    else if(temp1 -> item==n)
    {
    header=temp1 -> next;
    temp1->next=NULL;
    delete temp1;
    return(1);
    }
    else
    {
    temp2=temp1;
    temp1=temp1->next;
    while((temp1 != NULL)&&(temp1 -> item != n))
    {
    temp2=temp1;
    temp1=temp1->next;
    }
    if(temp1==NULL)
    {
    cout << "Element " << n << " not found!" << endl;
    return(0);
    }
    else
    {
    temp2->next = temp1->next;
    temp1->next=NULL;
    delete temp1;
    return(1);
    }
    }
    }

    // The retrieve function locates a list element in the list and returns
    // the pointer to the element. If the element is not found it returns NULL.
    cellptr List::Retrieve(ListElement n)
    {
    cellptr temp = header;
    if(temp==NULL)
    {
    cout << "Empty list!" << endl;
    return(NULL);
    }
    else
    {
    while((temp != NULL)&&(temp -> item != n))
    temp=temp->next;
    if(temp==NULL)
    {
    cout << "Element " << n << " not found!" << endl;
    return(NULL);
    }
    else
    return(temp);
    }
    }
    void List::PrintList()
    {
    cellptr temp = header;
    cout << "Showing the list:" << endl;
    while(temp!=NULL)
    {
    cout << temp -> item << endl;
    temp=temp->next;
    }
    }
    //Finds the sum of elements in the linked list
    int List::SumList()
    {
    int sum;
    cellptr temp = header;
    sum = 0;
    while(temp != NULL)
    {
    sum = sum + temp -> item;
    temp = temp -> next;
    }
    cout<< "The sum of the elements in the linked list is: ";
    cout<<sum<<endl;
    return sum;
    }
    //Finds the average of elements in the linked list
    float List::AverageList()
    {
    float Avg;
    Avg = float (SumList())/Length();
    cout<<"The list average is :"<<Avg<<endl;
    return Avg;
    }
    //Swaps the element pointed by p1 with that pointed by p2
    bool List::Swap ( cellptr p1, cellptr p2)
    {
    cellptr temp = header;
    int temporary;
    temporary = p2 -> item;
    p2 -> item = p1 -> item;
    p1 -> item = temporary;
    return 1;
    }
    //Reverses the elements in the linked list
    bool List::Reverse ()
    {
    cellptr temp = header;
    int size = Length(), i;
    for (i=0; i<size/2; i++)
    {
    Swap(Retrieve(i), Retrieve(size-1-i));
    temp = temp ->next;
    }
    return 1;
    }
    main()
    {
    List l;
    for(int i=0; i<10; i++)
    l.ListInsert(i);
    l.PrintList();
    /*l.ListDelete(5);
    l.ListDelete(9);
    l.ListDelete(11);
    l.ListInsert(300);
    l.ListInsert(101);
    l.PrintList();
    */
    //Finds the sum of elements in the linked list
    l.SumList();
    l.PrintList();
    //Finds the average of the elements in the linked list
    l.AverageList();
    l.PrintList();
    //Reverses the elements in the linked list
    cout<<"Here is the reversed list - "<<endl;
    l.Reverse();
    l.PrintList();
    //Swaps the fifth element with the 7th element
    cout<<"Here is the list when elements 5 and 7 are swapped - "<<endl;
    l.Swap ( l.Retrieve (5), l.Retrieve (7));
    l.PrintList();
    }
    /*
    Showing the list:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Here is the list when elements 5 and 7 are swapped -
    Showing the list:
    0
    1
    2
    3
    4
    7
    6
    5
    8
    9
    */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i am just looking for help to understand this. in order to add the member functions i need to enter the function in public (not sure as to how) ie, bool Bsort() or int Bsort() and i am also asking if i put the actual sort code in after the urinary operator/constructer for the Bsort or does it go somewhere else. i have to compile on g++.
    Just stick them in there some place. It doesn't matter WHERE they're at. Just as long as they're tagged as public, if they're supposed to be public, or private (or protected) if they're supposed to be private...

    Learn how to use code tags also:
    [code]
    ...Your code goes here...
    [/code]

    Something like this:

    Code:
    class MyClass
    {
        public:
            MyClass( );
            ~MyClass( );
    
            MyAlreadyExistingFunction( );
            MyNewFunction( );
            MySecondExistingFunction( );
        private:
            int privateInt;
    
            MyNewPrivateFunction( );
    };
    It doesn't matter where you stick them, just as long as they're under the right access tag.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    help again

    ok i got that part however i am not sure how to phrase it in this case... i need to do a sort function but do i code it as bool or void? also, does the exact sort code get placed in the body of the constructor? i have tried such and keep getting errors.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: help again

    Originally posted by syner
    ok i got that part however i am not sure how to phrase it in this case... i need to do a sort function but do i code it as bool or void? also, does the exact sort code get placed in the body of the constructor? i have tried such and keep getting errors.
    If you're creating a seperate sort function, then, no, you don't want this in the constructor. You'd want the sorting done in the actual sort function body.

    The only reason you'd need to define your function as one that returns a boolean, is if you want to check to see if the sort fails or not.

    If there is no way for the sort to fail, then why bother returning anything?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    are u on aim or anything by chance?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 08:01 PM
  2. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 08:38 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  4. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 10:30 AM
  5. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21