The following are implementations of a stack in 3 different languages: JAVA, C & C++
NOTE: Uploaded all
C++: This one is an integer stack, but can also be used as a pointer stack making it versatile.
IntStack.h
class IntStack{
public:
IntStack();
int pop();
bool popf(char * filen);
void push(int value);
bool pushf(char * file);
bool isEmpty();
int length;
void clear();
int peek();
static int getBottom(IntStack * stack);
private:
class Node{
public:
Node(int value, Node * next);
int getData();
void setData(int value);
Node * getNext();
private:
int data;
Node * next;
};
Node * top;
};
IntStack.c
#include "IntStack.h"
#include <fstream>
using namespace std;
IntStack::IntStack(){
this->top = 0;
length = 0;
}
void IntStack::push(int value){
this->top = new Node(value,this->top);
length++;
}
bool IntStack::pushf(char * filen){
bool bSuccess = false;
ifstream file(filen);
int temp = 0;
if(file){
while(file.good()){
file >> temp;
push(temp);
}
bSuccess = true;
}
return (bSuccess);
}
int IntStack::pop(){
Node * temp = this->top;
int value = this->top->getData();
//delete &this->top;
this->top = this->top->getNext();
length--;
return (value);
}
/**
* Pops all the satck values to the file.
* @param filen file name to pop values to
* @return true if poped to file.
*/
bool IntStack::popf(char * filen){
bool bSuccess = false;
int temp = 0;
ofstream of(filen);
if(of) {
while(!isEmpty()){
of<<this->pop()<<endl;
}
bSuccess = true;
}
return (bSuccess);
}
int IntStack::peek(){
return ( top->getData() );
}
void IntStack::clear(){
while(!isEmpty()){
pop();
}
}
bool IntStack::isEmpty(){
return (top==0);
}
IntStack::Node::Node(int val,Node * next){
data = val;
this->next = next;
}
int IntStack::Node::getData(){
return (data);
}
IntStack::Node* IntStack::Node::getNext(){
return(next);
}
void IntStack::Node::setData(int v){
data = v;
}
int IntStack::getBottom(IntStack * stack){
IntStack * temp = new IntStack();
int value=0;
while(!stack->isEmpty()){
temp->push( stack->pop() );
}
value = temp->pop();
while(!temp->isEmpty()){
stack->push( temp->pop() );
}
return (value);
}
Using the class
main.c
#include <stdio.h>
#include "IntStack.h"
int main(){
IntStack * stack = new IntStack();
stack->push(20);
stack->push(230);
stack->push(250);
stack->push(290);
stack->popf("flags.txt");
return 0;
}
C:
defines.h
tree.h
typedef struct fTree
{
struct fTree * next;
Object data;
}Tree,*farTree;
Object fTree_getData(farTree tree);
void fTree_setData(farTree tree,Object data);
farTree fTree_newTree(Object data,farTree next);
void fTree_setNext(farTree tree,farTree next_tree);
farTree fTree_getNext(farTree tree);
tree.c
#include "tree.h"
Object fTree_getData(farTree tree){
return (tree->data);
}
void fTree_setData(farTree tree,Object data){
tree->data = data;
}
farTree fTree_newTree(Object data,farTree next){
farTree tree = (farTree)malloc(sizeof(Tree));
tree->data = data;
tree->next = next;
return (tree);
}
void fTree_setNext(farTree tree,farTree next_tree){
tree->next = next_tree;
}
farTree fTree_getNext(farTree tree){
return (tree->next);
}
stack.h
#include "tree.h"
typedef struct _stack
{
farTree top;
int items;
}Stack, *farStack;
void stack_push(farStack stack,Object data);
Object stack_pop(farStack stack);
Object stack_peek(farStack stack);
Object stack_getBottom(farStack stack);
int stack_isEmpty(farStack stack);
farStack stack_newStack();
stack.c
#include "stack.h"
farStack stack_newStack(){
farStack newStack = (farStack)malloc(sizeof(Stack));
return (newStack);
}
void stack_push(farStack stack,Object data){
stack->top = fTree_newTree(data,stack->top);
stack->items++;
}
Object stack_pop(farStack stack){
Object retObject = 0;
farTree next = fTree_getNext(stack->top);
if(stack->top != 0){
retObject = stack->top->data;
free(&stack->top);
stack->top = next;
stack->items--;
}
return (retObject);
}
Object stack_peek(farStack stack){
return (stack->top ? stack->top->data : 0);
}
Object stack_getBottom(farStack stack){
Object value=0;
farStack temp = stack_newStack();
while ( !stack_isEmpty(stack) ){
stack_push(temp, stack_pop(stack));
}
value = stack_pop(temp);
while (!stack_isEmpty(temp)){
stack_push(stack,stack_pop(temp));
}
free(&temp);
return (value);
}
int stack_isEmpty(farStack stack){
return (stack->top==0);
}
Using it
main.c
#include <stdio.h>
//#include <windows.h>
//#pragma comment(lib,"user32.lib")
#include "stack.h"
int main(int argc, char *argv[])
{
Stack myStack={0,0};
stack_push(&myStack,"I'm on yuwa stack nuggah !");
stack_push(&myStack,"you can also push other data types, just know when to pop 'em ");
stack_push(&myStack,"this gets freed (~_^)");
stack_push(&myStack,"typedef");
stack_push(&myStack,"edfed");
stack_push(&myStack,"revolution");
stack_push(&myStack,"vid");
stack_push(&myStack,"end of strings");
while ( !stack_isEmpty(&myStack) ){
printf("\nPOP: %s\n",stack_pop(&myStack));
}
stack_push(&myStack,(Object)1234567890);
printf("\nPop int: %d\n\nStack is empty yo !\n\n",stack_pop(&myStack));
return 0;
}
JAVA:
IntStack.java
public class IntStack
{
/*
THE LIST
*/
private class List {
private List next;
private int data;
public List(int dat){ new List(dat,null); }
public List(){new List(0,null); }
public List(int dat,List link){data = dat;next = link;}
public void setData(int d){data=d;};
public int getData(){return (data);}
public List getNext(){return (next);}
public void setNext(List link){next=link;}
}
/*
END LIST
*/
private List top;
private int items;
public IntStack(){
top = null;
}
public boolean push(int value){
boolean bSuccess=false;
try{
top = new List(value, top);
items++;
bSuccess=true;
}catch(Exception e){}
return (bSuccess);
}
public boolean isEmpty(){
return (top==null);
}
public int size(){
return (items);
}
public int pop(){
int data = 0;
if(top!=null){
data = top.getData();
top = top.getNext();
items--;
}
return (data);
}
public int peek(){
return ( top !=null ? top.getData() : 0);
}
}
Using it
main.java
public class main {
public static int getBottom(IntStack stack){
int value=0;
IntStack temp = new IntStack();
while( !stack.isEmpty() )
{ /*empty all the values from the stack and push them on the temp sack*/
temp.push( stack.pop() );
}
//the last is now first
value = temp.pop();
//push back all the poped values in their original order
while ( !temp.isEmpty() ){
stack.push( temp.pop() );
}
return (value);
}
public static void main(String []agrs){
IntStack stack1 = new IntStack(), stack2 = new IntStack();
stack2.push(2);
stack2.push(1);
stack2.push(3);
stack2.push(24);
stack2.push(3);
stack2.push(5);
stack1.push(34);
System.out.printf("Stack 1: peek: %d\n",stack1.peek());
System.out.printf("Stack 2 size: %d\n",stack2.size());
System.out.printf("Get bottom of stack 2: %d\n",getBottom(stack2));
while(!stack2.isEmpty()){
System.out.print("stack2 Pop: "+stack2.pop()+"\n");
}
}
}
PS: Should I make one in C# too ?

I assume no responsibility of cleaning up
Now someone make one in assembly
