Google Ads

July 11, 2015

imamLL linked list library store struct elements

As imamLL (imamLL-1.3.tar.gz) is very versatile in storing any type of data into a list, you can store an struct as an element like shown in the following code example:

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

#include "imamll.h"

struct Person {
    char name[24];
    double age;
    double weight;
    double height;
};

struct imamLL *People_list = NULL;          /* Pointer to hold list */
struct imamLL_element *person = NULL;       /* Pointer to hold individual element */
unsigned long c;

int main (int argc, char* argv[])
{
    printf ("Allocating memory for list\n");
   
    People_list = imamLL_list_create();

    if (People_list == NULL) {
        printf ("Can not create the list\n");
        exit (1);
    }
   
    if ((person = imamLL_element_add (People_list, sizeof(struct Person), AT_END)) == NULL) {
        printf ("Can not add element");
        printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
        exit (1);
    }
    strcpy (((struct Person *)person->data)->name, "Md Imam Hossain");
    ((struct Person *)person->data)->age = 27.0;
    ((struct Person *)person->data)->height = 180.0;
    ((struct Person *)person->data)->weight = 67.0;

    printf ("Allocated: %lu Bytes\n", People_list->size);
   
    if ((person = imamLL_element_add (People_list, sizeof(struct Person), AT_END)) == NULL) {
        printf ("Can not add element");
        printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
        exit (1);
    }
    strcpy (((struct Person *)person->data)->name, "Md Salim Hossain");
    ((struct Person *)person->data)->age = 22.0;
    ((struct Person *)person->data)->height = 182.0;
    ((struct Person *)person->data)->weight = 75.0;

    printf ("Allocated: %lu Bytes\n", People_list->size);
   
    while (1) {
        person = imamLL_element_get_next (People_list);
        if (person == NULL) break;
        printf ("*Person*\n");
        printf ("Name: %s\n", ((struct Person *)person->data)->name);
        printf ("Age: %lf\n", ((struct Person *)person->data)->age);
        printf ("Height: %lf\n", ((struct Person *)person->data)->height);
        printf ("Weight: %lf\n", ((struct Person *)person->data)->weight);
    }
   
    printf ("Freed: %d elements\n", imamLL_list_free (People_list));
    printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
   
    return (EXIT_SUCCESS);
}


July 9, 2015

Fix Ubuntu shutdown and suspend problem on HP 250 G3

If you have a HP 250 G3 notebook and use Ubuntu based GNU/Linux then you may be experiencing problems such as the system will halt at shutdown and will hang when you try to suspend. All these problems are related to each other. You can easily fix these problems by just changing some settings in your BIOS.

First thing you can try if you have Windows operating system, then you can try installing the latest vendor BIOS program and see if the problems persist. After installing latest BIOS if the problems still occur then just go into your BIOS setup screen and look for USB 3 configuration in pre-OS option and then change the USB 3 configuration in pre-OS state to be in enabled. BIOS is the operating system neutral system settings program you can get into when the computer starts. To enter BIOS just try to press Esc key on the keyboard and press one of the F1-F12 key to enter BIOS settings. And if you have done everything correctly, hopefully, you will no longer experience any of the above problems anymore :)

July 5, 2015

imamLL a simple C linked list library

C programming language does not have a linked list implementation like other programming languages such as java. Since C program gives more control to it's users, it is up to users how they implement their own linked list for their programs. imamLL is a linked list implementation library for C designed to be efficient and flexible. Some of the features of imamLL library are
  • Dynamically allocate data at any given point in the program runtime 
  • Add elements of arbitrary sizes into the lists 
  • Navigate through the elements in both forward and backward 
  • Add, remove, modify and get elements from the lists
In this tutorial, we are going to build a simple program using imamLL library.

First, we download imamLL library from the following link:


imamLL-1.3.tar.gz


After extracting the file we run ./build.sh in the terminal to install the library into the system.

Now, we create a blank text file and name it num_list.c

The content of the file is shown below:

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

#include <imamll.h>        /*header for imamLL */

struct imamLL *num_list = NULL;
struct imamLL_element *element = NULL;

int main (int argc, char** argv)
{
    num_list = imamLL_list_create();
   
    if ( num_list == NULL) {
        printf ("Can not create the list\n");
        exit (EXIT_FAILURE);
    }
   
    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 10;
   
    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 20;

    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 30;
   
    imamLL_list_rewind (num_list);

    while ((element = imamLL_element_get_next(num_list)) != NULL) {
        printf ("%d\n", *((int *)element->data));
    }
   
    imamLL_list_destroy (num_list);

    return 0;
}


Now to compile the num_list.c, in the terminal:

gcc -Wall -o num_list num_list.c -limamll

The program above program can be easily understood by reading the Intro.html file found in the imamLL directory. There are also few more example programs located in the examples directory of imamLL.