flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Stack Implementation - Linked lists

Author
Thread Post new topic Reply to topic
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
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
Code:
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
Code:
#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

Code:
#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
Code:
typedef void * Object;
    


tree.h
Code:
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
Code:
#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
Code:
#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
Code:



#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

Code:
#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

Code:
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
Code:
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 ? Very Happy


Exclamation I assume no responsibility of cleaning up Razz

Now someone make one in assembly Razz


Last edited by typedef on 07 Oct 2011, 18:01; edited 1 time in total
Post 07 Oct 2011, 01:51
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Whenever function takes string (or anything) it doesn't change, use "const char * filen" instead of "char * filen". That allows compiler some extra optimizations.
Post 07 Oct 2011, 09:17
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
^^Where did this guy go ?


Last edited by typedef on 21 Oct 2012, 17:51; edited 1 time in total
Post 07 Oct 2011, 17:41
View user's profile Send private message Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael
vid wrote:
Whenever function takes string (or anything) it doesn't change, use "const char * filen" instead of "char * filen". That allows compiler some extra optimizations.
Contrary to popular belief it doesn't, const-correctness is more of a silly-bug-preventing feature, it doesn't help with data flow analysis, which is where that extra optimizations could be exposed. In C and related languages the real problem is pointer aliasing - if the compiler does a good job with alias analysis (possibly with some help from the programmer, see the "restrict" keyword in C99), then that extra optimizations will kick in.
Post 07 Oct 2011, 20:38
View user's profile Send private message Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc
typedef wrote:
Now someone make one in assembly

Why? PUSH/POP and the stack segment don't suffice? Razz
Post 07 Oct 2011, 21:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.