473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4871
weaknessforcats
9,208 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

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

Similar topics

19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
9
by: wizofaus | last post by:
Is the any reason according to the standard that calling tellg() on an std::ifstream after a call to peek() could place the filebuf in an inconsistent state? I think it's a bug in the VC7...
1
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
4
by: Gianluca | last post by:
This is the simplified code I'm trying to generate: ilg.DeclareLocal(typeof(int)); // define local integer Label exit = ilg.DefineLabel(); // define "exit" label ...
1
by: Shawn | last post by:
Hi. I'm using this code to loop through all the lines in a text field: While myStreamReader.Peek() > -1 myStreamReader.ReadLine() i = i + 1 End While Now, what I need to do is to loop through...
5
by: Avi Kak | last post by:
Hello: Does Python support a peek like method for its file objects? I'd like to be able to look at the next byte in a disk file before deciding whether I should read it with, say, the read()...
4
by: Manfred Braun | last post by:
Hi All ! I think, there is a bug in the System.Console class related to use the STDIO streams. I am doing a very simple thing in a console-based program named CS Console.In.Peek(); and...
14
by: Bob Nelson | last post by:
After completing a good book on C (KNK's 2nd edition), I dusted off an oldie for a good laugh or two. It's Traister's ``Mastering C Pointers'' and I am aware of just how bad this book it. See my...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.