Peek method for my Stack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blahblahblahCCCC
    New Member
    • Mar 2008
    • 1

    Peek method for my Stack

    This is one of those brain farts i always get.. but i need a peek method in this stack class and am having horrible time figuring out how to do it correctly can someone help me out plz
    i think it should look something like
    void peek(stack* head)
    {
    if(head!=NULL)
    {
    while(head->next!=NULL)
    {
    cout<<head->element<<"->";
    head=head->next;
    }
    cout<<head->element;
    cout<<endl;
    }
    else
    cout<<"the stack is empty\n";
    }


    but it doesnt work like i think it should...
    but below is the code i have so if someone could help it would be much appreciated..




    #ifndef STACK_H
    #define STACK_H

    #include "List.h"

    template< typename STACKTYPE >
    class Stack : private List< STACKTYPE >
    {
    public:

    void push(const STACKTYPE &data)
    {
    insertAtFront(d ata);
    }

    bool pop( STACKTYPE &data)
    {
    return removeFromFront (data);
    }

    bool isStackEmpty() const
    {
    return List<STACKTYPE> ::isEmpty();
    }
    void printStack() const
    {
    List<STACKTYPE> ::print();
    }
    };
    #endif

    // I use this linkedlist to implement my stack
    #ifndef LIST_H
    #define LIST_H

    #include <iostream>
    using std::cout;

    #include "ListNode.h "

    template< typename NODETYPE >
    class List
    {
    public:
    List();
    ~List();
    void insertAtFront( const NODETYPE &);
    bool removeFromFront ( NODETYPE & );
    bool isEmpty() const;
    void print() const;

    private:
    ListNode<NODETY PE> *firstPtr;
    ListNode<NODETY PE> *lastPtr;


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

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

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

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

    while(currentPt r != 0)
    {

    tempPtr = currentPtr;
    cout << tempPtr->data << '\n';
    currentPtr = currentPtr->nextPtr;
    delete tempPtr;
    }
    }
    cout << "All nodes destroyed\n\n";
    }

    template< typename NODETYPE >
    void List< NODETYPE >::insertAtFron t( const NODETYPE &value)
    {
    ListNode< NODETYPE > *newPtr = getNewNode( value );

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

    template <typename NODETYPE >
    bool List< NODETYPE >::removeFromFr ont( 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< typename NODETYPE>
    bool List< NODETYPE >::isEmpty() const
    {
    return firstPtr == 0;
    }



    template< typename NODETYPE >
    ListNode< NODETYPE > *List< NODETYPE >::getNewNode ( const NODETYPE &value)
    {
    return new ListNode< NODETYPE >(value);
    }

    template< typename NODETYPE>
    void List< NODETYPE>::prin t() const
    {
    if( isEmpty())
    {
    cout << "The list is empty\n\n";
    return;
    }
    ListNode< NODETYPE> *currentPtr = firstPtr;
    cout<<"The List is: ";
    while(currentPt r != 0)
    {
    cout << currentPtr->data << ' ';
    currentPtr = currentPtr->nextPtr;
    }
    cout << "\n\n";
    }

    #endif


    //ListNode Class
    #ifndef LISTNODE_H
    #define LISTNODE_H

    template< typename NODETYPE > class List;

    template < typename NODETYPE>
    class ListNode
    {
    friend class List< NODETYPE >;

    public:
    ListNode( const NODETYPE & );
    NODETYPE getData() const;
    private:
    NODETYPE data;
    ListNode< NODETYPE > *nextPtr;
    };

    template< typename NODETYPE>
    ListNode< NODETYPE >::ListNode( const NODETYPE &info )
    : data( info ), nextPtr( 0 )
    {

    }

    template< typename NODETYPE >
    NODETYPE ListNode< NODETYPE >::getData() const
    {
    return data;
    }

    #endif
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    How can you peek when the function returns void?

    All you do is return the data in the head node.

    Comment

    Working...