468,512 Members | 1,402 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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(data);
}

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<NODETYPE> *firstPtr;
ListNode<NODETYPE> *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(currentPtr != 0)
{

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

template< typename 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 <typename 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< 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>::print() 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


//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
Mar 12 '08 #1
1 4602
weaknessforcats
9,207 Expert Mod 8TB
How can you peek when the function returns void?

All you do is return the data in the head node.
Mar 12 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by les_ander | last post: by
9 posts views Thread by wizofaus | last post: by
1 post views Thread by Dan | last post: by
4 posts views Thread by Gianluca | last post: by
1 post views Thread by Shawn | last post: by
4 posts views Thread by Manfred Braun | last post: by
14 posts views Thread by Bob Nelson | last post: by
2 posts views Thread by Terry Reedy | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.