Monday, September 11, 2017

LinkedList implementation in C with java.util.ArrayList like sublist capability

Place this in an header file, called list.h

/*
 * File:   list.h
 * Author: hp
 *
 * Created on September 3, 2017, 2:41 AM
 */

#ifndef LIST_H
#define    LIST_H

#ifdef    __cplusplus
extern "C" {
#endif




 typedef struct Node {
    struct Node* next;
    struct Node* prev;
    void* val;
 }Node;

 typedef struct List {
     int length;
    struct Node* firstNode;
    struct Node* lastNode;
    struct List* subList;

    struct List* parent;

    //This indices are -1 if the sublist field is NULL
    int startIndex;
    int endIndex;
}List;

List* newList();
Node* initNode(Node* prev, void* val, Node* next);
void ToArray(List* list,void* result[]) ;
void addNode(List* list,Node *elem);
void AddArray(List* list, void* *array, int len);
void Add(List* list,void* val);
bool addNodeAt(List* list,Node* elem,int index );
bool AddVal(List* list,void* val, int index);
bool removeNode(List* list,Node* elem);
void RemoveAll(List* list,List* lst);
bool AddAll(List* list,List* lst);
bool IsEmpty(List* list);
bool AddAllAt(List* list,int index, List *lst);
bool Remove(List* list,void* val);
bool RemoveIndex(List* list,int index);
List* SubList(List* list,int startIndex, int endIndex);
Node* getNode(List* list,int index);
void* Get(List* list,int index);
Node* getLastNode(List* list);
void* LastElement(List* list);
int IndexOf(List* list,void* val);
bool isSubList(List* list);
/**
 *
 * Pass the list and an empty array of size 2. When the function returns, the array contains pointers to the boundary nodes
 */
void getBoundaryNodes(List* list,Node* bounds[]);
int indexOfNode(List* list,Node* node);
bool containsNode(List* list,Node* node);
void prepend(List* list,void* val);
 void append(List* list,void* val);
Node* insertBefore(List* list,void* e, Node* succ);
 Node* insertAfter(List* list, void* e, Node* succ);
bool Contains(List* list,void* val);


void Clear(List* list);
void removeLinkedRange(List* list, Node* startNode, Node* stopNode);
void Log(List* list,char* optionalLabel);

void syncRemoval(List* list, int index);
void syncAdditions(List* list, int index);
char* concatStrAndInt(char* str, int val);
 char* concat(char*str, char* str1);
 char* prependInt(int num, char* str2);
 char* appendInt( char* str , int num);
 char* concatOnInt( char* str , int num, char* str2);
 

   
   

#ifdef    __cplusplus
}
#endif

#endif    /* LIST_H */





Now, save this in another file called list.c





#include
#include
#include




This is the code for the doubly-linked linked list.

Now create a c file called listmain.c  that will test the list:



void quickAddInt(List* list , int num){
    void* num_ptr = #
    Add(list , num_ptr);
}

int main() {
   
    List* list = newList();
 
 
 
 
    Add(list, (void *)3);
    Add(list, (void *)8);
   
      srand(time(0));
        int TEST_SIZE = 25;

    int n = 200000;
    /* Assign pointers into appropriate parts of matrix. */
        for (int i = 0; i < TEST_SIZE; i++) {
            int rnd = rand() % n;
            void* p = &rnd;
          Add(list , p);
        }
 
    List* small_list = SubList(list , 5 , 12);
 
    Log(list, "LIST");
    Log(small_list, "SUB list");
 
    Clear(small_list);
 
 
    Log(list, "AFTER CLEARING SUB, list");
    Log(small_list, "SUB list");
   
   
 
    List* second = newList();
    quickAddInt(second , 5);
    quickAddInt(second , 205);
    quickAddInt(second , 502);
    quickAddInt(second , 45);
    quickAddInt(second , 67);
    quickAddInt(second , 99);
    quickAddInt(second , 510);
    quickAddInt(second , 1020);
    quickAddInt(second , 5008);
    quickAddInt(second , 30042);
    quickAddInt(second , 20014);
    quickAddInt(second , 30041);
    quickAddInt(second , 10518902); 
         
    Log(second, "SECOND list");
 
    AddAll(small_list, second);
 
 
    Log(small_list, "ADD second_list to SUB list...SUB list is now");
    Log(list, "LIST is now");
 
 
    void* arr[9] = {(void*)10,(void*)4,(void*)1,(void*)9,(void*)8,(void*)12,(void*)34,(void*)4,(void*)58};
    int siz = sizeof(arr)/sizeof(arr[0]);
    AddArray( small_list , arr, siz );
 
 
 
 
    Log(small_list, "AddValues called on SUB list...SUB list is now");
    Log(list, "LIST is now");
   
  void* array[4] = {(void*)1245,(void*)90,(void*)42,(void*)89};
    AddArray(list , array ,4);
 
    Log(list, "LIST");
 
    RemoveAll(list, SubList(second,0,7));
 
    Log(list, "LIST");
 

    return (EXIT_SUCCESS);
}





LinkedList implementation in C with java.util.ArrayList like sublist capability

Place this in an header file, called list.h /*  * File:   list.h  * Author: hp  *  * Created on September 3, 2017, 2:41 AM  */ #if...