Hey

So yeah i just thought i better update you guys didn't get much time to work on code this week! I will do soon getting closer to being jobless. Anyways..

I just am having problems with functions in stroage/heap/*.c calling mysys/my_list.h which is where the stl::list is defined etc..

But i am un-sure whether i can link in this c++ code into c code unless i was to go through and change that storage/heap code to be c++ code, it shouldnt be to bad/ too much changes to use g++ on them.

But then wondering is it worth that. I knocked up a quick LinkedList header and code, which is very generic.

#include <stdio.h>
#include <stdlib.h>

#include "linkedlist.h"

/**
* @return returns TRUE if has next else FALSE
* */
static int hasNext(struct ListNode *node );

/**
* prints the nodes from there recursivly
* ie: prints the list :) from that node!
* */
static void printNodes(struct ListNode *node);

extern void addItem(void* item, LinkedList *list){

 //if list->head == null
 if(list->head == (struct ListNode*) 0){
   //create head node
   list->head = malloc( sizeof(struct ListNode) );
   list->head->contents = item;
list->tail = NULL;
   list->head->nextnode = list->tail;
 }else{
   struct ListNode *tmp = list->head;
//while node has contents and isn't null
   while( tmp!= NULL && tmp->contents != (void* ) 0
      && tmp->nextnode != NULL)
   {
     tmp = tmp->nextnode;
   }
   tmp->nextnode = malloc( sizeof(struct ListNode) );
   tmp->nextnode->contents = item;
   tmp->nextnode->nextnode = NULL;
list->tail=tmp->nextnode;
 }
}

extern void removeItem(int pos, LinkedList *list){

 //if position is within bounds
 if(pos < size(list)) {
   int i;
   //if removing the head
   if(pos == 0) {
     struct ListNode *next = list->head;
     for(i=0;i<pos+1; i++){
   next=next->nextnode;
     }
     free(list->head);
     list->head = next;
   }
   //if removing the tail
   else if( pos == size(list) -1 ){
     struct ListNode *next = list->head;
     for(i=0;i<pos-1; i++){
   next=next->nextnode;
     }
     next->nextnode = NULL;
     //free allocated mem to tail
     free(list->tail);
     //reallocated tail
     list->tail = next;
   }
   else {
     struct ListNode *next = list->head;
     for(i=0; i<pos; ++i){
   next=next->nextnode;
     }
     struct ListNode *remove_node = next;
     next = list->head;
     for(i=0; i<pos-1; ++i){
   next=next->nextnode;
     }
     struct ListNode *prev_node = next;
     next = list->head;
     for(i=0; i<pos+1; ++i){
   next=next->nextnode;
     }
     struct ListNode *post_node = next;
     next = list->head;
prev_node->nextnode = post_node;
     //free memory allocated to the node
     free(remove_node);
   }
 }
 else
   return;
}

extern void* getItem(int pos, LinkedList *list){
if( pos <= size(list) ){ int i;
   struct ListNode *next = list->head;
   for(i=0; i<pos; i++)
     next = next->nextnode;
return next->contents;
 }
 else
   return NULL;
}

extern void printList(LinkedList *list){
 struct ListNode *next = list->head;
 printNodes(next);
}

static void printNodes(struct ListNode *node){

 if( node != NULL ){
   printf("%s\n", ( char* ) node->contents);
   printNodes(node->nextnode);
 }
 else
   return;
}

extern size_t size(LinkedList *list){
 size_t counter=0;
 struct ListNode *next = list->head;

 if( next!= NULL && next->contents != NULL){
   counter++;
   while( hasNext(next) == TRUE ){
     next = next->nextnode;
     counter++;
   }
 }
 return counter;
}

static int hasNext(struct ListNode *node ){

 if(node->nextnode != (struct ListNode *)0
    && node->contents != (void* ) 0 ){
   return TRUE;
 }
 else
   return FALSE;
}

===============================
#ifndef _LINKEDLIST_H
#define    _LINKEDLIST_H

#define TRUE 1
#define FALSE 0

typedef struct LinkedList {
   struct ListNode *head;
   struct ListNode *tail;
} LinkedList;

struct ListNode {
   void* contents;
   struct ListNode *nextnode;
};

extern void addItem( void* item, LinkedList *list);
extern void removeItem(int pos, LinkedList *list);

/*...@return returns the void* pointer to the object!*/
extern void* getItem(int pos, LinkedList *list);
extern void printList(LinkedList *list);

extern size_t size(LinkedList *list);

#endif    /* _LINKEDLIST_H */

Something like that may be easier to add into the code, and its pretty easy to use, just do something like:

LinkedList *myList = malloc( sizeof(LinkedList) );
addItem("bla1",myList);
addItem("bla2",myList);
addItem("bla3",myList);
addItem("bla4",myList);

The methods are pretty generic so you just pass in the LinkedList your talking about and it should take care of the rest.

Or do any of you guys have any experience mixing C++ classes in c code? Using the extern "C" { } doesnt really seem to work inside #ifdef :S

-Phil
http://redbrain.co.uk
#include <stdio.h>
#include <stdlib.h>

#include "linkedlist.h"

/**
 * @return returns TRUE if has next else FALSE
 * */
static int hasNext(struct ListNode *node );

/**
 * prints the nodes from there recursivly 
 * ie: prints the list :) from that node!
 * */
static void printNodes(struct ListNode *node);

extern void addItem(void* item, LinkedList *list){

  //if list->head == null
  if(list->head == (struct ListNode*) 0){
    //create head node
    list->head = malloc( sizeof(struct ListNode) );
    list->head->contents = item;
    
    list->tail = NULL;
    list->head->nextnode = list->tail;
  }else{
    struct ListNode *tmp = list->head; 
    
    //while node has contents and isn't null
    while( tmp!= NULL && tmp->contents != (void* ) 0 
	   && tmp->nextnode != NULL) 
    {
      tmp = tmp->nextnode;
    }
    tmp->nextnode = malloc( sizeof(struct ListNode) );
    tmp->nextnode->contents = item;
    tmp->nextnode->nextnode = NULL;
    
    list->tail=tmp->nextnode;
  }
}

extern void removeItem(int pos, LinkedList *list){
  
  //if position is within bounds
  if(pos < size(list)) {
    int i;
    //if removing the head
    if(pos == 0) {
      struct ListNode *next = list->head;
      for(i=0;i<pos+1; i++){
	next=next->nextnode;
      }
      free(list->head);
      list->head = next;
    } 
    //if removing the tail
    else if( pos == size(list) -1 ){
      struct ListNode *next = list->head;
      for(i=0;i<pos-1; i++){
	next=next->nextnode;
      }
      next->nextnode = NULL;
      //free allocated mem to tail
      free(list->tail);
      //reallocated tail
      list->tail = next;
    }
    else {
      struct ListNode *next = list->head;
      for(i=0; i<pos; ++i){
	next=next->nextnode;
      }
      struct ListNode *remove_node = next;
      next = list->head;
      for(i=0; i<pos-1; ++i){
	next=next->nextnode;
      }
      struct ListNode *prev_node = next;
      next = list->head;
      for(i=0; i<pos+1; ++i){
	next=next->nextnode;
      }
      struct ListNode *post_node = next;
      next = list->head;
      
      prev_node->nextnode = post_node;
      //free memory allocated to the node
      free(remove_node);
    }
  }
  else
    return;
}

extern void* getItem(int pos, LinkedList *list){
  if( pos <= size(list) ){		
    int i;
    struct ListNode *next = list->head;
    for(i=0; i<pos; i++)
      next = next->nextnode;
    
    return next->contents;
  } 
  else
    return NULL;
}

extern void printList(LinkedList *list){
  struct ListNode *next = list->head;
  printNodes(next);
}

static void printNodes(struct ListNode *node){
  
  if( node != NULL ){
    printf("%s\n", ( char* ) node->contents);
    printNodes(node->nextnode);
  } 
  else
    return;
}

extern size_t size(LinkedList *list){
  size_t counter=0;
  struct ListNode *next = list->head;
  
  if( next!= NULL && next->contents != NULL){
    counter++;
    while( hasNext(next) == TRUE ){
      next = next->nextnode;
      counter++;
    }
  }
  return counter;
}

static int hasNext(struct ListNode *node ){
  
  if(node->nextnode != (struct ListNode *)0 
     && node->contents != (void* ) 0 ){
    return TRUE;
  }
  else
    return FALSE;
}
#ifndef _LINKEDLIST_H
#define	_LINKEDLIST_H

#define TRUE 1
#define FALSE 0

typedef struct LinkedList {
	struct ListNode *head;
	struct ListNode *tail;
} LinkedList;

struct ListNode {
    void* contents;
    struct ListNode *nextnode;
};

extern void addItem( void* item, LinkedList *list);
extern void removeItem(int pos, LinkedList *list);

/*...@return returns the void* pointer to the object!*/
extern void* getItem(int pos, LinkedList *list);
extern void printList(LinkedList *list);

extern size_t size(LinkedList *list);

#endif	/* _LINKEDLIST_H */
_______________________________________________
Mailing list: https://launchpad.net/~drizzle-discuss
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~drizzle-discuss
More help   : https://help.launchpad.net/ListHelp

Reply via email to