0

Ok so im trying to implement a stack using vectors in C++

i just want to know if the following implementation code makes sense, this is just the implementation file, the client file is working fine but when i run the program it doesnt work so i believe it has to do something wit the implementation file below. what the client program suppose to is let the user enter a postfix notation and the program will evaluate the result. Please any help

#include<iostream>
#include "stackV.h"
using namespace std;

stack::stack()
{
  maxsize = 100;
  currsize = 0;
}
                                                 
stack::~stack()
{
  clearIt();

}
                                                                          
bool stack::pop(el_t &)
{
    if(!isEmpty())
      {
        el.erase(el.begin() + 0);
        currsize--;
        return true;
      }
}

void stack::push(el_t n)
{
  if(!isFull())
    {
      el.insert(el.begin() + 0,n);
      currsize++;
    }

}
                                                                                                                                                                                                                  
el_t stack::topElem(el_t &n)
{
  if(!isEmpty())
    return el[0];

}                                                                                                                                                                                                                                                                                                                                                  
bool stack::isFull()
{
  return false;
}
                                                                                                                                                                                                                                                                                                                                         
bool stack::isEmpty()
{
  return(currsize == 0);
}                                                      
                           
void stack::displayAll()
{

  for(int i = 0; i < el.size(); i++)
    cout << el[i] << " " ;

}
  
void stack::clearIt()
{


}

Edited by Nick Evan: Added [CODE] tags

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by israruval
0

Vector has push_back and pop_back methods, so just use the end of the vector as the top of the stack. You'll have to peek at the end to get the value before you pop it, though.

Edited by jonsca: n/a

0

im suppose to implement my own stack class using the vector class, and how would i implement peek and how would i apply it to pop?

0

std::vector has a size() method. Use it (and subtract 1) to find the index of the last element. Access that with [].

0

ok so i got this to access the last element, i dont know if its right and also how would i implement the pop function

el_t stack::topElem()
{
el_t n;

if(isEmpty())
cout <<"stack is empty";
else
{
n = el.back();
}
return n;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.