ordinary function

This is a discussion on ordinary function within the C++ Programming forums, part of the General Programming Boards category; I am trying to write an ordinary (non-member, non-friend) function that takes one parameter of type "List" and which will ...

  1. #1
    Unregistered
    Guest

    ordinary function

    I am trying to write an ordinary (non-member, non-friend) function that takes one parameter of type "List" and which will reverse that "List"
    Not for sure what to use, can I use the swap function?
    thanks,
    Marcus


    #ifndef LIST_H
    #define LIST_H

    #include <iostream>
    #include <cassert>
    #include "listnd.h"

    using std::cout;

    template< class NODETYPE >
    class List {
    public:
    List();
    ~List();
    void insertAtFront( const NODETYPE & );
    void insertAtBack( const NODETYPE & );
    bool removeFromFront( NODETYPE & );
    bool removeFromBack( NODETYPE & );
    bool isEmpty() const;
    void print() const;
    private:
    ListNode< NODETYPE > *firstPtr;
    ListNode< NODETYPE > *lastPtr;

    ListNode< NODETYPE > *getNewNode( const NODETYPE & );
    };

    template< class NODETYPE >
    List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }

    template< class NODETYPE >
    List< NODETYPE >::~List()
    {
    if ( !isEmpty() ) {
    cout << "Destroying nodes ...\n";

    ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;

    while ( currentPtr != 0 ) {
    tempPtr = currentPtr;
    cout << tempPtr->data << '\n';
    currentPtr = currentPtr->nextPtr;
    delete tempPtr;
    }
    }

    cout << "All nodes destroyed\n\n";
    }

    template< class NODETYPE >
    void List< NODETYPE >::insertAtFront( const NODETYPE &value )
    {
    ListNode< NODETYPE > *newPtr = getNewNode( value );

    if ( isEmpty() )
    firstPtr = lastPtr = newPtr;
    else {
    newPtr->nextPtr = firstPtr;
    firstPtr = newPtr;
    }
    }

    template< class NODETYPE >
    void List< NODETYPE >::insertAtBack( const NODETYPE &value )
    {
    ListNode< NODETYPE > *newPtr = getNewNode( value );

    if ( isEmpty() )
    firstPtr = lastPtr = newPtr;
    else {
    lastPtr->nextPtr = newPtr;
    lastPtr = newPtr;
    }
    }

    template< class NODETYPE >
    bool List< NODETYPE >::removeFromFront( NODETYPE &value )
    {
    if ( isEmpty() )
    return false;
    else {
    ListNode< NODETYPE > *tempPtr = firstPtr;

    if ( firstPtr == lastPtr )
    firstPtr = lastPtr = 0;
    else
    firstPtr = firstPtr->nextPtr;

    value = tempPtr->data;
    delete tempPtr;
    return true;
    }
    }

    template< class NODETYPE >
    bool List< NODETYPE >::removeFromBack( NODETYPE &value )
    {
    if ( isEmpty() )
    return false;
    else {
    ListNode< NODETYPE > *tempPtr = lastPtr;

    if ( firstPtr == lastPtr )
    firstPtr = lastPtr = 0;
    else {
    ListNode< NODETYPE > *currentPtr = firstPtr;

    while ( currentPtr->nextPtr != lastPtr )
    currentPtr = currentPtr->nextPtr;

    lastPtr = currentPtr;
    currentPtr->nextPtr = 0;
    }

    value = tempPtr->data;
    delete tempPtr;
    return true;
    }
    }


    template< class NODETYPE >
    bool List< NODETYPE >::isEmpty() const
    { return firstPtr == 0; }


    template< class NODETYPE >
    ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
    const NODETYPE &value )
    {
    ListNode< NODETYPE > *ptr =
    new ListNode< NODETYPE >( value );
    assert( ptr != 0 );
    return ptr;
    }

    template< class NODETYPE >
    void List< NODETYPE >:rint() const
    {
    if ( isEmpty() ) {
    cout << "The list is empty\n\n";
    return;
    }

    ListNode< NODETYPE > *currentPtr = firstPtr;

    cout << "The list is: ";

    while ( currentPtr != 0 ) {
    cout << currentPtr->data << ' ';
    currentPtr = currentPtr->nextPtr;
    }

    cout << "\n\n";
    }

    #endif

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Your reverse function could start at the existing front of your list and iterate through it removing each node in turn and re-inserting each one using your insertAtFront method.
    zen

  3. #3
    Unregistered
    Guest

    reverse?

    rbegin? Or could I use a pus_back?

    Or how is this, I could be way offfff here.

    nodedtype reverse_list<vector<T>::List,
    T,
    listnode<T>::reference_list,
    listnode<T>::difference_type> reverse_list; []
    reverse_list insiertatfront(end());
    reverse_list insertatback(begin());

  4. #4
    Unregistered
    Guest
    Personally, I would use two for loops that go through each list and put the new values into a third list.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Zen has the best idea with the smallest amount of code to be used. Very simple and very easy.
    SilasP

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    Your implementation do not allow extrnal access to 'first' and 'last' pointers in the List class so in order to write external, non-friend function that reverse a list you should use only the public part of the List class - e.g. insertAtXXX and removeFromXXX menods

    so here is one possible solution
    Code:
    template< class NODETYPE>
    void reverse(List< NODETYPE >& list)
    {
       List< NODETYPE > local;
       NODETYPE value;
       // remove them and reverse
       while(list.removeFromFront(value))
          local.InsertAtFront(value);
      // put them back
       while(local.removeFromFront(value))
          list.InsertAtBack(value);
    }
    I know that it is dummy but ... :-)

    damyan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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