C++ Stack Implementation

Continuing with the theme of implementing elementary data structures, I pumped out a quick stack implementation in C++.

#include <stdio.h>
#include <iostream>

class stack{
int stack[100]; // allocate a maximum of 101 information slots
int t= 0; //top of stack
void push(int x){
stack[t]=x;
t++; // add error exception for full stack
};
int pop(){
return stack[t];
t--; // add error exception for empty stack
}
int length(){
return t;
}
int peek(){ //make sure this is what peek actually is supposed to do
return stack[t-1]; //add out of bounds except
}
bool isEmpty(){ //better way to do this? backed myself into a corner?
return t==-1 ; //fix
}

As the comments indicate, I haven’t finished yet.

Advertisements
C++ Stack Implementation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s