473,614 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Peek method for my Stack

1 New Member
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
Mar 12 '08 #1
1 4880
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
10578
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 do readline(). Is there a way to do this? for example: while 1: line=stdin.peek_nextline()
9
5430
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 dinkumware implementation (and I've reported to them as such), but the following code std::ofstream ofs("test.txt"); ofs << "0123456789"; ofs.close(); std::wifstream ifs("test.txt");
1
18377
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 or change the value of the Position property, but this does not seem to affect the Peek() method, which keeps returning -1 as if it had not been notified that the stream pointer has been repositioned to file beginning. How can I let Peek() work...
4
3063
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 ilg.Emit(OpCodes.Ldc_I4_0); // put 0 on the stack ilg.Emit(OpCodes.Stloc_0); // set the local integer to 0 ilg.Emit(OpCodes.Ldloc_0); // load the local integer (0) on the stack ilg.Emit(OpCodes.Ldc_I4_1); // load integer 1 on the stack
1
15521
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 all the lines one more time and do the work that I'm supposed to do. The reason for this is that I have to know how many lines exists in the text file in order to display a ProgressBar to the user. I've tried setting
5
4050
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() method. Is it possible to do so in Python? Your answer would be much appreciated.
4
4489
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 the program hungs if no parameters were provided on the commandline. If I use simple redirection like "echo "hallo" | cs.exe"
14
5259
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 posting from 10 years ago here in c.l.c. Knowing the dubious quality of the book, what he writes on page 78 may be fiction (but there's a chance it may have been true): ``The original C programming language contained a peek() function ''
2
2282
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 itertools.chain(short sequences) or put the short sequences as the first zip arg. To test without consuming, wrap the iterator in a trivial-to-write one_ahead or peek class such as has been posted before.
0
8198
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8294
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8444
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.